elysiajs / elysia

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

Normalize not working for children instances #692

Closed dodas closed 2 months ago

dodas commented 3 months ago

What version of Elysia.JS is running?

1.0.25

What platform is your computer?

Linux 5333aedf2660 6.6.26-linuxkit #1 SMP Sat Apr 27 04:13:19 UTC 2024 aarch64 Linux

What steps can reproduce the bug?

const child = new Elysia({ normalize: true }).get("/", () => ({ a: 1, b: 2 }), {
  response: t.Object({ a: t.Number() }),
});

const main = new Elysia().use(child);

const resp = await main.handle(new Request("http://localhost/"));

// resp.status === 422

// resp.json():
// {
//   type: 'validation',
//   on: 'response',
//   property: '/b',
//   message: 'Unexpected property',
//   expected: { a: 0 },
//   found: { a: 1, b: 2 },
//   errors: [
//     {
//       type: 42,
//       schema: [Object],
//       path: '/b',
//       value: 2,
//       message: 'Unexpected property'
//     }
//   ]
// }

What is the expected behavior?

The normalize config option should be respected on individual Elysia sub-instances.

What do you see instead?

Normalize only works when it's set on the root Elysia instance (main in the example above).

kravetsone commented 2 months ago

It works fine with elysia@1.1.3

import Elysia, { t } from "elysia";

const child = new Elysia({ normalize: true }).get("/", () => ({ a: 1, b: 2 }), {
    response: t.Object({ a: t.Number() }),
});

const main = new Elysia().use(child);

const resp = await main.handle(new Request("http://localhost/"));
console.log(resp, await resp.json());

result is

Response (0 KB) {
  ok: true,
  url: "",
  status: 200,
  statusText: "",
  headers: Headers {
    "content-type": "application/json;charset=utf-8",
  },
  redirected: false,
  bodyUsed: true
} {
  a: 1,
}
dodas commented 2 months ago

Can confirm, seems like it's been fixed here 👍