Skip to content
This repository was archived by the owner on Dec 10, 2021. It is now read-only.

Commit 541e019

Browse files
author
Lance Cooper
authored
Ensure requestBody and parameters are being generated forthe openapi doc (#6)
1 parent a85bd82 commit 541e019

File tree

2 files changed

+67
-11
lines changed

2 files changed

+67
-11
lines changed

src/DefinitionGenerator.ts

+2-4
Original file line numberDiff line numberDiff line change
@@ -174,13 +174,11 @@ export class DefinitionGenerator {
174174
operationObj.deprecated = true;
175175
}
176176

177-
if (operationObj.requestBody) {
177+
if (documentationConfig.requestBody) {
178178
operationObj.requestBody = this.getRequestBodiesFromConfig(documentationConfig);
179179
}
180180

181-
if (operationObj.parameters) {
182-
operationObj.parameters = this.getParametersFromConfig(documentationConfig);
183-
}
181+
operationObj.parameters = this.getParametersFromConfig(documentationConfig);
184182

185183
operationObj.responses = this.getResponsesFromConfig(documentationConfig);
186184

+65-7
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import * as path from 'path';
22
import * as Serverless from 'serverless';
33
import { DefinitionGenerator } from '../DefinitionGenerator';
4+
import { merge } from '../utils';
45

56
class ServerlessInterface extends Serverless {
67
public service: any = {};
@@ -11,10 +12,12 @@ class ServerlessInterface extends Serverless {
1112
}
1213

1314
describe('OpenAPI Documentation Generator', () => {
14-
it('Generates OpenAPI document', async () => {
15+
let sls: ServerlessInterface;
16+
17+
beforeEach(async () => {
1518
const servicePath = path.join(__dirname, '../../test/project');
1619
const serverlessYamlPath = path.join(servicePath, './serverless.yml');
17-
const sls: ServerlessInterface = new Serverless();
20+
sls = new Serverless();
1821

1922
sls.config.update({
2023
servicePath,
@@ -26,12 +29,67 @@ describe('OpenAPI Documentation Generator', () => {
2629
await sls.service.load(config);
2730
await sls.variables.populateService();
2831

29-
if ('documentation' in sls.service.custom) {
30-
const docGen = new DefinitionGenerator(sls.service.custom.documentation);
31-
32-
expect(docGen).not.toBeNull();
33-
} else {
32+
if (!('documentation' in sls.service.custom)) {
3433
throw new Error('Cannot find "documentation" in custom section of "serverless.yml"');
3534
}
3635
});
36+
37+
it('Generates OpenAPI document', async () => {
38+
const docGen = new DefinitionGenerator(sls.service.custom.documentation);
39+
expect(docGen).not.toBeNull();
40+
});
41+
42+
it('adds paths to OpenAPI output from function configuration', async () => {
43+
const docGen = new DefinitionGenerator(sls.service.custom.documentation);
44+
45+
// implementation copied from ServerlessOpenApiDocumentation.ts
46+
docGen.parse();
47+
48+
const funcConfigs = sls.service.getAllFunctions().map((functionName) => {
49+
const func = sls.service.getFunction(functionName);
50+
return merge({ _functionName: functionName }, func);
51+
});
52+
53+
docGen.readFunctions(funcConfigs);
54+
55+
// get the parameters from the `/create POST' endpoint
56+
const actual = docGen.definition.paths['/create'].post.parameters;
57+
const expected = [
58+
{
59+
description: 'The username for a user to create',
60+
in: 'path',
61+
name: 'username',
62+
required: true,
63+
schema: {
64+
pattern: '^[-a-z0-9_]+$',
65+
type: 'string',
66+
},
67+
},
68+
{
69+
allowEmptyValue: false,
70+
description: `The user's Membership Type`,
71+
in: 'query',
72+
name: 'membershipType',
73+
required: false,
74+
schema: {
75+
enum: [
76+
'premium',
77+
'standard',
78+
],
79+
type: 'string',
80+
},
81+
},
82+
{
83+
description: 'A Session ID variable',
84+
in: 'cookie',
85+
name: 'SessionId',
86+
required: false,
87+
schema: {
88+
type: 'string',
89+
},
90+
},
91+
];
92+
93+
expect(actual).toEqual(expected);
94+
});
3795
});

0 commit comments

Comments
 (0)