Skip to content

Commit 55b041a

Browse files
committed
fix(cli): allow path level parameters for openapi
1 parent 85fcbd4 commit 55b041a

File tree

4 files changed

+41
-5
lines changed

4 files changed

+41
-5
lines changed

packages/cli/generators/openapi/spec-helper.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,29 @@ function getTagDescription(apiSpec, tag) {
5151
return undefined;
5252
}
5353

54+
/**
55+
* Merge path level parameters into the operation level
56+
* @param {OperationObject} operationSpec Operation spec
57+
* @param {ParameterObject[]} pathLevelParams Path level parameters
58+
*/
59+
function mergeParameters(operationSpec, pathLevelParams) {
60+
if (!pathLevelParams || pathLevelParams.length === 0) return;
61+
for (const p of pathLevelParams) {
62+
operationSpec.parameters = operationSpec.parameters || [];
63+
let found = false;
64+
for (const param of operationSpec.parameters) {
65+
if (p.name === param.name && p.in === param.in) {
66+
// The parameter has been overridden at operation level
67+
found = true;
68+
break;
69+
}
70+
}
71+
if (!found) {
72+
operationSpec.parameters.push(p);
73+
}
74+
}
75+
}
76+
5477
/**
5578
* Group operations by controller class name
5679
* @param {object} apiSpec OpenAPI 3.x spec
@@ -61,10 +84,12 @@ function groupOperationsByController(apiSpec) {
6184
for (const path in apiSpec.paths) {
6285
if (isExtension(path)) continue;
6386
debug('Path: %s', path);
87+
const pathLevelParams = apiSpec.paths[path].parameters;
6488
for (const verb in apiSpec.paths[path]) {
6589
if (isExtension(verb) || !isHttpVerb(verb)) continue;
6690
debug('Verb: %s', verb);
6791
const op = apiSpec.paths[path][verb];
92+
mergeParameters(op, pathLevelParams);
6893
const operation = {
6994
path,
7095
verb,

packages/cli/generators/openapi/utils.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ function escapeIdentifier(name) {
145145
return '_' + name;
146146
}
147147
if (!name.match(SAFE_IDENTIFER)) {
148-
return camelCase(name);
148+
return _.camelCase(name);
149149
}
150150
return name;
151151
}

packages/cli/test/fixtures/openapi/3.0/customer.yaml

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,17 @@ tags:
1111
description: Customer resource
1212
paths:
1313
/customers:
14+
parameters:
15+
- name: access-token
16+
in: query
17+
description: Access token
18+
required: false
19+
schema:
20+
type: string
1421
get:
1522
tags:
1623
- Customer
17-
description: Returna all customers
24+
description: Returns all customers
1825
parameters:
1926
- name: if
2027
in: query

packages/cli/test/unit/openapi/controller-spec.unit.js

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,17 +23,21 @@ describe('openapi to controllers/models', () => {
2323
imports: ["import {Customer} from '../models/customer.model';"],
2424
methods: [
2525
{
26-
description: 'Returna all customers',
26+
description: 'Returns all customers',
2727
decoration: "@operation('get', '/customers')",
2828
signature:
2929
"async ''(@param({name: 'if', in: 'query'}) _if: string[], " +
30-
"@param({name: 'limit', in: 'query'}) limit: number): " +
30+
"@param({name: 'limit', in: 'query'}) limit: number, " +
31+
"@param({name: 'access-token', in: 'query'}) " +
32+
'accessToken: string): ' +
3133
'Promise<Customer[]>',
3234
},
3335
{
3436
description: 'Creates a new customer',
3537
decoration: "@operation('post', '/customers')",
36-
signature: 'async createCustomer(): Promise<Customer>',
38+
signature:
39+
"async createCustomer(@param({name: 'access-token', " +
40+
"in: 'query'}) accessToken: string): Promise<Customer>",
3741
},
3842
{
3943
description: 'Returns a customer based on a single ID',

0 commit comments

Comments
 (0)