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

Commit 5870855

Browse files
author
Phil Sturgeon
committed
Support example and x-example in Swagger Properties
1 parent 24cc71a commit 5870855

File tree

4 files changed

+48
-8
lines changed

4 files changed

+48
-8
lines changed

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1252,6 +1252,12 @@ methods.convertParameterObjectIntoParameter = (parameterEntry) => {
12521252
applicableContexts
12531253
}
12541254

1255+
if (parameter.example) {
1256+
paramInstance.examples = List([parameter.example])
1257+
} else if (parameter['x-example']) {
1258+
paramInstance.examples = List([parameter['x-example']])
1259+
}
1260+
12551261
if (parameter.type === 'array' && parameter.items) {
12561262
const { value } = methods.convertParameterObjectIntoParameter({
12571263
key: null,

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

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2772,7 +2772,8 @@ describe('parsers/swagger/v2.0/Parser.js', () => {
27722772
maximum: 321,
27732773
minimum: 123,
27742774
multipleOf: 5,
2775-
default: 100
2775+
default: 100,
2776+
example: 12345
27762777
}
27772778
}
27782779

@@ -2787,6 +2788,7 @@ describe('parsers/swagger/v2.0/Parser.js', () => {
27872788
required: true,
27882789
type: 'integer',
27892790
default: 100,
2791+
examples: List([12345]),
27902792
constraints: List([
27912793
new Constraint.Maximum(321),
27922794
new Constraint.Minimum(123),
@@ -2816,7 +2818,8 @@ describe('parsers/swagger/v2.0/Parser.js', () => {
28162818
maximum: 321,
28172819
minimum: 123,
28182820
multipleOf: 5,
2819-
default: 100
2821+
default: 100,
2822+
example: 12345
28202823
}
28212824
}
28222825
}
@@ -2838,6 +2841,7 @@ describe('parsers/swagger/v2.0/Parser.js', () => {
28382841
required: true,
28392842
type: 'integer',
28402843
default: 100,
2844+
examples: List([12345]),
28412845
constraints: List([
28422846
new Constraint.Maximum(321),
28432847
new Constraint.Minimum(123),
@@ -2920,6 +2924,35 @@ describe('parsers/swagger/v2.0/Parser.js', () => {
29202924
const actual = inputs.map(input => __internals__.convertParameterObjectIntoParameter(input))
29212925
expect(actual).toEqual(expected)
29222926
})
2927+
2928+
it('should respect x-example too', () => {
2929+
const entry = {
2930+
key: 'UserId',
2931+
value: {
2932+
name: 'userId',
2933+
in: 'query',
2934+
type: 'integer',
2935+
required: true,
2936+
'x-example': 23456
2937+
}
2938+
}
2939+
2940+
const expected = {
2941+
key: 'UserId',
2942+
value: new Parameter({
2943+
key: 'userId',
2944+
name: 'userId',
2945+
in: 'queries',
2946+
uuid: 'UserId',
2947+
required: true,
2948+
type: 'integer',
2949+
examples: List([23456]),
2950+
constraints: List()
2951+
})
2952+
}
2953+
2954+
const actual = __internals__.convertParameterObjectIntoParameter(entry)
2955+
})
29232956
})
29242957

29252958
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
@@ -1347,7 +1347,7 @@
13471347
"key": "petId",
13481348
"name": "petId",
13491349
"description": "ID of pet that needs to be fetched",
1350-
"examples": [],
1350+
"examples": [123456789],
13511351
"type": "integer",
13521352
"format": null,
13531353
"default": null,
@@ -2633,7 +2633,7 @@
26332633
"uuid": "orderId-path",
26342634
"key": "orderId",
26352635
"name": "orderId",
2636-
"description": "ID of pet that needs to be fetched",
2636+
"description": "ID of purchase order that needs to be fetched",
26372637
"examples": [],
26382638
"type": "string",
26392639
"format": null,
@@ -2725,7 +2725,7 @@
27252725
"uuid": "orderId-path",
27262726
"key": "orderId",
27272727
"name": "orderId",
2728-
"description": "ID of pet that needs to be fetched",
2728+
"description": "ID of purchase order that needs to be fetched",
27292729
"examples": [],
27302730
"type": "string",
27312731
"format": null,
@@ -5715,4 +5715,4 @@
57155715
},
57165716
"version": "1.0.0"
57175717
}
5718-
}
5718+
}

0 commit comments

Comments
 (0)