elysiajs / elysia

Ergonomic Framework for Humans
https://elysiajs.com
MIT License
9.74k stars 207 forks source link

Type coercion not working when `aot` is off #752

Closed crishoj closed 1 week ago

crishoj commented 1 month ago

What version of Elysia.JS is running?

1.1.3

What platform is your computer?

Darwin 23.5.0 arm64 arm

What steps can reproduce the bug?

import Elysia, {t} from "elysia"
import {describe, it, expect} from "bun:test"

export const req = (path: string, options?: RequestInit) =>
    new Request(`http://localhost${path}`, options)

describe("AOT", () => {
    it.each([
        ["enabled", {aot: true}],
        ["disabled", {aot: false}],
    ])("aot %s should parse query parameter according to schema", async (_name, options) => {

        const app = new Elysia(options)
            .get('items/:id', 
              ({params: {id}}) => typeof id,
              {params: t.Object({id: t.Number()})})

        const res = await app.handle(req('/items/2'))

        expect(await res.text()).toBe('number');
    });
});

What is the expected behavior?

I expect the param id to be cast to number (according to the schema) regardless of aot.

✓ AOT > aot enabled should parse query parameter according to schema
✓ AOT > aot disabled should parse query parameter according to schema

What do you see instead?

When aot is off, the id param is passed to the handler as string, not as number.

✓ AOT > aot enabled should parse query parameter according to schema [6.64ms]
✗ AOT > aot disabled should parse query parameter according to schema [1.69ms]

Additional information

No response

crishoj commented 1 month ago

In https://github.com/crishoj/elysia/commit/9cc09ed5a4bd1b218daf4bf98891b0725cf4f941 I used describe.each() to run tests/validator/* with and without aot.

With { aot: false } a number of validator tests fail, including these:

✗ Params Validator > create default number params [0.50ms]

Expected: "number"
Received: "string"

✗ Params Validator > coerce number object to numeric [0.17ms]

Expected: "boolean"
Received: "string"
crishoj commented 1 month ago

@SaltyAom if you can point me to where type coercion without aot takes place, I'd be happy to look into this issue.