diff --git a/src/DefinitionGenerator.ts b/src/DefinitionGenerator.ts index 13a0630..ba08223 100644 --- a/src/DefinitionGenerator.ts +++ b/src/DefinitionGenerator.ts @@ -76,6 +76,7 @@ export class DefinitionGenerator { // loop through http events for (const httpEvent of this.getHttpEvents(funcConfig.events)) { const httpEventConfig = httpEvent.http; + if (httpEventConfig.documentation) { const documentationConfig = httpEventConfig.documentation; // Build OpenAPI path configuration structure for each method @@ -91,6 +92,7 @@ export class DefinitionGenerator { }, }, }; + // merge path configuration into main configuration merge(this.definition.paths, pathConfig); } @@ -150,7 +152,7 @@ export class DefinitionGenerator { if (type === 'path') { parameterConfig.required = true; } else if (type === 'query') { - parameterConfig.allowEmptyValues = parameter.allowEmptyValue || false; // OpenAPI default is false + parameterConfig.allowEmptyValue = parameter.allowEmptyValue || false; // OpenAPI default is false if ('allowReserved' in parameter) { parameterConfig.allowReserved = parameter.allowReserved || false; @@ -209,12 +211,7 @@ export class DefinitionGenerator { }, }; - // Add examples if any can be found - if (requestModel.examples && Array.isArray(requestModel.examples)) { - merge(reqModelConfig, { examples: clone(requestModel.examples) }); - } else if (requestModel.example) { - merge(reqModelConfig, { example: clone(requestModel.example) }); - } + this.applyExamples(requestModel, reqModelConfig); const reqBodyConfig: { content: object, description?: string } = { content: { @@ -234,6 +231,14 @@ export class DefinitionGenerator { return requestBodies; } + private applyExamples (target, config) { + if (target.examples && Array.isArray(target.examples)) { + merge(config, { examples: clone(target.examples) }); + } else if (target.example) { + merge(config, { example: clone(target.example) }); + } + } + /** * Gets response bodies from documentation config * @param documentationConfig @@ -274,21 +279,21 @@ export class DefinitionGenerator { private getResponseContent (response) { const content = {}; + for (const responseKey of Object.keys(response)) { - const responseModel = this.config.models.filter( - (model) => model.name === response[responseKey], - ).pop(); + const responseModel = this.config.models.find((model) => + model.name === response[responseKey], + ); + if (responseModel) { const resModelConfig = { schema: { $ref: `#/components/schemas/${response[responseKey]}`, }, }; - if (responseModel.examples && Array.isArray(responseModel.examples)) { - merge(resModelConfig, { examples: clone(responseModel.examples) }); - } else if (responseModel.example) { - merge(resModelConfig, { example: clone(responseModel.example) }); - } + + this.applyExamples(responseModel, resModelConfig); + merge(content, { [responseKey] : resModelConfig }); } } diff --git a/src/types.ts b/src/types.ts index 9daa350..22e2c6d 100644 --- a/src/types.ts +++ b/src/types.ts @@ -36,7 +36,7 @@ export interface IParameterConfig { required?: boolean; schema?: object; deprecated?: boolean; - allowEmptyValues?: boolean; + allowEmptyValue?: boolean; style?: 'form' | 'simple'; explode?: boolean; allowReserved?: boolean; diff --git a/test/project/openapi.yml b/test/project/openapi.yml index f3bcdf6..7b1330f 100644 --- a/test/project/openapi.yml +++ b/test/project/openapi.yml @@ -1,56 +1,4 @@ openapi: 3.0.0-RC1 -description: '' -version: 0.0.0 -title: '' -paths: - /create: - post: - operationId: createUser - summary: Create User - description: Creates a user and then sends a generated password email - responses: - '201': - description: A user object along with generated API Keys - content: - application/json: - schema: - $ref: '#/components/schemas/PutDocumentResponse' - '500': - description: An error message when creating a new user - content: - application/json: - schema: - $ref: '#/components/schemas/ErrorResponse' - parameters: - - name: username - in: path - description: The username for a user to create - required: true - schema: - type: string - pattern: '^[-a-z0-9_]+$' - - name: membershipType - in: query - description: The user's Membership Type - required: false - allowEmptyValues: false - schema: - type: string - enum: - - premium - - standard - - name: SessionId - in: cookie - description: A Session ID variable - required: false - schema: - type: string - requestBody: - content: - application/json: - schema: - $ref: '#/components/schemas/PutDocumentRequest' - description: A user information object components: schemas: ErrorResponse: @@ -213,8 +161,56 @@ components: SomeAttribute: type: string securitySchemes: {} -servers: [] info: title: '' description: '' - version: a9cb0cf9-ab05-4a6b-9077-5818f1d1afb5 + version: 442d95b9-dcc5-4e6e-a354-a561b8904c08 +paths: + /create: + post: + operationId: createUser + summary: Create User + description: Creates a user and then sends a generated password email + responses: + '201': + description: A user object along with generated API Keys + content: + application/json: + schema: + $ref: '#/components/schemas/PutDocumentResponse' + '500': + description: An error message when creating a new user + content: + application/json: + schema: + $ref: '#/components/schemas/ErrorResponse' + parameters: + - name: username + in: path + description: The username for a user to create + required: true + schema: + type: string + pattern: '^[-a-z0-9_]+$' + - name: membershipType + in: query + description: The user's Membership Type + required: false + allowEmptyValue: false + schema: + type: string + enum: + - premium + - standard + - name: SessionId + in: cookie + description: A Session ID variable + required: false + schema: + type: string + requestBody: + content: + application/json: + schema: + $ref: '#/components/schemas/PutDocumentRequest' + description: A user information object