diff --git a/src/DefinitionGenerator.ts b/src/DefinitionGenerator.ts index 5aef836..48eebc8 100644 --- a/src/DefinitionGenerator.ts +++ b/src/DefinitionGenerator.ts @@ -174,13 +174,11 @@ export class DefinitionGenerator { operationObj.deprecated = true; } - if (operationObj.requestBody) { + if (documentationConfig.requestBody) { operationObj.requestBody = this.getRequestBodiesFromConfig(documentationConfig); } - if (operationObj.parameters) { - operationObj.parameters = this.getParametersFromConfig(documentationConfig); - } + operationObj.parameters = this.getParametersFromConfig(documentationConfig); operationObj.responses = this.getResponsesFromConfig(documentationConfig); diff --git a/src/__tests__/DefinitionGenerator.spec.ts b/src/__tests__/DefinitionGenerator.spec.ts index 2004ae6..9490410 100644 --- a/src/__tests__/DefinitionGenerator.spec.ts +++ b/src/__tests__/DefinitionGenerator.spec.ts @@ -1,6 +1,7 @@ import * as path from 'path'; import * as Serverless from 'serverless'; import { DefinitionGenerator } from '../DefinitionGenerator'; +import { merge } from '../utils'; class ServerlessInterface extends Serverless { public service: any = {}; @@ -11,10 +12,12 @@ class ServerlessInterface extends Serverless { } describe('OpenAPI Documentation Generator', () => { - it('Generates OpenAPI document', async () => { + let sls: ServerlessInterface; + + beforeEach(async () => { const servicePath = path.join(__dirname, '../../test/project'); const serverlessYamlPath = path.join(servicePath, './serverless.yml'); - const sls: ServerlessInterface = new Serverless(); + sls = new Serverless(); sls.config.update({ servicePath, @@ -26,12 +29,67 @@ describe('OpenAPI Documentation Generator', () => { await sls.service.load(config); await sls.variables.populateService(); - if ('documentation' in sls.service.custom) { - const docGen = new DefinitionGenerator(sls.service.custom.documentation); - - expect(docGen).not.toBeNull(); - } else { + if (!('documentation' in sls.service.custom)) { throw new Error('Cannot find "documentation" in custom section of "serverless.yml"'); } }); + + it('Generates OpenAPI document', async () => { + const docGen = new DefinitionGenerator(sls.service.custom.documentation); + expect(docGen).not.toBeNull(); + }); + + it('adds paths to OpenAPI output from function configuration', async () => { + const docGen = new DefinitionGenerator(sls.service.custom.documentation); + + // implementation copied from ServerlessOpenApiDocumentation.ts + docGen.parse(); + + const funcConfigs = sls.service.getAllFunctions().map((functionName) => { + const func = sls.service.getFunction(functionName); + return merge({ _functionName: functionName }, func); + }); + + docGen.readFunctions(funcConfigs); + + // get the parameters from the `/create POST' endpoint + const actual = docGen.definition.paths['/create'].post.parameters; + const expected = [ + { + description: 'The username for a user to create', + in: 'path', + name: 'username', + required: true, + schema: { + pattern: '^[-a-z0-9_]+$', + type: 'string', + }, + }, + { + allowEmptyValue: false, + description: `The user's Membership Type`, + in: 'query', + name: 'membershipType', + required: false, + schema: { + enum: [ + 'premium', + 'standard', + ], + type: 'string', + }, + }, + { + description: 'A Session ID variable', + in: 'cookie', + name: 'SessionId', + required: false, + schema: { + type: 'string', + }, + }, + ]; + + expect(actual).toEqual(expected); + }); });