fix(metadata): keep a subclass's decorators off its base class (GitHub #44)
CI / dco (pull_request) Successful in 4s
CI / runtimes (bun) (pull_request) Successful in 20s
CI / build (pull_request) Successful in 29s
CI / runtimes (edge) (pull_request) Successful in 19s
CI / runtimes (deno) (pull_request) Successful in 34s

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>
This commit is contained in:
chrnx committed 2026-10-24 15:55:47 -05:00
1 parent 9f6f916063
commit b256bc8570
5 files changed
+51 -5

No files matched your search

+15
View File
@@ -110,3 +110,18 @@ describe('@Html', () => {
expect(getHtmlMeta(C, 'm')).toBeUndefined();
});
});
// GitHub #44: the route decorator had the same read-the-inherited-array-then-push pattern as
// `record` in params.ts, so a route on a subclass was added to the base controller as well.
describe('route inheritance', () => {
class Base { @Get('/a') a() {} }
class Child extends Base { @Get('/b') b() {} }
it('a route on a subclass leaves the base class unchanged', () => {
expect(getRoutes(Base).map((route) => route.handlerName)).toEqual(['a']);
});
it('the subclass keeps the inherited routes and adds its own', () => {
expect(getRoutes(Child).map((route) => route.handlerName)).toEqual(['a', 'b']);
});
});
+22
View File
@@ -187,3 +187,25 @@ describe('resolveArgs validation', () => {
}).toThrow();
});
});
// GitHub #44: `record` read the inherited map and pushed into it, so a subclass's decorator landed
// on the base class too.
describe('inheritance', () => {
class Base {
list(@query('page') _page: unknown) {}
}
class Child extends Base {
override list(@query('page') _page: unknown, @query('sort') _sort?: unknown) {}
other(@param('id') _id: unknown) {}
}
it('decorating a subclass method leaves the base class unchanged', () => {
expect(getArgs(Base, 'list')).toEqual([{ index: 0, source: 'query', key: 'page' }]);
expect(getArgs(Base, 'other')).toEqual([]);
});
it('the subclass keeps what it inherited and adds its own', () => {
expect(getArgs(Child, 'list').map((spec) => spec.key)).toEqual(['page', 'page', 'sort']);
expect(getArgs(Child, 'other')).toEqual([{ index: 0, source: 'params', key: 'id' }]);
});
});