Skip to content

Commit

Permalink
Merge pull request #3 from temando/rc2
Browse files Browse the repository at this point in the history
Rc2
  • Loading branch information
nfour authored Jul 4, 2017
2 parents 86c236f + 90025a8 commit 726a738
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 70 deletions.
35 changes: 20 additions & 15 deletions src/DefinitionGenerator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -91,6 +92,7 @@ export class DefinitionGenerator {
},
},
};

// merge path configuration into main configuration
merge(this.definition.paths, pathConfig);
}
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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: {
Expand All @@ -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
Expand Down Expand Up @@ -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 });
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ export interface IParameterConfig {
required?: boolean;
schema?: object;
deprecated?: boolean;
allowEmptyValues?: boolean;
allowEmptyValue?: boolean;
style?: 'form' | 'simple';
explode?: boolean;
allowReserved?: boolean;
Expand Down
104 changes: 50 additions & 54 deletions test/project/openapi.yml
Original file line number Diff line number Diff line change
@@ -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:
Expand Down Expand Up @@ -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

0 comments on commit 726a738

Please sign in to comment.