elysiajs / elysia

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

Avoid response normalization side-effects #905

Closed crishoj closed 1 week ago

crishoj commented 3 weeks ago

The Typebox cleaner mutates the cleaned value:

Clean is a mutable operation. To avoid mutation, Clone the value first.

if (IsSchema(additionalProperties) && Check(additionalProperties, references, value[key])) {
    value[key] = Visit(additionalProperties, references, value[key]);
    continue;
}
delete value[key];

Therefore, it can break type-safety and lead to subtle bugs (and poor DX) when long-lived server objects are used in responses.

This proposed fix here is to use structuredClone() on the response body before cleaning.

Fixes #897

crishoj commented 1 week ago

🙏