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

Ensure requestBody and parameters are being generated forthe openapi doc #6

Merged
merged 5 commits into from
May 6, 2019
Merged
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
6 changes: 2 additions & 4 deletions src/DefinitionGenerator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
72 changes: 65 additions & 7 deletions src/__tests__/DefinitionGenerator.spec.ts
Original file line number Diff line number Diff line change
@@ -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 = {};
Expand All @@ -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,
Expand All @@ -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);
});
});