Skip to content

Commit

Permalink
feat(cli): normalize variable names for OpenAPI paths
Browse files Browse the repository at this point in the history
Non-word characters in variable names for OpenAPI paths are not allowed
by `@loopback/rest`.
  • Loading branch information
raymondfeng committed Apr 11, 2019
1 parent 6082829 commit b6e02fc
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 8 deletions.
17 changes: 15 additions & 2 deletions packages/cli/generators/openapi/spec-helper.js
Expand Up @@ -203,7 +203,13 @@ function buildMethodSpec(controllerSpec, op, options) {
const pType = mapSchemaType(p.schema, options);
addImportsForType(pType);
comments.push(`@param ${name} ${p.description || ''}`);
return `@param({name: '${p.name}', in: '${p.in}'}) ${name}: ${

// Normalize parameter name to match `\w`
let paramName = p.name;
if (p.in === 'path') {
paramName = paramName.replace(/[^\w]+/g, '_');
}
return `@param({name: '${paramName}', in: '${p.in}'}) ${name}: ${
pType.signature
}`;
});
Expand Down Expand Up @@ -285,10 +291,17 @@ function buildMethodSpec(controllerSpec, op, options) {
returnType.signature
}>`;
comments.unshift(op.spec.description || '', '\n');

// Normalize path variable names to alphanumeric characters including the
// underscore (Equivalent to [A-Za-z0-9_]). Please note `@loopback/rest`
// does not allow other characters that don't match `\w`.
const opPath = op.path.replace(/\{[^\}]+\}/g, varName =>
varName.replace(/[^\w\{\}]+/g, '_'),
);
const methodSpec = {
description: op.spec.description,
comments,
decoration: `@operation('${op.verb}', '${op.path}')`,
decoration: `@operation('${op.verb}', '${opPath}')`,
signature,
};
if (op.spec['x-implementation']) {
Expand Down
4 changes: 2 additions & 2 deletions packages/cli/test/fixtures/openapi/3.0/customer.yaml
Expand Up @@ -69,15 +69,15 @@ paths:
application/json:
schema:
$ref: '#/components/schemas/Customer'
/customers/{id}:
/customers/{customer-id}:
get:
tags:
- Customer
description: Returns a customer based on a single ID
x-implementation: "return {id: id, 'first-name': 'John', last-name: 'Smith'};"
operationId: find customer by id
parameters:
- name: id
- name: customer-id
in: path
description: ID of customer to fetch
required: true
Expand Down
8 changes: 4 additions & 4 deletions packages/cli/test/unit/openapi/controller-spec.unit.js
Expand Up @@ -59,13 +59,13 @@ describe('openapi to controllers/models', () => {
comments: [
'Returns a customer based on a single ID',
'\n',
'@param id ID of customer to fetch',
'@param customerId ID of customer to fetch',
'@returns customer response',
],
decoration: "@operation('get', '/customers/{id}')",
decoration: "@operation('get', '/customers/{customer_id}')",
signature:
"async findCustomerById(@param({name: 'id', in: 'path'}) " +
'id: number): Promise<Customer>',
"async findCustomerById(@param({name: 'customer_id', " +
"in: 'path'}) customerId: number): Promise<Customer>",
implementation:
"return {id: id, 'first-name': 'John', last-name: 'Smith'};",
},
Expand Down

0 comments on commit b6e02fc

Please sign in to comment.