Skip to content
New issue

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

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

Already on GitHub? # to your account

Typebox Boolean regression in 1.1.17 #872

Open
ryanleecode opened this issue Oct 10, 2024 · 7 comments
Open

Typebox Boolean regression in 1.1.17 #872

ryanleecode opened this issue Oct 10, 2024 · 7 comments
Labels
bug Something isn't working

Comments

@ryanleecode
Copy link

What version of Elysia is running?

1.1.17

What platform is your computer?

Linux 6.6.52-1-MANJARO x86_64 unknown

What steps can reproduce the bug?

Create api route where query parameter has a boolean

for example

query: t.Object({
  registered: t.Optional(
    t.Boolean({
      description: 'Filter usernames by registration status',
    }),
  ),
}),

Submit request and do not include the boolean value, in this case omit registered

What is the expected behavior?

it works

What do you see instead?

Tybebox Error

Additional information

No response

Have you try removing the node_modules and bun.lockb and try again yet?

No response

@ryanleecode ryanleecode added the bug Something isn't working label Oct 10, 2024
@aburii
Copy link

aburii commented Oct 15, 2024

I've been experiencing the same when using optional booleans in query parameters
image

@ilijapuaca
Copy link

Same issue here, was running 1.1.16 so the issue was likely introduced in an earlier version. Tried upgrading but it's still there, defining an optional boolean in a query is always being treated as required

@crishoj
Copy link
Contributor

crishoj commented Nov 2, 2024

Can you provide a minimal example to reproduce the issue?

Optional boolean query parameters seem to work in Elysia 1.1.23:

import Elysia, {t} from 'elysia'

const app = new Elysia()
    .get('/test', ({query: {registered}}) => {
        return `Registered: ${registered}`
    }, {
        query: t.Object({registered: t.Optional(t.Boolean())}),
    })

const urls = [
    'https://example.com/test?registered=true',
    'https://example.com/test?registered=false',
    'https://example.com/test'
]

for (const url of urls) {
    const res = await app.handle(new Request(url))
    console.log(url, '→', await res.text())
}

Output:

https://example.com/test?registered=true → Registered: true
https://example.com/test?registered=false → Registered: false
https://example.com/test → Registered: undefined

@ilijapuaca
Copy link

@crishoj I almost gave up trying to reproduce it as your snippet seems to work well, but if you add another required property to the query object it fails with a boolean validation error

@crishoj
Copy link
Contributor

crishoj commented Nov 3, 2024

@ilijapuaca Bingo — reproducible when second query param is added to the schema:

import Elysia, {t} from 'elysia'

const app = new Elysia()
    .get('/test', ({query: {registered}}) => {
        return `Registered: ${registered}`
    }, {
        query: t.Object({
            registered: t.Optional(t.Boolean()),
            other: t.String(),
        }),
    })

const urls = [
    'https://example.com/test?other=foo&registered=true',
    'https://example.com/test?other=foo&registered=false',
    'https://example.com/test?other=foo'
]

for (const url of urls) {
    const res = await app.handle(new Request(url))
    console.log(url, '→', res.status, await res.text())
}

Output

image

@crishoj
Copy link
Contributor

crishoj commented Nov 3, 2024

The issue appears to only affect ahead-of-time optimized handlers, which is Elysia's default:

image

crishoj added a commit to crishoj/elysia that referenced this issue Nov 3, 2024
crishoj added a commit to crishoj/elysia that referenced this issue Nov 3, 2024
@crishoj
Copy link
Contributor

crishoj commented Nov 3, 2024

Possible fix for the issue: #907

I am not sure I understand the intended behavior of BooleanString.Decode well enough, though, so hoping @bogeychan will chime in here.

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

5 participants
@crishoj @ilijapuaca @ryanleecode @aburii and others