Skip to content

Commit 6bb435b

Browse files
committed
avoid using example.value
use the whole object for example instead of example.value
1 parent 8c93366 commit 6bb435b

File tree

4 files changed

+102
-49
lines changed

4 files changed

+102
-49
lines changed

lib/schemaUtils.js

Lines changed: 0 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1360,30 +1360,6 @@ module.exports = {
13601360
return example;
13611361
},
13621362

1363-
/**
1364-
* Identifies if the given schema has the property "value"
1365-
* @param {*} schema bodyObject (media type) schema to use
1366-
* @param {object} components - components defined in the OAS spec.
1367-
* @param {object} options - a standard list of options that's globally passed around. Check options.js for more.
1368-
* @returns {boolean} Wheter to use the property value of the example as the value of the
1369-
* body data
1370-
*/
1371-
schemaHasValueProp(schema, components, options) {
1372-
let schemaHasValue = false,
1373-
schemaObject;
1374-
if (!schema) {
1375-
return false;
1376-
}
1377-
if (schema.$ref) {
1378-
schemaObject = this.getRefObject(schema.$ref, components, options);
1379-
}
1380-
else {
1381-
schemaObject = schema;
1382-
}
1383-
schemaHasValue = schemaObject.properties.hasOwnProperty('value');
1384-
return schemaHasValue;
1385-
},
1386-
13871363
/**
13881364
* converts one of the eamples or schema in Media Type object to postman data
13891365
* @param {*} bodyObj is MediaTypeObject
@@ -1433,11 +1409,6 @@ module.exports = {
14331409
}
14341410
}
14351411
bodyData = bodyObj.example;
1436-
// return example value if present else example is returned
1437-
if (bodyData.hasOwnProperty('value') &&
1438-
!this.schemaHasValueProp(bodyObj.schema, components, options)) {
1439-
bodyData = bodyData.value;
1440-
}
14411412
}
14421413
else if (!_.isEmpty(bodyObj.examples) && (resolveTo === 'example' || !bodyObj.schema)) {
14431414
// take one of the examples as the body and not all
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"

test/unit/base.test.js

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,8 @@ describe('CONVERT FUNCTION TESTS ', function() {
4747
parameterExamples = path.join(__dirname, VALID_OPENAPI_PATH + '/parameteres_with_examples.yaml'),
4848
issue10229 = path.join(__dirname, VALID_OPENAPI_PATH, '/issue#10229.json'),
4949
deepObjectLengthProperty = path.join(__dirname, VALID_OPENAPI_PATH, '/deepObjectLengthProperty.yaml'),
50-
valuePropInExample = path.join(__dirname, VALID_OPENAPI_PATH, '/valuePropInExample.yaml');
50+
valuePropInExample = path.join(__dirname, VALID_OPENAPI_PATH, '/valuePropInExample.yaml'),
51+
petstoreParamExample = path.join(__dirname, VALID_OPENAPI_PATH, '/petstoreParamExample.yaml');
5152

5253

5354
it('Should add collection level auth with type as `bearer`' +
@@ -468,7 +469,7 @@ describe('CONVERT FUNCTION TESTS ', function() {
468469
expect(rootRequest.body.raw).to
469470
.equal('{\n "a": "<string>",\n "b": "<string>"\n}');
470471
expect(exampleRequest.body.raw).to
471-
.equal('{\n "a": "example-b",\n "b": "example-c"\n}');
472+
.equal('{\n "value": {\n "a": "example-b",\n "b": "example-c"\n }\n}');
472473
done();
473474
});
474475
});
@@ -1125,6 +1126,19 @@ describe('CONVERT FUNCTION TESTS ', function() {
11251126
done();
11261127
});
11271128
});
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+
});
11281142
});
11291143

11301144
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)