Skip to content

Commit 756b727

Browse files
committed
Fixed various known type errors related issues
1 parent 84d982c commit 756b727

File tree

6 files changed

+142
-9
lines changed

6 files changed

+142
-9
lines changed

lib/schemaUtils.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1513,7 +1513,7 @@ module.exports = {
15131513
var example,
15141514
exampleKey;
15151515

1516-
if (typeof exampleObj !== 'object') {
1516+
if (!exampleObj || typeof exampleObj !== 'object') {
15171517
return '';
15181518
}
15191519

@@ -1720,7 +1720,7 @@ module.exports = {
17201720

17211721
Object.keys(deepObject).forEach((key) => {
17221722
let value = deepObject[key];
1723-
if (typeof value === 'object') {
1723+
if (value && typeof value === 'object') {
17241724
extractedParams = _.concat(extractedParams, this.extractDeepObjectParams(value, objectKey + '[' + key + ']'));
17251725
}
17261726
else {
@@ -2640,7 +2640,7 @@ module.exports = {
26402640
// App / Collection transformer fail with the object syntax
26412641
if (item.request.url.variables.members && item.request.url.variables.members.length > 0) {
26422642
item.request.url.variables.members = _.map(item.request.url.variables.members, (m) => {
2643-
if (typeof m.description === 'object' && m.description.content) {
2643+
if (m.description && typeof m.description === 'object' && m.description.content) {
26442644
m.description = m.description.content;
26452645
}
26462646
return m;

libV2/schemaUtils.js

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -401,7 +401,7 @@ let QUERYPARAM = 'query',
401401
let example = {},
402402
exampleKey;
403403

404-
if (typeof exampleObj !== 'object') {
404+
if (!exampleObj || typeof exampleObj !== 'object') {
405405
return '';
406406
}
407407

@@ -545,6 +545,11 @@ let QUERYPARAM = 'query',
545545
{ includeDeprecated } = context.computedOptions;
546546

547547
_.forOwn(schema.properties, (property, propertyName) => {
548+
// Skip property resolution if it's not schema object
549+
if (!_.isObject(property)) {
550+
return;
551+
}
552+
548553
if (
549554
property.format === 'decimal' ||
550555
property.format === 'byte' ||
@@ -875,7 +880,7 @@ let QUERYPARAM = 'query',
875880

876881
Object.keys(deepObject).forEach((key) => {
877882
let value = deepObject[key];
878-
if (typeof value === 'object') {
883+
if (value && typeof value === 'object') {
879884
extractedParams = _.concat(extractedParams, extractDeepObjectParams(value, objectKey + '[' + key + ']'));
880885
}
881886
else {
@@ -1720,8 +1725,9 @@ let QUERYPARAM = 'query',
17201725
let responses = [],
17211726
requestAcceptHeader;
17221727

1723-
_.forOwn(operationItem.responses, (responseSchema, code) => {
1728+
_.forOwn(operationItem.responses, (responseObj, code) => {
17241729
let response,
1730+
responseSchema = _.has(responseObj, '$ref') ? resolveSchema(context, responseObj) : responseObj,
17251731
{ includeAuthInfoInExample } = context.computedOptions,
17261732
responseAuthHelper,
17271733
auth = request.auth,

libV2/validationUtils.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -885,7 +885,7 @@ function extractDeepObjectParams (deepObject, objectKey) {
885885

886886
Object.keys(deepObject).forEach((key) => {
887887
let value = deepObject[key];
888-
if (typeof value === 'object') {
888+
if (value && typeof value === 'object') {
889889
extractedParams = _.concat(extractedParams, extractDeepObjectParams(value, objectKey + '[' + key + ']'));
890890
}
891891
else {
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
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: string
23+
responses:
24+
'200':
25+
description: Expected response to a valid request
26+
content:
27+
application/json:
28+
schema:
29+
$ref: "#/components/schemas/Pets"
30+
default:
31+
$ref: "#/components/responses/defaultRes"
32+
components:
33+
responses:
34+
defaultRes:
35+
description: unexpected error
36+
content:
37+
application/json:
38+
schema:
39+
$ref: "#/components/schemas/Error"
40+
headers:
41+
x-trace-id:
42+
schema:
43+
type: string
44+
example: 12345-6789
45+
schemas:
46+
Pet:
47+
required:
48+
- id
49+
- name
50+
properties:
51+
id:
52+
type: integer
53+
format: int64
54+
name:
55+
type: string
56+
tag:
57+
type: string
58+
Pets:
59+
type: array
60+
items:
61+
$ref: "#/components/schemas/Pet"
62+
Error:
63+
required:
64+
- code
65+
- message
66+
properties:
67+
code:
68+
type: integer
69+
format: int32
70+
message:
71+
type: string
72+
nullProp: null

test/unit/convertV2.test.js

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,9 @@ const expect = require('chai').expect,
8181
xmlRequestAndResponseBodyArrayTypeWrapped =
8282
path.join(__dirname, VALID_OPENAPI_PATH, '/xmlRequestAndResponseBodyArrayTypeWrapped.json'),
8383
schemaWithAdditionalProperties =
84-
path.join(__dirname, VALID_OPENAPI_PATH, '/schemaWithAdditionalProperties.yaml');
84+
path.join(__dirname, VALID_OPENAPI_PATH, '/schemaWithAdditionalProperties.yaml'),
85+
specWithResponseRef =
86+
path.join(__dirname, VALID_OPENAPI_PATH, '/specWithResponseRef.yaml');
8587

8688

8789
describe('The convert v2 Function', function() {
@@ -2073,4 +2075,33 @@ describe('The convert v2 Function', function() {
20732075
done();
20742076
});
20752077
});
2078+
2079+
it('Should convert a collection with response $ref correctly', function(done) {
2080+
var openapi = fs.readFileSync(specWithResponseRef, 'utf8');
2081+
Converter.convertV2({ type: 'string', data: openapi }, { parametersResolution: 'Example' },
2082+
(err, conversionResult) => {
2083+
expect(err).to.be.null;
2084+
expect(conversionResult.result).to.equal(true);
2085+
expect(conversionResult.output.length).to.equal(1);
2086+
expect(conversionResult.output[0].type).to.equal('collection');
2087+
expect(conversionResult.output[0].data).to.have.property('info');
2088+
expect(conversionResult.output[0].data).to.have.property('item');
2089+
expect(conversionResult.output[0].data.item.length).to.equal(1);
2090+
2091+
const item = conversionResult.output[0].data.item[0].item[0].item[0];
2092+
2093+
expect(item.response[1].name).to.eql('unexpected error');
2094+
expect(item.response[1].header.length).to.eql(2);
2095+
expect(item.response[1].header[0].key).to.eql('Content-Type');
2096+
expect(item.response[1].header[0].value).to.eql('application/json');
2097+
expect(item.response[1].header[1].key).to.eql('x-trace-id');
2098+
expect(item.response[1].header[1].value).to.eql('12345-6789');
2099+
expect(JSON.parse(item.response[1].body)).to.have.property('code');
2100+
expect(JSON.parse(item.response[1].body)).to.have.property('message');
2101+
2102+
// Incorrectly defined properties will be skipped
2103+
expect(JSON.parse(item.response[1].body)).to.not.have.property('nullProp');
2104+
done();
2105+
});
2106+
});
20762107
});

test/unit/schemaUtils.test.js

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
1-
const { getParametersForPathItem, verifyDeprecatedProperties } = require('../../lib/schemaUtils'),
1+
const {
2+
getParametersForPathItem,
3+
verifyDeprecatedProperties,
4+
getExampleData,
5+
extractDeepObjectParams
6+
} = require('../../lib/schemaUtils'),
27
expect = require('chai').expect;
38

49

@@ -261,3 +266,22 @@ describe('verifyDeprecatedProperties function', function () {
261266
expect(schema.properties.b.properties.d).to.not.be.undefined;
262267
});
263268
});
269+
270+
describe('getExampleData function', function () {
271+
it('should correctly provide result with null example object', function () {
272+
const result = getExampleData(null, {}, {});
273+
274+
expect(result).to.equal('');
275+
});
276+
});
277+
278+
describe('extractDeepObjectParams function', function () {
279+
it('should correctly provide result with nested object containing null values', function () {
280+
const result = extractDeepObjectParams({ id: null }, 'user');
281+
282+
expect(result).to.eql([{
283+
key: 'user[id]',
284+
value: null
285+
}]);
286+
});
287+
});

0 commit comments

Comments
 (0)