fix(metadata): keep a subclass's decorators off its base class (GitHub #44) #168

Merged
chrnx merged 1 commits from fix/param-metadata-inheritance into develop 2026-10-24 23:31:37 +00:00
Owner

Fixes GitHub #44.

record (src/params.ts) read the argument map with Reflect.getMetadata, which walks the prototype chain, then pushed into what it got back. On a subclass that was the base class's map, so a @query on Child.list also showed up in getArgs(Base, 'list').

Same bug, also fixed here: the route decorator in src/metadata.ts did the same with the routes array, so a @Get on a subclass was added to the base controller. The other decorators in metadata.ts (@Provider, @Step, @Route, @Module, @Transformer, @Html) define a fresh value and never mutate what they read, so they are unaffected.

Fix. Both now write a copy onto the decorated class: a shallow copy of the map plus a new list in params.ts, a new array in metadata.ts. This copies on every write, not only the first. getMetadata returns the class's own data once it has any, so the result is the same as a getOwnMetadata copy-on-first-write. It costs one array copy per decorator, at decoration time only.

Worth a look: a subclass that overrides a method keeps the base's argument specs for that method and adds its own. With the same index on both, getArgs returns two specs for that index. That was already the behaviour on the subclass before, and the issue asks for "clone the inherited map and list", so I kept it. Starting an overridden method from an empty list would be a separate decision.

Tests: test/params.test.ts and test/metadata.test.ts, "inheritance" blocks. The base-unchanged cases failed on develop (expected ['a','b'] to deeply equal ['a']) and pass with the fix.

Ran: lint (0 errors), format:check, typecheck, npm test (1165 passed), complexity:check, test:deno (63 passed), test:bun (52 passed), test:edge (PASS).

Fixes GitHub #44. `record` (src/params.ts) read the argument map with `Reflect.getMetadata`, which walks the prototype chain, then pushed into what it got back. On a subclass that was the base class's map, so a `@query` on `Child.list` also showed up in `getArgs(Base, 'list')`. **Same bug, also fixed here:** the route decorator in src/metadata.ts did the same with the `routes` array, so a `@Get` on a subclass was added to the base controller. The other decorators in metadata.ts (`@Provider`, `@Step`, `@Route`, `@Module`, `@Transformer`, `@Html`) define a fresh value and never mutate what they read, so they are unaffected. **Fix.** Both now write a copy onto the decorated class: a shallow copy of the map plus a new list in params.ts, a new array in metadata.ts. This copies on every write, not only the first. `getMetadata` returns the class's own data once it has any, so the result is the same as a `getOwnMetadata` copy-on-first-write. It costs one array copy per decorator, at decoration time only. **Worth a look:** a subclass that overrides a method keeps the base's argument specs for that method and adds its own. With the same index on both, `getArgs` returns two specs for that index. That was already the behaviour on the subclass before, and the issue asks for "clone the inherited map and list", so I kept it. Starting an overridden method from an empty list would be a separate decision. Tests: `test/params.test.ts` and `test/metadata.test.ts`, "inheritance" blocks. The base-unchanged cases failed on develop (`expected ['a','b'] to deeply equal ['a']`) and pass with the fix. Ran: lint (0 errors), format:check, typecheck, `npm test` (1165 passed), complexity:check, test:deno (63 passed), test:bun (52 passed), test:edge (PASS).
chrnx added 1 commit 2026-10-24 23:27:38 +00:00
fix(metadata): keep a subclass's decorators off its base class (GitHub #44)
CI / dco (pull_request) Successful in 4s
CI / build (pull_request) Successful in 29s
CI / runtimes (bun) (pull_request) Successful in 20s
CI / runtimes (deno) (pull_request) Successful in 33s
CI / runtimes (edge) (pull_request) Successful in 19s
dae052f400
record in params.ts read the argument map with Reflect.getMetadata,
which walks the prototype chain, then pushed into the list it got back
and defined that same object on the subclass. On a subclass with no
metadata of its own, that object was the base class's, so a @query on
Child.list also showed up in getArgs(Base, 'list').

routeDecorator in metadata.ts had the same pattern with the routes
array, so a @Get on a subclass was also served by the base controller.
It is the same bug in the same shape, so it is fixed here too.

Both now write a copy onto the decorated class: a shallow copy of the
map and a new list for the method in params.ts, a new array in
metadata.ts. getMetadata returns the class's own metadata once it has
some, so later decorators on that class copy its own data, not the
base's. A subclass still starts from what it inherits, as before.

The other decorators in metadata.ts (@Provider, @Step, @Route,
@Module, @Transformer, @Html) define a fresh value per class or per
method and never mutate what they read, so they are unaffected.

Refs: GitHub #44
Signed-off-by: Diego Resendez <diego.resendez@zero-oneit.com>
chrnx force-pushed fix/param-metadata-inheritance from b256bc8570 to dae052f400 2026-10-24 23:27:38 +00:00 Compare
chrnx merged commit ebb828a495 into develop 2026-10-24 23:31:37 +00:00
Sign in to join this conversation.