Skip to content

Commit 11ba3b7

Browse files
authored
Merge pull request #106 from JaredCE/correctly-deal-with-examples
Correctly deal with examples
2 parents a21af36 + 7700756 commit 11ba3b7

File tree

5 files changed

+45
-9
lines changed

5 files changed

+45
-9
lines changed

README.md

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -295,12 +295,22 @@ custom:
295295
contentType: "application/json"
296296
schema:
297297
$schema: "http://json-schema.org/draft-04/schema#"
298+
type: object
298299
properties:
299300
SomeObject:
300301
type: "object"
301302
properties:
302303
SomeAttribute:
303304
type: "string"
305+
examples:
306+
- name: someObjectInlineExample
307+
summary: an example of a request
308+
description: a longer string than the summary
309+
value: {SomeObject: {SomeAttribute: 'attribute'}}
310+
- name: someObjectExternalExample
311+
summary: an example of a request external
312+
description: a longer string than the summary
313+
externalValue: https://example.com/external.json
304314
```
305315

306316
The Second way of writing the models:
@@ -329,12 +339,22 @@ custom:
329339
application/json:
330340
schema:
331341
$schema: "http://json-schema.org/draft-04/schema#"
342+
type: object
332343
properties:
333344
SomeObject:
334345
type: "object"
335346
properties:
336347
SomeAttribute:
337348
type: "string"
349+
examples:
350+
- name: someObjectInlineExample
351+
summary: an example of a request
352+
description: a longer string than the summary
353+
value: {SomeObject: {SomeAttribute: 'attribute'}}
354+
- name: someObjectExternalExample
355+
summary: an example of a request external
356+
description: a longer string than the summary
357+
externalValue: https://example.com/external.json
338358
```
339359

340360
##### Model re-use

package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "serverless-openapi-documenter",
3-
"version": "0.0.51",
3+
"version": "0.0.52",
44
"description": "Generate OpenAPI v3 documentation and Postman Collections from your Serverless Config",
55
"main": "index.js",
66
"keywords": [

src/definitionGenerator.js

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -458,16 +458,22 @@ class DefinitionGenerator {
458458
}
459459
const obj = {}
460460

461-
if (mediaTypeDocumentation.example)
462-
obj.example = mediaTypeDocumentation.example
463-
464-
if (mediaTypeDocumentation.examples)
465-
obj.examples = this.createExamples(mediaTypeDocumentation.examples)
466-
467461
let schema
468462
if (mediaTypeDocumentation?.content) {
463+
if (mediaTypeDocumentation.content[contentKey]?.example)
464+
obj.example = mediaTypeDocumentation.content[contentKey].example
465+
466+
if (mediaTypeDocumentation.content[contentKey]?.examples)
467+
obj.examples = this.createExamples(mediaTypeDocumentation.content[contentKey].examples)
468+
469469
schema = mediaTypeDocumentation.content[contentKey].schema
470470
} else if (mediaTypeDocumentation?.contentType && mediaTypeDocumentation.schema) {
471+
if (mediaTypeDocumentation?.example)
472+
obj.example = mediaTypeDocumentation.example
473+
474+
if (mediaTypeDocumentation?.examples)
475+
obj.examples = this.createExamples(mediaTypeDocumentation.examples)
476+
471477
schema = mediaTypeDocumentation.schema
472478
}
473479

@@ -748,6 +754,7 @@ class DefinitionGenerator {
748754

749755
for(const example of examples) {
750756
Object.assign(examplesObj, {[example.name]: example})
757+
delete examplesObj[example.name].name
751758
}
752759

753760
return examplesObj;

test/serverless-tests/serverless 3/serverless.yml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,3 +95,12 @@ custom:
9595
properties:
9696
SomeAttribute:
9797
type: string
98+
examples:
99+
- name: someObjectInlineExample
100+
summary: an example of a request
101+
description: a longer string than the summary
102+
value: {SomeObject: {SomeAttribute: 'attribute'}}
103+
- name: someObjectExternalExample
104+
summary: an example of a request external
105+
description: a longer string than the summary
106+
externalValue: https://example.com/external.json

0 commit comments

Comments
 (0)