fix(metadata): keep a subclass's decorators off its base class (GitHub #44) #168
No files matched your search
@@ -293,6 +293,14 @@ npm treats versions as semver and semver forbids leading zeros.
|
||||
against the disconnect, and the source's `return()` is called without being waited on, so one
|
||||
that hangs or throws can no longer hold up or break the cleanup. Compressed streams included.
|
||||
|
||||
- **Decorating a subclass no longer changes its base class** (GitHub #44). The argument
|
||||
decorators (`@query`, `@param`, `@needs`, …) and the route decorators (`@Get`, `@Ws`, …) read
|
||||
their metadata with `Reflect.getMetadata`, which walks the prototype chain, and then pushed into
|
||||
what came back. On a subclass that was the base class's own map or array, so a `@query` on
|
||||
`Child.list` also appeared in `getArgs(Base, 'list')`, and a route declared on `Child` was served
|
||||
by `Base` too. Each decorator now writes a copy onto the class it decorates. The subclass still
|
||||
starts from what it inherits.
|
||||
|
||||
- **`listen()` rejects when the port is taken** (#138). A bind failure such as `EADDRINUSE` or
|
||||
`EACCES` arrives as the server's `error` event, which `listen()` did not handle: the promise
|
||||
never settled, and the error surfaced as an unhandled one. It now rejects with that error. The
|
||||
|
||||
+2
-1
@@ -124,7 +124,8 @@ export type RouteDecorator = (
|
||||
function routeDecorator(method: HttpMethod, transport: Transport): RouteDecorator {
|
||||
return (path, opts) => (target, propertyKey) => {
|
||||
const ctor = target.constructor;
|
||||
const routes = (Reflect.getMetadata(K.routes, ctor) as RouteMeta[]) ?? [];
|
||||
// A copy, as in `record` (params.ts): the inherited array belongs to the base class (GitHub #44).
|
||||
const routes = [...((Reflect.getMetadata(K.routes, ctor) as RouteMeta[]) ?? [])];
|
||||
routes.push({
|
||||
method,
|
||||
path,
|
||||
|
||||
+4
-4
@@ -31,10 +31,10 @@ const ARGS = Symbol('gt:args');
|
||||
function record(target: object, methodKey: string | symbol | undefined, spec: ArgSpec): void {
|
||||
if (methodKey === undefined) return; // constructor params not supported
|
||||
const ctor = target.constructor as Ctor;
|
||||
const map: Record<string, ArgSpec[]> = Reflect.getMetadata(ARGS, ctor) ?? {};
|
||||
const list = map[String(methodKey)] ?? [];
|
||||
list.push(spec);
|
||||
map[String(methodKey)] = list;
|
||||
// Copy, never mutate (GitHub #44): `getMetadata` walks the prototype chain, so on a subclass the
|
||||
// map and list it returns are the base class's, and pushing into them decorated the base too.
|
||||
const map: Record<string, ArgSpec[]> = { ...(Reflect.getMetadata(ARGS, ctor) ?? {}) };
|
||||
map[String(methodKey)] = [...(map[String(methodKey)] ?? []), spec];
|
||||
Reflect.defineMetadata(ARGS, map, ctor);
|
||||
}
|
||||
|
||||
|
||||
@@ -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']);
|
||||
});
|
||||
});
|
||||
@@ -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' }]);
|
||||
});
|
||||
});
|
||||
Reference in new issue
Block a user