Skip to content

Commit

Permalink
Support example and x-example in Swagger Properties
Browse files Browse the repository at this point in the history
  • Loading branch information
Phil Sturgeon committed Dec 11, 2017
1 parent 6642994 commit 891bbf1
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 8 deletions.
6 changes: 6 additions & 0 deletions src/parsers/swagger/v2.0/Parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -1235,6 +1235,12 @@ methods.convertParameterObjectIntoParameter = (parameterEntry) => {
applicableContexts
}

if (parameter.example) {
paramInstance.examples = List([parameter.example])
} else if (parameter['x-example']) {
paramInstance.examples = List([parameter['x-example']])
}

if (parameter.type === 'array' && parameter.items) {
const { value } = methods.convertParameterObjectIntoParameter({
key: null,
Expand Down
39 changes: 37 additions & 2 deletions src/parsers/swagger/v2.0/__tests__/Parser.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -2239,7 +2239,8 @@ describe('parsers/swagger/v2.0/Parser.js', () => {
maximum: 321,
minimum: 123,
multipleOf: 5,
default: 100
default: 100,
example: 12345
}
}

Expand All @@ -2254,6 +2255,7 @@ describe('parsers/swagger/v2.0/Parser.js', () => {
required: true,
type: 'integer',
default: 100,
examples: List([12345]),
constraints: List([
new Constraint.Maximum(321),
new Constraint.Minimum(123),
Expand Down Expand Up @@ -2283,7 +2285,8 @@ describe('parsers/swagger/v2.0/Parser.js', () => {
maximum: 321,
minimum: 123,
multipleOf: 5,
default: 100
default: 100,
example: 12345
}
}
}
Expand All @@ -2305,6 +2308,7 @@ describe('parsers/swagger/v2.0/Parser.js', () => {
required: true,
type: 'integer',
default: 100,
examples: List([12345]),
constraints: List([
new Constraint.Maximum(321),
new Constraint.Minimum(123),
Expand All @@ -2318,6 +2322,37 @@ describe('parsers/swagger/v2.0/Parser.js', () => {

expect(actual).toEqual(expected)
})

it('should respect x-example too', () => {
const entry = {
key: 'UserId',
value: {
name: 'userId',
in: 'query',
type: 'integer',
required: true,
'x-example': 23456
}
}

const expected = {
key: 'UserId',
value: new Parameter({
key: 'userId',
name: 'userId',
in: 'queries',
uuid: 'UserId',
required: true,
type: 'integer',
examples: List([23456]),
constraints: List()
})
}

const actual = __internals__.convertParameterObjectIntoParameter(entry)

expect(actual).toEqual(expected)
})
})

describe('@convertParameterObjectArrayIntoParameterMap', () => {
Expand Down
5 changes: 3 additions & 2 deletions testing/e2e/swagger2-internal/input.json
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,8 @@
"description": "ID of pet that needs to be fetched",
"required": true,
"type": "integer",
"format": "int64"
"format": "int64",
"x-example": 12356789
}
],
"responses": {
Expand Down Expand Up @@ -414,7 +415,7 @@
{
"in": "path",
"name": "orderId",
"description": "ID of pet that needs to be fetched",
"description": "ID of purchase order that needs to be fetched",
"required": true,
"type": "string"
}
Expand Down
8 changes: 4 additions & 4 deletions testing/e2e/swagger2-internal/output.json
Original file line number Diff line number Diff line change
Expand Up @@ -1345,7 +1345,7 @@
"key": "petId",
"name": "petId",
"description": "ID of pet that needs to be fetched",
"examples": [],
"examples": [123456789],
"type": "integer",
"format": null,
"default": null,
Expand Down Expand Up @@ -2630,7 +2630,7 @@
"uuid": "orderId-path",
"key": "orderId",
"name": "orderId",
"description": "ID of pet that needs to be fetched",
"description": "ID of purchase order that needs to be fetched",
"examples": [],
"type": "string",
"format": null,
Expand Down Expand Up @@ -2722,7 +2722,7 @@
"uuid": "orderId-path",
"key": "orderId",
"name": "orderId",
"description": "ID of pet that needs to be fetched",
"description": "ID of purchase order that needs to be fetched",
"examples": [],
"type": "string",
"format": null,
Expand Down Expand Up @@ -5708,4 +5708,4 @@
},
"version": "1.0.0"
}
}
}

0 comments on commit 891bbf1

Please sign in to comment.