Skip to content

Commit c3e6afc

Browse files
committed
fix(rest): Fix parameter description
Avoid describing parameters if debug is not on Check undefined parameters (not decorated with @param) in the array
1 parent 3f124f3 commit c3e6afc

File tree

2 files changed

+34
-8
lines changed

2 files changed

+34
-8
lines changed

packages/rest/src/router/routing-table.ts

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -89,13 +89,15 @@ export class RoutingTable {
8989
registerRoute(route: RouteEntry) {
9090
// TODO(bajtos) handle the case where opSpec.parameters contains $ref
9191
// See https://github.com/strongloop/loopback-next/issues/435
92-
debug(
93-
'Registering route %s %s -> %s(%s)',
94-
route.verb,
95-
route.path,
96-
route.describe(),
97-
describeOperationParameters(route.spec),
98-
);
92+
if (debug.enabled) {
93+
debug(
94+
'Registering route %s %s -> %s(%s)',
95+
route.verb,
96+
route.path,
97+
route.describe(),
98+
describeOperationParameters(route.spec),
99+
);
100+
}
99101
this._routes.push(route);
100102
}
101103

@@ -322,6 +324,6 @@ export class ControllerRoute extends BaseRoute {
322324

323325
function describeOperationParameters(opSpec: OperationObject) {
324326
return ((opSpec.parameters as ParameterObject[]) || [])
325-
.map(p => p.name)
327+
.map(p => (p && p.name) || '')
326328
.join(', ');
327329
}

packages/rest/test/unit/router/routing-table.test.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@ import {
88
parseRequestUrl,
99
RoutingTable,
1010
ControllerRoute,
11+
getControllerSpec,
12+
param,
13+
get,
1114
} from '../../..';
1215
import {expect, ShotRequestOptions, ShotRequest} from '@loopback/testlab';
1316
import {anOpenApiSpec} from '@loopback/openapi-spec-builder';
@@ -30,6 +33,27 @@ describe('RoutingTable', () => {
3033
);
3134
});
3235

36+
it('does not fail if some of the parameters are not decorated', () => {
37+
class TestController {
38+
@get('/greet')
39+
greet(prefix: string, @param.query.string('message') message: string) {
40+
return prefix + ': ' + message;
41+
}
42+
}
43+
const spec = getControllerSpec(TestController);
44+
const table = new RoutingTable();
45+
table.registerController(TestController, spec);
46+
const paths = table.describeApiPaths();
47+
const params = paths['/greet']['get'].parameters;
48+
expect(params).to.have.property('length', 2);
49+
expect(params[0]).to.be.undefined();
50+
expect(params[1]).to.have.properties({
51+
name: 'message',
52+
in: 'query',
53+
type: 'string',
54+
});
55+
});
56+
3357
it('finds simple "GET /hello" endpoint', () => {
3458
const spec = anOpenApiSpec()
3559
.withOperationReturningString('get', '/hello', 'greet')

0 commit comments

Comments
 (0)