Skip to content

Commit 652c28e

Browse files
authored
Merge pull request #503 from postmanlabs/fix/valuePropInExample
Fix/value prop in example
2 parents a22f7d0 + 6bb435b commit 652c28e

File tree

6 files changed

+250
-25
lines changed

6 files changed

+250
-25
lines changed

lib/schemaUtils.js

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1409,10 +1409,6 @@ module.exports = {
14091409
}
14101410
}
14111411
bodyData = bodyObj.example;
1412-
// return example value if present else example is returned
1413-
if (bodyData.hasOwnProperty('value')) {
1414-
bodyData = bodyData.value;
1415-
}
14161412
}
14171413
else if (!_.isEmpty(bodyObj.examples) && (resolveTo === 'example' || !bodyObj.schema)) {
14181414
// take one of the examples as the body and not all

test/data/valid_openapi/deepObjectLengthProperty.yaml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ paths:
77
get:
88
operationId: someRequest
99
parameters:
10-
- $ref: '#/components/parameters/DeepObjectNestedList'
1110
- $ref: '#/components/parameters/DeepObjectLengthParameter'
1211
responses:
1312
'200':
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
openapi: "3.0.0"
2+
info:
3+
version: 1.0.0
4+
title: Swagger Petstore
5+
license:
6+
name: MIT
7+
servers:
8+
- url: http://petstore.swagger.io/v1
9+
paths:
10+
/pets/{petId}:
11+
get:
12+
summary: Info for a specific pet
13+
operationId: showPetById
14+
tags:
15+
- pets
16+
parameters:
17+
- name: petId
18+
in: path
19+
required: true
20+
description: The id of the pet to retrieve
21+
schema:
22+
type: object
23+
properties:
24+
value:
25+
type:
26+
- integer
27+
example:
28+
value: 1
29+
- name: user
30+
in: query
31+
required: true
32+
style: deepObject
33+
schema:
34+
type:
35+
- object
36+
properties:
37+
id:
38+
type:
39+
- integer
40+
value:
41+
type:
42+
- string
43+
example:
44+
id: 1
45+
value: Vani
46+
responses:
47+
'200':
48+
description: Expected response to a valid request
49+
content:
50+
application/json:
51+
schema:
52+
$ref: "#/components/schemas/Pets"
53+
components:
54+
schemas:
55+
Pet:
56+
required:
57+
- id
58+
- name
59+
properties:
60+
id:
61+
type: integer
62+
format: int64
63+
name:
64+
type: string
65+
tag:
66+
type: string
67+
Pets:
68+
type: array
69+
items:
70+
$ref: "#/components/schemas/Pet"
Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
1+
openapi: 3.0.0
2+
info:
3+
version: '1.1.1'
4+
title: 'ExampleYaml'
5+
license:
6+
name: MIT
7+
servers:
8+
- url: 'localhost:3000'
9+
paths:
10+
/user:
11+
get:
12+
summary: 'Sample endpoint: Returns details about a particular user'
13+
operationId: listUser
14+
tags:
15+
- user
16+
parameters:
17+
- name: id
18+
in: query
19+
description: ID of the user
20+
required: true
21+
schema:
22+
type: integer
23+
format: int32
24+
responses:
25+
'200':
26+
description: 'Sample response: Details about a user by ID'
27+
headers:
28+
x-next:
29+
description: A link to the next page of responses
30+
schema:
31+
type: string
32+
content:
33+
application/json:
34+
schema:
35+
$ref: '#/components/schemas/User'
36+
example:
37+
id: "5789-6378-6372-6372"
38+
name: Vani
39+
tag: true
40+
value: QA
41+
lastmodifieddate: "2022-03-30T07:01:46"
42+
lastmodifiedBy: VM
43+
default:
44+
description: Unexpected error
45+
content:
46+
application/json:
47+
schema:
48+
$ref: '#/components/schemas/Error'
49+
/pet:
50+
get:
51+
summary: 'Sample endpoint: Returns details about a particular pet'
52+
operationId: listPet
53+
tags:
54+
- pet
55+
parameters:
56+
- name: id
57+
in: query
58+
description: ID of the pet
59+
required: true
60+
schema:
61+
type: integer
62+
format: int32
63+
responses:
64+
'200':
65+
description: 'Sample response: Details about a pet by ID'
66+
headers:
67+
x-next:
68+
description: A link to the next page of responses
69+
schema:
70+
type: string
71+
content:
72+
application/json:
73+
schema:
74+
type: object
75+
properties:
76+
id:
77+
type: integer
78+
format: int64
79+
value:
80+
type: string
81+
example:
82+
id: "5789-6378-6372-6372"
83+
value: QA
84+
default:
85+
description: Unexpected error
86+
content:
87+
application/json:
88+
schema:
89+
$ref: '#/components/schemas/Error'
90+
components:
91+
schemas:
92+
User:
93+
type: object
94+
required:
95+
- id
96+
- name
97+
- tag
98+
- value
99+
- lastmodifieddate
100+
- lastmodifiedBy
101+
properties:
102+
id:
103+
type: integer
104+
format: int64
105+
name:
106+
type: string
107+
tag:
108+
type: string
109+
value:
110+
type: string
111+
lastmodifieddate:
112+
type: integer
113+
format: int64
114+
lastmodifiedBy:
115+
type: string
116+
Error:
117+
type: object
118+
required:
119+
- code
120+
- message
121+
properties:
122+
code:
123+
type: integer
124+
format: int32
125+
message:
126+
type: string
127+
securitySchemes:
128+
BasicAuth:
129+
type: http
130+
scheme: basic
131+
security:
132+
- BasicAuth: []

test/unit/base.test.js

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,9 @@ describe('CONVERT FUNCTION TESTS ', function() {
4646
rootUrlServerWithVariables = path.join(__dirname, VALID_OPENAPI_PATH + '/root_url_server_with_variables.json'),
4747
parameterExamples = path.join(__dirname, VALID_OPENAPI_PATH + '/parameteres_with_examples.yaml'),
4848
issue10229 = path.join(__dirname, VALID_OPENAPI_PATH, '/issue#10229.json'),
49-
deepObjectLengthProperty = path.join(__dirname, VALID_OPENAPI_PATH, '/deepObjectLengthProperty.yaml');
49+
deepObjectLengthProperty = path.join(__dirname, VALID_OPENAPI_PATH, '/deepObjectLengthProperty.yaml'),
50+
valuePropInExample = path.join(__dirname, VALID_OPENAPI_PATH, '/valuePropInExample.yaml'),
51+
petstoreParamExample = path.join(__dirname, VALID_OPENAPI_PATH, '/petstoreParamExample.yaml');
5052

5153

5254
it('Should add collection level auth with type as `bearer`' +
@@ -467,7 +469,7 @@ describe('CONVERT FUNCTION TESTS ', function() {
467469
expect(rootRequest.body.raw).to
468470
.equal('{\n "a": "<string>",\n "b": "<string>"\n}');
469471
expect(exampleRequest.body.raw).to
470-
.equal('{\n "a": "example-b",\n "b": "example-c"\n}');
472+
.equal('{\n "value": {\n "a": "example-b",\n "b": "example-c"\n }\n}');
471473
done();
472474
});
473475
});
@@ -1109,6 +1111,34 @@ describe('CONVERT FUNCTION TESTS ', function() {
11091111
done();
11101112
});
11111113
});
1114+
1115+
it('Should convert value property in example' +
1116+
valuePropInExample, function(done) {
1117+
var openapi = fs.readFileSync(valuePropInExample, 'utf8');
1118+
Converter.convert({ type: 'string', data: openapi },
1119+
{ schemaFaker: true, requestParametersResolution: 'Example' }, (err, conversionResult) => {
1120+
expect(err).to.be.null;
1121+
expect(conversionResult.result).to.equal(true);
1122+
expect(conversionResult.output[0].data.item[0].response[0]
1123+
.body).to.include('"value": "QA"');
1124+
expect(conversionResult.output[0].data.item[1].response[0]
1125+
.body).to.include('"value": "QA"');
1126+
done();
1127+
});
1128+
});
1129+
1130+
it('Should convert example in parameters' +
1131+
petstoreParamExample, function(done) {
1132+
var openapi = fs.readFileSync(petstoreParamExample, 'utf8');
1133+
Converter.convert({ type: 'string', data: openapi },
1134+
{ schemaFaker: true, requestParametersResolution: 'Example' }, (err, conversionResult) => {
1135+
expect(err).to.be.null;
1136+
expect(conversionResult.result).to.equal(true);
1137+
expect(conversionResult.output[0].data.item[0].request.url.variable[0].value).to.equal('value,1');
1138+
expect(conversionResult.output[0].data.item[0].request.url.query[1].key).to.equal('user[value]');
1139+
done();
1140+
});
1141+
});
11121142
});
11131143

11141144
describe('requestNameSource option', function() {

test/unit/util.test.js

Lines changed: 16 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -690,14 +690,23 @@ describe('SCHEMA UTILITY FUNCTION TESTS ', function () {
690690
});
691691

692692
it('should work for example', function() {
693+
var bodyWithExample = {
694+
example: 'This is a sample value'
695+
},
696+
retValExample = SchemaUtils.convertToPmBodyData(bodyWithExample, 'application/json');
697+
698+
expect(retValExample).to.equal('This is a sample value');
699+
});
700+
701+
it('should work for example with value property', function() {
693702
var bodyWithExample = {
694703
example: {
695704
value: 'This is a sample value'
696705
}
697706
},
698707
retValExample = SchemaUtils.convertToPmBodyData(bodyWithExample, 'application/json');
699708

700-
expect(retValExample).to.equal('This is a sample value');
709+
expect(retValExample.value).to.equal('This is a sample value');
701710
});
702711

703712
it('should work for examples', function() {
@@ -1678,10 +1687,7 @@ describe('SCHEMA UTILITY FUNCTION TESTS ', function () {
16781687
description: 'body description',
16791688
content: {
16801689
'text/plain': {
1681-
example: {
1682-
summary: 'A list containing two items',
1683-
value: 'text/plain description'
1684-
}
1690+
example: 'text/plain description'
16851691
}
16861692
}
16871693
},
@@ -1690,7 +1696,8 @@ describe('SCHEMA UTILITY FUNCTION TESTS ', function () {
16901696
exampleParametersResolution: 'example'
16911697
});
16921698
resultBody = result.body.raw;
1693-
expect(resultBody).to.equal('"text/plain description"');
1699+
expect(resultBody)
1700+
.to.equal('"text/plain description"');
16941701
expect(result.contentHeader).to.deep.include(
16951702
{ key: 'Content-Type', value: 'text/plain' });
16961703
done();
@@ -1701,10 +1708,7 @@ describe('SCHEMA UTILITY FUNCTION TESTS ', function () {
17011708
description: 'body description',
17021709
content: {
17031710
'text/html': {
1704-
example: {
1705-
summary: 'A list containing two items',
1706-
value: '<html><body><ul><li>item 1</li><li>item 2</li></ul></body></html>'
1707-
}
1711+
example: '<html><body><ul><li>item 1</li><li>item 2</li></ul></body></html>'
17081712
}
17091713
}
17101714
},
@@ -1857,10 +1861,7 @@ describe('SCHEMA UTILITY FUNCTION TESTS ', function () {
18571861
description: 'body description',
18581862
content: {
18591863
'text/plain': {
1860-
example: {
1861-
summary: 'A list containing two items',
1862-
value: 'text/plain description'
1863-
}
1864+
example: 'text/plain description'
18641865
}
18651866
}
18661867
},
@@ -1877,10 +1878,7 @@ describe('SCHEMA UTILITY FUNCTION TESTS ', function () {
18771878
description: 'body description',
18781879
content: {
18791880
'text/html': {
1880-
example: {
1881-
summary: 'A list containing two items',
1882-
value: '<html><body><ul><li>item 1</li><li>item 2</li></ul></body></html>'
1883-
}
1881+
example: '<html><body><ul><li>item 1</li><li>item 2</li></ul></body></html>'
18841882
}
18851883
}
18861884
},

0 commit comments

Comments
 (0)