Skip to content
This repository was archived by the owner on Sep 17, 2025. It is now read-only.

Commit 891bbf1

Browse files
author
Phil Sturgeon
committed
Support example and x-example in Swagger Properties
1 parent 6642994 commit 891bbf1

File tree

4 files changed

+50
-8
lines changed

4 files changed

+50
-8
lines changed

src/parsers/swagger/v2.0/Parser.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1235,6 +1235,12 @@ methods.convertParameterObjectIntoParameter = (parameterEntry) => {
12351235
applicableContexts
12361236
}
12371237

1238+
if (parameter.example) {
1239+
paramInstance.examples = List([parameter.example])
1240+
} else if (parameter['x-example']) {
1241+
paramInstance.examples = List([parameter['x-example']])
1242+
}
1243+
12381244
if (parameter.type === 'array' && parameter.items) {
12391245
const { value } = methods.convertParameterObjectIntoParameter({
12401246
key: null,

src/parsers/swagger/v2.0/__tests__/Parser.spec.js

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2239,7 +2239,8 @@ describe('parsers/swagger/v2.0/Parser.js', () => {
22392239
maximum: 321,
22402240
minimum: 123,
22412241
multipleOf: 5,
2242-
default: 100
2242+
default: 100,
2243+
example: 12345
22432244
}
22442245
}
22452246

@@ -2254,6 +2255,7 @@ describe('parsers/swagger/v2.0/Parser.js', () => {
22542255
required: true,
22552256
type: 'integer',
22562257
default: 100,
2258+
examples: List([12345]),
22572259
constraints: List([
22582260
new Constraint.Maximum(321),
22592261
new Constraint.Minimum(123),
@@ -2283,7 +2285,8 @@ describe('parsers/swagger/v2.0/Parser.js', () => {
22832285
maximum: 321,
22842286
minimum: 123,
22852287
multipleOf: 5,
2286-
default: 100
2288+
default: 100,
2289+
example: 12345
22872290
}
22882291
}
22892292
}
@@ -2305,6 +2308,7 @@ describe('parsers/swagger/v2.0/Parser.js', () => {
23052308
required: true,
23062309
type: 'integer',
23072310
default: 100,
2311+
examples: List([12345]),
23082312
constraints: List([
23092313
new Constraint.Maximum(321),
23102314
new Constraint.Minimum(123),
@@ -2318,6 +2322,37 @@ describe('parsers/swagger/v2.0/Parser.js', () => {
23182322

23192323
expect(actual).toEqual(expected)
23202324
})
2325+
2326+
it('should respect x-example too', () => {
2327+
const entry = {
2328+
key: 'UserId',
2329+
value: {
2330+
name: 'userId',
2331+
in: 'query',
2332+
type: 'integer',
2333+
required: true,
2334+
'x-example': 23456
2335+
}
2336+
}
2337+
2338+
const expected = {
2339+
key: 'UserId',
2340+
value: new Parameter({
2341+
key: 'userId',
2342+
name: 'userId',
2343+
in: 'queries',
2344+
uuid: 'UserId',
2345+
required: true,
2346+
type: 'integer',
2347+
examples: List([23456]),
2348+
constraints: List()
2349+
})
2350+
}
2351+
2352+
const actual = __internals__.convertParameterObjectIntoParameter(entry)
2353+
2354+
expect(actual).toEqual(expected)
2355+
})
23212356
})
23222357

23232358
describe('@convertParameterObjectArrayIntoParameterMap', () => {

testing/e2e/swagger2-internal/input.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -236,7 +236,8 @@
236236
"description": "ID of pet that needs to be fetched",
237237
"required": true,
238238
"type": "integer",
239-
"format": "int64"
239+
"format": "int64",
240+
"x-example": 12356789
240241
}
241242
],
242243
"responses": {
@@ -414,7 +415,7 @@
414415
{
415416
"in": "path",
416417
"name": "orderId",
417-
"description": "ID of pet that needs to be fetched",
418+
"description": "ID of purchase order that needs to be fetched",
418419
"required": true,
419420
"type": "string"
420421
}

testing/e2e/swagger2-internal/output.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1345,7 +1345,7 @@
13451345
"key": "petId",
13461346
"name": "petId",
13471347
"description": "ID of pet that needs to be fetched",
1348-
"examples": [],
1348+
"examples": [123456789],
13491349
"type": "integer",
13501350
"format": null,
13511351
"default": null,
@@ -2630,7 +2630,7 @@
26302630
"uuid": "orderId-path",
26312631
"key": "orderId",
26322632
"name": "orderId",
2633-
"description": "ID of pet that needs to be fetched",
2633+
"description": "ID of purchase order that needs to be fetched",
26342634
"examples": [],
26352635
"type": "string",
26362636
"format": null,
@@ -2722,7 +2722,7 @@
27222722
"uuid": "orderId-path",
27232723
"key": "orderId",
27242724
"name": "orderId",
2725-
"description": "ID of pet that needs to be fetched",
2725+
"description": "ID of purchase order that needs to be fetched",
27262726
"examples": [],
27272727
"type": "string",
27282728
"format": null,
@@ -5708,4 +5708,4 @@
57085708
},
57095709
"version": "1.0.0"
57105710
}
5711-
}
5711+
}

0 commit comments

Comments
 (0)