Skip to content

Commit daa7f78

Browse files
committed
feat(cli): add comments for generated methods from openapi
1 parent caf66c2 commit daa7f78

File tree

4 files changed

+45
-14
lines changed

4 files changed

+45
-14
lines changed

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

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,7 @@ function getMethodName(opSpec) {
149149
*/
150150
function buildMethodSpec(controllerSpec, op, options) {
151151
const methodName = getMethodName(op.spec);
152+
const comments = [];
152153
let args = [];
153154
const parameters = op.spec.parameters;
154155
// Keep track of param names to avoid duplicates
@@ -163,6 +164,7 @@ function buildMethodSpec(controllerSpec, op, options) {
163164
}
164165
const pType = mapSchemaType(p.schema, options);
165166
addImportsForType(pType);
167+
comments.push(`@param ${name} ${p.description || ''}`);
166168
return `@param({name: '${p.name}', in: '${p.in}'}) ${name}: ${
167169
pType.signature
168170
}`;
@@ -195,6 +197,9 @@ function buildMethodSpec(controllerSpec, op, options) {
195197
args.unshift(
196198
`@requestBody(${bodySpec}) ${bodyParam}: ${bodyType.signature}`,
197199
);
200+
comments.unshift(
201+
`@param ${bodyName} ${op.spec.requestBody.description || ''}`,
202+
);
198203
}
199204
let returnType = {signature: 'any'};
200205
const responses = op.spec.responses;
@@ -212,20 +217,24 @@ function buildMethodSpec(controllerSpec, op, options) {
212217
*/
213218
for (const code in responses) {
214219
if (isExtension(code)) continue;
215-
if (code !== '200') continue;
220+
if (code !== '200' && code !== '201') continue;
216221
const content = responses[code].content;
217222
const jsonType = content && content['application/json'];
218223
if (jsonType && jsonType.schema) {
219224
returnType = mapSchemaType(jsonType.schema, options);
220225
addImportsForType(returnType);
226+
comments.push(`@returns ${responses[code].description || ''}`);
227+
break;
221228
}
222229
}
223230
}
224231
const signature = `async ${methodName}(${args.join(', ')}): Promise<${
225232
returnType.signature
226233
}>`;
234+
comments.unshift(op.spec.description || '', '\n');
227235
const methodSpec = {
228236
description: op.spec.description,
237+
comments,
229238
decoration: `@operation('${op.verb}', '${op.path}')`,
230239
signature,
231240
};

packages/cli/generators/openapi/templates/src/controllers/controller-template.ts.ejs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,9 @@ export class <%- className %> {
1818

1919
<%_ for (const m of methods) { -%>
2020
/**
21-
* <%- m.description %>
21+
<%_ for (const c of m.comments) { -%>
22+
* <%- c %>
23+
<%_ } -%>
2224
*/
2325
<%- m.decoration %>
2426
<%- m.signature %> {

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

Lines changed: 28 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,36 +18,56 @@ describe('openapi to controllers/models', () => {
1818
expect(customerSepc.controllerSpecs).to.eql([
1919
{
2020
tag: 'Customer',
21-
className: 'CustomerController',
2221
description: 'Customer resource',
22+
className: 'CustomerController',
2323
imports: ["import {Customer} from '../models/customer.model';"],
2424
methods: [
2525
{
2626
description: 'Returns all customers',
27+
comments: [
28+
'Returns all customers',
29+
'\n',
30+
'@param _if if condition',
31+
'@param limit maximum number of results to return',
32+
'@param accessToken Access token',
33+
'@returns customer response',
34+
],
2735
decoration: "@operation('get', '/customers')",
2836
signature:
2937
"async getCustomers(@param({name: 'if', in: 'query'}) _if: " +
3038
"string[], @param({name: 'limit', in: 'query'}) limit: number, " +
31-
"@param({name: 'access-token', in: 'query'}) " +
32-
'accessToken: string): ' +
33-
'Promise<Customer[]>',
39+
"@param({name: 'access-token', in: 'query'}) accessToken: " +
40+
'string): Promise<Customer[]>',
3441
},
3542
{
3643
description: 'Creates a new customer',
44+
comments: [
45+
'Creates a new customer',
46+
'\n',
47+
'@param body Customer to add',
48+
'@param accessToken Access token',
49+
'@returns customer response',
50+
],
3751
decoration: "@operation('post', '/customers')",
3852
signature:
3953
'async createCustomer(@requestBody() body: Customer, ' +
40-
"@param({name: 'access-token', " +
41-
"in: 'query'}) accessToken: string): Promise<Customer>",
54+
"@param({name: 'access-token', in: 'query'}) accessToken: " +
55+
'string): Promise<Customer>',
4256
},
4357
{
4458
description: 'Returns a customer based on a single ID',
59+
comments: [
60+
'Returns a customer based on a single ID',
61+
'\n',
62+
'@param id ID of customer to fetch',
63+
'@returns customer response',
64+
],
4565
decoration: "@operation('get', '/customers/{id}')",
46-
implementation:
47-
"return {id: id, 'first-name': 'John', last-name: 'Smith'};",
4866
signature:
4967
"async findCustomerById(@param({name: 'id', in: 'path'}) " +
5068
'id: number): Promise<Customer>',
69+
implementation:
70+
"return {id: id, 'first-name': 'John', last-name: 'Smith'};",
5171
},
5272
],
5373
},

packages/cli/test/unit/openapi/schema-model.unit.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -210,8 +210,8 @@ describe('schema to model', () => {
210210
import: "import {Address} from './address.model';",
211211
kind: 'class',
212212
declaration:
213-
'{\n street?: string;\n city?: string;\n state?: string;\n' +
214-
' zipCode?: string;\n}',
213+
'{\n street?: string;\n city?: string;\n state?: string;\n ' +
214+
'zipCode?: string;\n}',
215215
signature: 'Address',
216216
},
217217
{
@@ -248,8 +248,8 @@ describe('schema to model', () => {
248248
import: "import {Customer} from './customer.model';",
249249
kind: 'class',
250250
declaration:
251-
"{\n id: number;\n 'first-name'?: string;\n " +
252-
"'last-name'?: Name;\n addresses?: Address[];\n}",
251+
"{\n id: number;\n 'first-name'?: string;\n 'last-name'?: " +
252+
'Name;\n addresses?: Address[];\n}',
253253
signature: 'Customer',
254254
},
255255
]);

0 commit comments

Comments
 (0)