Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 5 additions & 11 deletions packages/cli/generators/openapi/spec-helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -213,13 +213,16 @@ function buildMethodSpec(controllerSpec, op, options) {
let args = [];
let interfaceArgs = [];
const namedParameters = [];
// The controller's own argument names, which its implementation passes on
const implementationArgs = [];
const parameters = op.spec.parameters;
// Keep track of param names to avoid duplicates
const paramNames = {};
const interfaceParamNames = {};
if (parameters) {
args = parameters.map(p => {
const {paramType, argName} = buildParameter(p, paramNames, comments);
implementationArgs.push(argName);
const paramJson = printSpecObject(p);
const optionalType = !p.required ? ' | undefined' : '';
return `@param(${paramJson}) ${argName}: ${paramType.signature}${optionalType}`;
Expand Down Expand Up @@ -268,6 +271,7 @@ function buildMethodSpec(controllerSpec, op, options) {
args.push(
`@requestBody(${bodySpecJson}) ${bodyParam}: ${bodyType.signature}`,
);
implementationArgs.push(bodyParam);
interfaceArgs.push(`${bodyParam}: ${bodyType.signature}`);
namedParameters.push({
paramName: 'requestBody',
Expand Down Expand Up @@ -350,22 +354,12 @@ function buildMethodSpec(controllerSpec, op, options) {
methodSpec.implementation = op.spec['x-implementation'];
}
if (!methodSpec.implementation) {
const methodParameters = {};
methodParameters[methodName] = [];
if (parameters) {
parameters.forEach(param => {
methodParameters[methodName].push(param.name);
});
}
if (op.spec.requestBody) {
methodParameters[methodName].push('_requestBody');
}
controllerSpec.serviceClassNameCamelCase = camelCase(
controllerSpec.serviceClassName,
);
methodSpec.implementation = `return this.${
controllerSpec.serviceClassNameCamelCase
}.${methodName}(${methodParameters[methodName].join(', ')});`;
}.${methodName}(${implementationArgs.join(', ')});`;
}
return methodSpec;

Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

53 changes: 51 additions & 2 deletions packages/cli/test/unit/openapi/spec-helper.unit.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
const expect = require('@loopback/testlab').expect;
const getMethodName =
require('../../../generators/openapi/spec-helper').getMethodName;
const {
getMethodName,
generateControllerSpecs,
} = require('../../../generators/openapi/spec-helper');

describe('Spec Helpers', () => {
describe('getMethodName', () => {
Expand Down Expand Up @@ -45,4 +47,51 @@ describe('Spec Helpers', () => {
);
});
});

describe('generateControllerSpecs', () => {
function implementationOf(operation) {
const apiSpec = {
openapi: '3.0.0',
info: {title: 'Pet API', version: '1.0.0'},
paths: {'/pets': {post: {operationId: 'getPet', ...operation}}},
};
const [controller] = generateControllerSpecs(apiSpec, {
objectTypeMapping: new Map(),
schemaMapping: {},
});
return controller.methods[0].implementation;
}

it('passes a hyphenated parameter by the name the method declares', () => {
const implementation = implementationOf({
parameters: [
{
name: 'pet-id',
in: 'query',
required: true,
schema: {type: 'string'},
},
],
responses: {200: {description: 'ok'}},
});

expect(implementation).to.equal(
'return this.openApiService.getPet(petId);',
);
});

it('passes the request body by the name the method declares', () => {
const implementation = implementationOf({
parameters: [{name: 'limit', in: 'query', schema: {type: 'number'}}],
requestBody: {
content: {'application/json': {schema: {type: 'string'}}},
},
responses: {200: {description: 'ok'}},
});

expect(implementation).to.equal(
'return this.openApiService.getPet(limit, _requestBody);',
);
});
});
});