Skip to content

Commit 6d71439

Browse files
committed
fix(cli): escape identifiers with conflicting name as decorators
1 parent 9b49773 commit 6d71439

File tree

5 files changed

+19
-4
lines changed

5 files changed

+19
-4
lines changed

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,7 @@ function buildMethodSpec(controllerSpec, op, options) {
193193
const paramNames = {};
194194
if (parameters) {
195195
args = parameters.map(p => {
196-
const name = escapeIdentifier(p.name);
196+
let name = escapeIdentifier(p.name);
197197
if (name in paramNames) {
198198
name = `${name}${paramNames[name]++}`;
199199
} else {
@@ -234,6 +234,7 @@ function buildMethodSpec(controllerSpec, op, options) {
234234
if (bodyName in paramNames) {
235235
bodyName = `${bodyName}${paramNames[bodyName]++}`;
236236
}
237+
bodyName = escapeIdentifier(bodyName);
237238
if (contentType && contentType.schema) {
238239
registerAnonymousSchema(
239240
[methodName, bodyName],

packages/cli/generators/openapi/utils.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,11 @@ const JS_KEYWORDS = [
128128
'false',
129129
];
130130

131+
/**
132+
* Avoid tslint error - Shadowed name: 'requestBody'
133+
*/
134+
const DECORATOR_NAMES = ['operation', 'param', 'requestBody'];
135+
131136
const SAFE_IDENTIFER = /^[a-zA-Z_$][0-9a-zA-Z_$]*$/;
132137

133138
/**
@@ -148,6 +153,9 @@ function escapeIdentifier(name) {
148153
if (!name.match(SAFE_IDENTIFER)) {
149154
name = _.camelCase(name);
150155
}
156+
if (DECORATOR_NAMES.includes(name)) {
157+
return '_' + name;
158+
}
151159
return name;
152160
}
153161

packages/cli/test/integration/generators/openapi-uspto-with-anonymous.integration.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ describe('openapi-generator specific files', () => {
5555

5656
assert.fileContent(
5757
searchController,
58-
`async performSearch(@requestBody() requestBody: PerformSearchRequestBody, ` +
58+
`async performSearch(@requestBody() _requestBody: PerformSearchRequestBody, ` +
5959
`@param({name: 'version', in: 'path'}) version: string, ` +
6060
`@param({name: 'dataset', in: 'path'}) dataset: string): ` +
6161
`Promise<PerformSearchResponseBody> {`,

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,13 +44,13 @@ describe('openapi to controllers/models', () => {
4444
comments: [
4545
'Creates a new customer',
4646
'\n',
47-
'@param requestBody Customer to add',
47+
'@param _requestBody Customer to add',
4848
'@param accessToken Access token',
4949
'@returns customer response',
5050
],
5151
decoration: "@operation('post', '/customers')",
5252
signature:
53-
'async createCustomer(@requestBody() requestBody: Customer, ' +
53+
'async createCustomer(@requestBody() _requestBody: Customer, ' +
5454
"@param({name: 'access-token', in: 'query'}) accessToken: " +
5555
'string): Promise<Customer>',
5656
},

packages/cli/test/unit/openapi/openapi-utils.unit.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,12 @@ describe('openapi utils', () => {
1111
expect(utils.escapeIdentifier('for')).to.eql('_for');
1212
});
1313

14+
it('escapes an identifier conflicting with decorators for ', () => {
15+
expect(utils.escapeIdentifier('requestBody')).to.eql('_requestBody');
16+
expect(utils.escapeIdentifier('operation')).to.eql('_operation');
17+
expect(utils.escapeIdentifier('param')).to.eql('_param');
18+
});
19+
1420
it('escapes illegal chars for an identifier', () => {
1521
expect(utils.escapeIdentifier('foo bar')).to.eql('fooBar');
1622
expect(utils.escapeIdentifier('foo-bar')).to.eql('fooBar');

0 commit comments

Comments
 (0)