Skip to content

Commit 569a2c2

Browse files
authored
Merge pull request #658 from postmanlabs/fix/issue#11519
Fixing issue#11519 : imported OpenAPI yaml is missing properties
2 parents 3d2c1ca + c79a76f commit 569a2c2

File tree

4 files changed

+204
-6
lines changed

4 files changed

+204
-6
lines changed

lib/deref.js

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -247,16 +247,20 @@ module.exports = {
247247
}
248248
return { value: 'reference ' + schema.$ref + ' not found in the OpenAPI spec' };
249249
}
250-
if (concreteUtils.compareTypes(schema.type, SCHEMA_TYPES.object) || schema.hasOwnProperty('properties') ||
251-
schema.hasOwnProperty('additionalProperties')) {
250+
251+
if (
252+
concreteUtils.compareTypes(schema.type, SCHEMA_TYPES.object) ||
253+
schema.hasOwnProperty('properties') ||
254+
(schema.hasOwnProperty('additionalProperties') && !schema.hasOwnProperty('type'))
255+
) {
252256
// go through all props
253257
schema.type = SCHEMA_TYPES.object;
258+
254259
if (_.has(schema, 'properties') || _.has(schema, 'additionalProperties')) {
255-
// shallow cloning schema object except properties object
256260
let tempSchema = _.omit(schema, ['properties', 'additionalProperties']);
261+
// shallow cloning schema object except properties object
257262

258263
if (_.has(schema, 'additionalProperties')) {
259-
// don't resolve boolean values
260264
if (_.isBoolean(schema.additionalProperties)) {
261265
tempSchema.additionalProperties = schema.additionalProperties;
262266
}
@@ -332,7 +336,8 @@ module.exports = {
332336
// have to create a shallow clone of schema object,
333337
// so that the original schema.items object will not change
334338
// without this, schemas with circular references aren't faked correctly
335-
let tempSchema = _.omit(schema, 'items');
339+
let tempSchema = _.omit(schema, ['items', 'additionalProperties']);
340+
336341
tempSchema.items = this.resolveRefs(schema.items, parameterSourceOption,
337342
components, schemaResolutionCache, resolveFor, resolveTo, stack, _.cloneDeep(seenRef), stackLimit);
338343
return tempSchema;
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
openapi: "3.0.0"
2+
info:
3+
version: "1.0.0"
4+
title: Pet Service
5+
servers:
6+
- url: "http://localhost:3006"
7+
8+
paths:
9+
/v1/listPets:
10+
post:
11+
summary: Get Pets
12+
description: Retrieve the list of Pets
13+
operationId: listPets
14+
x-rpc-controller: pets
15+
tags:
16+
- Pets
17+
requestBody:
18+
content:
19+
application/json:
20+
schema:
21+
type: object
22+
properties:
23+
test:
24+
type: string
25+
additionalProperties: false
26+
responses:
27+
"200":
28+
description: A list of Pets
29+
content:
30+
application/json:
31+
schema:
32+
$ref: "#/components/schemas/StringResponse1"
33+
"201":
34+
description: A list of Pets
35+
content:
36+
application/json:
37+
schema:
38+
$ref: "#/components/schemas/StringResponse2"
39+
components:
40+
schemas:
41+
StringResponse1:
42+
additionalProperties:
43+
type: string
44+
StringResponse2:
45+
type: object
46+
properties:
47+
test1:
48+
type: string
49+
additionalProperties:
50+
type: string
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
openapi: "3.0.0"
2+
info:
3+
version: "1.0.0"
4+
title: Pet Service
5+
servers:
6+
- url: "http://localhost:3006"
7+
8+
paths:
9+
/v1/listPets:
10+
post:
11+
summary: Get Pets
12+
description: Retrieve the list of Pets
13+
operationId: listPets
14+
x-rpc-controller: pets
15+
tags:
16+
- Pets
17+
requestBody:
18+
content:
19+
application/json:
20+
schema:
21+
allOf:
22+
- $ref: "#/components/schemas/JsonRpcResponse"
23+
- type: object
24+
properties:
25+
result:
26+
$ref: "#/components/schemas/ListPetResponse"
27+
responses:
28+
"200":
29+
description: A list of Pets
30+
content:
31+
application/json:
32+
schema:
33+
allOf:
34+
- $ref: "#/components/schemas/JsonRpcResponse"
35+
- type: object
36+
properties:
37+
result:
38+
$ref: "#/components/schemas/ListPetResponse"
39+
40+
components:
41+
schemas:
42+
43+
JsonRpcResponse:
44+
required:
45+
- jsonrpc
46+
additionalProperties: true
47+
properties:
48+
jsonrpc:
49+
type: string
50+
default: "2.0"
51+
example: "2.0"
52+
nullable: false
53+
54+
Pet:
55+
required:
56+
- id
57+
- name
58+
additionalProperties: false
59+
nullable: true
60+
properties:
61+
id:
62+
type: string
63+
format: uuid
64+
name:
65+
type: string
66+
67+
ListPetResponse:
68+
type: array
69+
additionalProperties: false
70+
items:
71+
$ref: "#/components/schemas/Pet"

test/unit/base.test.js

Lines changed: 73 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,8 @@ describe('CONVERT FUNCTION TESTS ', function() {
7474
specWithAuthDigest = path.join(__dirname, VALID_OPENAPI_PATH + '/specWithAuthDigest.yaml'),
7575
specWithAuthOauth1 = path.join(__dirname, VALID_OPENAPI_PATH + '/specWithAuthOauth1.yaml'),
7676
specWithAuthBasic = path.join(__dirname, VALID_OPENAPI_PATH + '/specWithAuthBasic.yaml'),
77+
schemaWithArrayTypeAndAdditionalProperties =
78+
path.join(__dirname, VALID_OPENAPI_PATH + '/schemaWithArrayTypeAndAdditionalProperties.yaml'),
7779
xmlRequestAndResponseBody = path.join(__dirname, VALID_OPENAPI_PATH, '/xmlRequestAndResponseBody.json'),
7880
xmlRequestAndResponseBodyNoPrefix =
7981
path.join(__dirname, VALID_OPENAPI_PATH, '/xmlRequestAndResponseBodyNoPrefix.json'),
@@ -82,7 +84,9 @@ describe('CONVERT FUNCTION TESTS ', function() {
8284
xmlRequestAndResponseBodyArrayTypeNoPrefix =
8385
path.join(__dirname, VALID_OPENAPI_PATH, '/xmlRequestAndResponseBodyArrayTypeNoPrefix.json'),
8486
xmlRequestAndResponseBodyArrayTypeWrapped =
85-
path.join(__dirname, VALID_OPENAPI_PATH, '/xmlRequestAndResponseBodyArrayTypeWrapped.json');
87+
path.join(__dirname, VALID_OPENAPI_PATH, '/xmlRequestAndResponseBodyArrayTypeWrapped.json'),
88+
schemaWithAdditionalProperties =
89+
path.join(__dirname, VALID_OPENAPI_PATH, '/schemaWithAdditionalProperties.yaml');
8690

8791

8892
it('Should add collection level auth with type as `bearer`' +
@@ -1746,6 +1750,74 @@ describe('CONVERT FUNCTION TESTS ', function() {
17461750
}
17471751
);
17481752
});
1753+
1754+
it('Should fake correctly the body when schema has array type and additionalProperties', function(done) {
1755+
var openapi = fs.readFileSync(schemaWithArrayTypeAndAdditionalProperties, 'utf8');
1756+
Converter.convert({ type: 'string', data: openapi }, { schemaFaker: true }, (err, conversionResult) => {
1757+
const resultantResponseBody = JSON.parse(
1758+
conversionResult.output[0].data.item[0].response[0].body
1759+
),
1760+
resultantRequestBody = JSON.parse(
1761+
conversionResult.output[0].data.item[0].request.body.raw
1762+
);
1763+
expect(err).to.be.null;
1764+
expect(conversionResult.result).to.equal(true);
1765+
expect(conversionResult.output.length).to.equal(1);
1766+
expect(conversionResult.output[0].type).to.equal('collection');
1767+
expect(conversionResult.output[0].data).to.have.property('info');
1768+
expect(conversionResult.output[0].data).to.have.property('item');
1769+
expect(resultantResponseBody.result).to.be.an('array')
1770+
.with.length(2);
1771+
expect(resultantResponseBody.result[0]).to.include.all.keys('id', 'name');
1772+
expect(resultantResponseBody.result[1]).to.include.all.keys('id', 'name');
1773+
expect(resultantRequestBody.result).to.be.an('array')
1774+
.with.length(2);
1775+
expect(resultantRequestBody.result[0]).to.include.all.keys('id', 'name');
1776+
expect(resultantRequestBody.result[1]).to.include.all.keys('id', 'name');
1777+
done();
1778+
});
1779+
});
1780+
1781+
it('Should resolve correctly schemas with additionalProperties as false', function(done) {
1782+
var openapi = fs.readFileSync(schemaWithAdditionalProperties, 'utf8');
1783+
Converter.convert(
1784+
{ type: 'string', data: openapi },
1785+
{ schemaFaker: true },
1786+
(err, conversionResult) => {
1787+
const requestBodyWithAdditionalPropertiesAsFalse =
1788+
JSON.parse(conversionResult.output[0].data.item[0].request.body.raw);
1789+
expect(requestBodyWithAdditionalPropertiesAsFalse).to.include.keys('test');
1790+
expect(Object.keys(requestBodyWithAdditionalPropertiesAsFalse)).to.have.length(1);
1791+
done();
1792+
});
1793+
});
1794+
1795+
it('Should resolve correctly schemas with ONLY additionalProperties', function(done) {
1796+
var openapi = fs.readFileSync(schemaWithAdditionalProperties, 'utf8');
1797+
Converter.convert(
1798+
{ type: 'string', data: openapi },
1799+
{ schemaFaker: true },
1800+
(err, conversionResult) => {
1801+
const responseBodyWithOnlyAdditionalProperties =
1802+
JSON.parse(conversionResult.output[0].data.item[0].response[0].body);
1803+
expect(Object.keys(responseBodyWithOnlyAdditionalProperties).length).to.be.greaterThan(0);
1804+
done();
1805+
});
1806+
});
1807+
1808+
it('Should resolve correctly schemas with additionalProperties', function(done) {
1809+
var openapi = fs.readFileSync(schemaWithAdditionalProperties, 'utf8');
1810+
Converter.convert(
1811+
{ type: 'string', data: openapi },
1812+
{ schemaFaker: true },
1813+
(err, conversionResult) => {
1814+
const responseBodyWithAdditionalProperties =
1815+
JSON.parse(conversionResult.output[0].data.item[0].response[1].body);
1816+
expect(responseBodyWithAdditionalProperties).to.include.keys('test1');
1817+
expect(Object.keys(responseBodyWithAdditionalProperties).length).to.be.greaterThan(1);
1818+
done();
1819+
});
1820+
});
17491821
});
17501822

17511823
describe('Converting swagger 2.0 files', function() {

0 commit comments

Comments
 (0)