Skip to content

Commit 726a738

Browse files
authored
Merge pull request #3 from temando/rc2
Rc2
2 parents 86c236f + 90025a8 commit 726a738

File tree

3 files changed

+71
-70
lines changed

3 files changed

+71
-70
lines changed

src/DefinitionGenerator.ts

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ export class DefinitionGenerator {
7676
// loop through http events
7777
for (const httpEvent of this.getHttpEvents(funcConfig.events)) {
7878
const httpEventConfig = httpEvent.http;
79+
7980
if (httpEventConfig.documentation) {
8081
const documentationConfig = httpEventConfig.documentation;
8182
// Build OpenAPI path configuration structure for each method
@@ -91,6 +92,7 @@ export class DefinitionGenerator {
9192
},
9293
},
9394
};
95+
9496
// merge path configuration into main configuration
9597
merge(this.definition.paths, pathConfig);
9698
}
@@ -150,7 +152,7 @@ export class DefinitionGenerator {
150152
if (type === 'path') {
151153
parameterConfig.required = true;
152154
} else if (type === 'query') {
153-
parameterConfig.allowEmptyValues = parameter.allowEmptyValue || false; // OpenAPI default is false
155+
parameterConfig.allowEmptyValue = parameter.allowEmptyValue || false; // OpenAPI default is false
154156

155157
if ('allowReserved' in parameter) {
156158
parameterConfig.allowReserved = parameter.allowReserved || false;
@@ -209,12 +211,7 @@ export class DefinitionGenerator {
209211
},
210212
};
211213

212-
// Add examples if any can be found
213-
if (requestModel.examples && Array.isArray(requestModel.examples)) {
214-
merge(reqModelConfig, { examples: clone(requestModel.examples) });
215-
} else if (requestModel.example) {
216-
merge(reqModelConfig, { example: clone(requestModel.example) });
217-
}
214+
this.applyExamples(requestModel, reqModelConfig);
218215

219216
const reqBodyConfig: { content: object, description?: string } = {
220217
content: {
@@ -234,6 +231,14 @@ export class DefinitionGenerator {
234231
return requestBodies;
235232
}
236233

234+
private applyExamples (target, config) {
235+
if (target.examples && Array.isArray(target.examples)) {
236+
merge(config, { examples: clone(target.examples) });
237+
} else if (target.example) {
238+
merge(config, { example: clone(target.example) });
239+
}
240+
}
241+
237242
/**
238243
* Gets response bodies from documentation config
239244
* @param documentationConfig
@@ -274,21 +279,21 @@ export class DefinitionGenerator {
274279

275280
private getResponseContent (response) {
276281
const content = {};
282+
277283
for (const responseKey of Object.keys(response)) {
278-
const responseModel = this.config.models.filter(
279-
(model) => model.name === response[responseKey],
280-
).pop();
284+
const responseModel = this.config.models.find((model) =>
285+
model.name === response[responseKey],
286+
);
287+
281288
if (responseModel) {
282289
const resModelConfig = {
283290
schema: {
284291
$ref: `#/components/schemas/${response[responseKey]}`,
285292
},
286293
};
287-
if (responseModel.examples && Array.isArray(responseModel.examples)) {
288-
merge(resModelConfig, { examples: clone(responseModel.examples) });
289-
} else if (responseModel.example) {
290-
merge(resModelConfig, { example: clone(responseModel.example) });
291-
}
294+
295+
this.applyExamples(responseModel, resModelConfig);
296+
292297
merge(content, { [responseKey] : resModelConfig });
293298
}
294299
}

src/types.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ export interface IParameterConfig {
3636
required?: boolean;
3737
schema?: object;
3838
deprecated?: boolean;
39-
allowEmptyValues?: boolean;
39+
allowEmptyValue?: boolean;
4040
style?: 'form' | 'simple';
4141
explode?: boolean;
4242
allowReserved?: boolean;

test/project/openapi.yml

Lines changed: 50 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -1,56 +1,4 @@
11
openapi: 3.0.0-RC1
2-
description: ''
3-
version: 0.0.0
4-
title: ''
5-
paths:
6-
/create:
7-
post:
8-
operationId: createUser
9-
summary: Create User
10-
description: Creates a user and then sends a generated password email
11-
responses:
12-
'201':
13-
description: A user object along with generated API Keys
14-
content:
15-
application/json:
16-
schema:
17-
$ref: '#/components/schemas/PutDocumentResponse'
18-
'500':
19-
description: An error message when creating a new user
20-
content:
21-
application/json:
22-
schema:
23-
$ref: '#/components/schemas/ErrorResponse'
24-
parameters:
25-
- name: username
26-
in: path
27-
description: The username for a user to create
28-
required: true
29-
schema:
30-
type: string
31-
pattern: '^[-a-z0-9_]+$'
32-
- name: membershipType
33-
in: query
34-
description: The user's Membership Type
35-
required: false
36-
allowEmptyValues: false
37-
schema:
38-
type: string
39-
enum:
40-
- premium
41-
- standard
42-
- name: SessionId
43-
in: cookie
44-
description: A Session ID variable
45-
required: false
46-
schema:
47-
type: string
48-
requestBody:
49-
content:
50-
application/json:
51-
schema:
52-
$ref: '#/components/schemas/PutDocumentRequest'
53-
description: A user information object
542
components:
553
schemas:
564
ErrorResponse:
@@ -213,8 +161,56 @@ components:
213161
SomeAttribute:
214162
type: string
215163
securitySchemes: {}
216-
servers: []
217164
info:
218165
title: ''
219166
description: ''
220-
version: a9cb0cf9-ab05-4a6b-9077-5818f1d1afb5
167+
version: 442d95b9-dcc5-4e6e-a354-a561b8904c08
168+
paths:
169+
/create:
170+
post:
171+
operationId: createUser
172+
summary: Create User
173+
description: Creates a user and then sends a generated password email
174+
responses:
175+
'201':
176+
description: A user object along with generated API Keys
177+
content:
178+
application/json:
179+
schema:
180+
$ref: '#/components/schemas/PutDocumentResponse'
181+
'500':
182+
description: An error message when creating a new user
183+
content:
184+
application/json:
185+
schema:
186+
$ref: '#/components/schemas/ErrorResponse'
187+
parameters:
188+
- name: username
189+
in: path
190+
description: The username for a user to create
191+
required: true
192+
schema:
193+
type: string
194+
pattern: '^[-a-z0-9_]+$'
195+
- name: membershipType
196+
in: query
197+
description: The user's Membership Type
198+
required: false
199+
allowEmptyValue: false
200+
schema:
201+
type: string
202+
enum:
203+
- premium
204+
- standard
205+
- name: SessionId
206+
in: cookie
207+
description: A Session ID variable
208+
required: false
209+
schema:
210+
type: string
211+
requestBody:
212+
content:
213+
application/json:
214+
schema:
215+
$ref: '#/components/schemas/PutDocumentRequest'
216+
description: A user information object

0 commit comments

Comments
 (0)