Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

getQuery returns string value; expected boolean #794

Closed
bacongobbler opened this issue Jun 26, 2024 · 1 comment
Closed

getQuery returns string value; expected boolean #794

bacongobbler opened this issue Jun 26, 2024 · 1 comment
Labels
bug Something isn't working

Comments

@bacongobbler
Copy link

bacongobbler commented Jun 26, 2024

Environment

h3 1.12.0, 2.0.0-beta.0, main

Reproduction

describe("getQuery", () => {
    it("can parse generic types", async () => {
      ctx.app.use(
        "/",
        eventHandler((event) => {
          const query = getQuery<{ bool: boolean }>(event);
          expect(query).toMatchObject({
            bool: true,
          });
          return "200";
        }),
      );
      const result = await ctx.request.get("/api/test?bool=true");

      expect(result.text).toBe("200");
    });
  });

Describe the bug

h3's getQuery parses boolean types as strings. ufo's getQuery parses them as booleans. This causes issues when passing a generic that accepts booleans.

The reproduction test provided above (added as part of utils.test.ts) fails with the following assertion error:

AssertionError: expected '{"statusCode":500,"stack":["Assertion…' to be '200' // Object.is equality

- Expected
+ Received

- 200
+ {"statusCode":500,"stack":["AssertionError: expected { bool: 'true' } to match object { bool: true }","at Proxy.<anonymous> (file:///home/bacongobbler/code/github.com/unjs/h3/node_modules/.pnpm/@vitest+expect@1.6.0/node_modules/@vitest/expect/dist/index.js:951:13)","at Proxy.<anonymous> (file:///home/bacongobbler/code/github.com/unjs/h3/node_modules/.pnpm/@vitest+expect@1.6.0/node_modules/@vitest/expect/dist/index.js:800:17)","at Proxy.methodWrapper (/home/bacongobbler/code/github.com/unjs/h3/node_modules/.pnpm/chai@4.4.1/node_modules/chai/lib/chai/utils/addMethod.js:57:25)","at Object.handler (/home/bacongobbler/code/github.com/unjs/h3/test/utils.test.ts:222:25)","at Object.handler (/home/bacongobbler/code/github.com/unjs/h3/src/app/_handler.ts:51:33)","at processTicksAndRejections (node:internal/process/task_queues:95:5)","at Server.nodeHandler (/home/bacongobbler/code/github.com/unjs/h3/src/adapters/node/utils.ts:27:25)"]}

This causes issues down the road because query.bool is a boolean type, not a string type.

Additional context

test case confirming bool: "true" (string type) is expected in h3:

bool: "true",

test case confirming bool: true (boolean type) is expected in ufo: https://github.com/unjs/ufo/blob/666091db37a03dc871e25d3621aeff1b89648196/test/query.test.ts#L16-L18

Logs

No response

@bacongobbler bacongobbler added the bug Something isn't working label Jun 26, 2024
@pi0
Copy link
Member

pi0 commented Jul 18, 2024

Thanks for reporting issue. Upcoming h3 v2 is being based on URLSearchParams as parser which only accepts string as query values therefore you should base your types to only rely on string as possible value.

You can use destr also to safely parse json values.

@pi0 pi0 closed this as not planned Won't fix, can't repro, duplicate, stale Jul 18, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants