Skip to content

Commit a6d837e

Browse files
bugfix(@nestjs/common) fix custom decorators (data isnt always string)
1 parent 0904b0b commit a6d837e

2 files changed

Lines changed: 25 additions & 4 deletions

File tree

packages/common/decorators/http/create-route-param-metadata.decorator.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import {
66
import { PipeTransform } from '../../index';
77
import { Type } from '../../interfaces';
88
import { CustomParamFactory } from '../../interfaces/features/custom-route-param-factory.interface';
9-
import { isNil, isString } from '../../utils/shared.utils';
9+
import { isFunction, isNil } from '../../utils/shared.utils';
1010
import { ParamData, RouteParamsMetadata } from './route-params.decorator';
1111

1212
const assignCustomMetadata = (
@@ -51,7 +51,11 @@ export function createParamDecorator(
5151
const args =
5252
Reflect.getMetadata(ROUTE_ARGS_METADATA, target.constructor, key) || {};
5353

54-
const hasParamData = isNil(data) || isString(data);
54+
const isPipe = pipe =>
55+
pipe &&
56+
((isFunction(pipe) && pipe.prototype) || isFunction(pipe.transform));
57+
58+
const hasParamData = isNil(data) || !isPipe(data);
5559
const paramData = hasParamData ? data : undefined;
5660
const paramPipes = hasParamData ? pipes : [data, ...pipes];
5761

packages/common/test/decorators/create-route-param-metadata.spec.ts

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,15 @@ describe('createRouteParamDecorator', () => {
1818
const Decorator = createRouteParamDecorator(factoryFn);
1919

2020
describe('when 0 pipes have been passed', () => {
21-
const data = 'test';
21+
const data = { data: 'test' };
2222
class Test {
2323
public test(@Decorator(data) param) {}
2424
}
2525
it('should enhance param with "data"', () => {
2626
const metadata = Reflect.getMetadata(ROUTE_ARGS_METADATA, Test, 'test');
2727
const key = Object.keys(metadata)[0];
2828
expect(metadata[key]).to.be.eql({
29-
data: 'test',
29+
data,
3030
factory: factoryFn,
3131
index: 0,
3232
pipes: [],
@@ -44,6 +44,8 @@ describe('createRouteParamDecorator', () => {
4444
) {}
4545

4646
public testNoData(@Decorator(pipe) param) {}
47+
48+
public testNoDataClass(@Decorator(ParseIntPipe) param) {}
4749
}
4850
it('should enhance param with "data" and ParseIntPipe', () => {
4951
const metadata = Reflect.getMetadata(ROUTE_ARGS_METADATA, Test, 'test');
@@ -70,6 +72,21 @@ describe('createRouteParamDecorator', () => {
7072
pipes: [pipe],
7173
});
7274
});
75+
76+
it('should enhance param with ParseIntPipe metatype', () => {
77+
const metadata = Reflect.getMetadata(
78+
ROUTE_ARGS_METADATA,
79+
Test,
80+
'testNoDataClass',
81+
);
82+
const key = Object.keys(metadata)[0];
83+
expect(metadata[key]).to.be.eql({
84+
data: undefined,
85+
factory: factoryFn,
86+
index: 0,
87+
pipes: [ParseIntPipe],
88+
});
89+
});
7390
});
7491
});
7592
});

0 commit comments

Comments
 (0)