Skip to content

Commit 52c8835

Browse files
committed
Resolving comments
- Returning the condition when schema only contains additionalProperties - The condition was modified to resolve the schema as object only if it doesn't have type - If schema have array type it will be resolved as array - If schema have any other type and additionalProperties it will ignore the additionalProperties
1 parent 1773e2e commit 52c8835

File tree

3 files changed

+127
-30
lines changed

3 files changed

+127
-30
lines changed

lib/deref.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -260,11 +260,15 @@ module.exports = {
260260
return additionalProperties;
261261
};
262262

263-
if (concreteUtils.compareTypes(schema.type, SCHEMA_TYPES.object) || schema.hasOwnProperty('properties')) {
263+
if (
264+
concreteUtils.compareTypes(schema.type, SCHEMA_TYPES.object) ||
265+
schema.hasOwnProperty('properties') ||
266+
(schema.hasOwnProperty('additionalProperties') && !schema.hasOwnProperty('type'))
267+
) {
264268
// go through all props
265269
schema.type = SCHEMA_TYPES.object;
266270

267-
if (_.has(schema, 'properties')) {
271+
if (_.has(schema, 'properties') || _.has(schema, 'additionalProperties')) {
268272
let tempSchema = _.omit(schema, ['properties', 'additionalProperties']);
269273
// shallow cloning schema object except properties object
270274

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

test/unit/base.test.js

Lines changed: 71 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,9 @@ describe('CONVERT FUNCTION TESTS ', function() {
8484
xmlRequestAndResponseBodyArrayTypeNoPrefix =
8585
path.join(__dirname, VALID_OPENAPI_PATH, '/xmlRequestAndResponseBodyArrayTypeNoPrefix.json'),
8686
xmlRequestAndResponseBodyArrayTypeWrapped =
87-
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');
8890

8991

9092
it('Should add collection level auth with type as `bearer`' +
@@ -1568,33 +1570,6 @@ describe('CONVERT FUNCTION TESTS ', function() {
15681570
});
15691571
});
15701572

1571-
it('Should fake correctly the body when schema has array type and additionalProperties', function(done) {
1572-
var openapi = fs.readFileSync(schemaWithArrayTypeAndAdditionalProperties, 'utf8');
1573-
Converter.convert({ type: 'string', data: openapi }, { schemaFaker: true }, (err, conversionResult) => {
1574-
const resultantResponseBody = JSON.parse(
1575-
conversionResult.output[0].data.item[0].response[0].body
1576-
),
1577-
resultantRequestBody = JSON.parse(
1578-
conversionResult.output[0].data.item[0].request.body.raw
1579-
);
1580-
expect(err).to.be.null;
1581-
expect(conversionResult.result).to.equal(true);
1582-
expect(conversionResult.output.length).to.equal(1);
1583-
expect(conversionResult.output[0].type).to.equal('collection');
1584-
expect(conversionResult.output[0].data).to.have.property('info');
1585-
expect(conversionResult.output[0].data).to.have.property('item');
1586-
expect(resultantResponseBody.result).to.be.an('array')
1587-
.with.length(2);
1588-
expect(resultantResponseBody.result[0]).to.include.all.keys('id', 'name');
1589-
expect(resultantResponseBody.result[1]).to.include.all.keys('id', 'name');
1590-
expect(resultantRequestBody.result).to.be.an('array')
1591-
.with.length(2);
1592-
expect(resultantRequestBody.result[0]).to.include.all.keys('id', 'name');
1593-
expect(resultantRequestBody.result[1]).to.include.all.keys('id', 'name');
1594-
done();
1595-
});
1596-
});
1597-
15981573
it('Should convert and resolve xml bodies correctly when prefix is provided', function(done) {
15991574
var openapi = fs.readFileSync(xmlRequestAndResponseBody, 'utf8');
16001575
Converter.convert(
@@ -1775,6 +1750,74 @@ describe('CONVERT FUNCTION TESTS ', function() {
17751750
}
17761751
);
17771752
});
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+
});
17781821
});
17791822

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

0 commit comments

Comments
 (0)