Skip to content

Commit 3fd662a

Browse files
committed
IMPORT-795 : ISSUE#401 - Fix for schema having enum but no type
1 parent 0ce4b5e commit 3fd662a

File tree

3 files changed

+47
-6
lines changed

3 files changed

+47
-6
lines changed

lib/deref.js

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -427,7 +427,7 @@ module.exports = {
427427
}
428428
else if (!schema.hasOwnProperty('default')) {
429429
if (schema.hasOwnProperty('type')) {
430-
// Override default value to schema for CONVERSION only for parmeter resolution set to schema
430+
// Override default value to schema for CONVERSION only for parameter resolution set to schema
431431
if (resolveFor === 'CONVERSION' && resolveTo === 'schema') {
432432
if (!schema.hasOwnProperty('format')) {
433433
schema.default = '<' + schema.type + '>';
@@ -447,10 +447,27 @@ module.exports = {
447447
}
448448
}
449449
else if (schema.enum && schema.enum.length > 0) {
450-
return {
451-
type: (typeof (schema.enum[0])),
452-
value: schema.enum[0]
453-
};
450+
if (resolveFor === 'CONVERSION' && resolveTo === 'schema') {
451+
schema.type = (typeof (schema.enum[0]));
452+
if (!schema.hasOwnProperty('format')) {
453+
schema.default = '<' + schema.type + '>';
454+
}
455+
else if (typesMap.hasOwnProperty(schema.type)) {
456+
schema.default = typesMap[schema.type][schema.format];
457+
if (!schema.default && schema.format) {
458+
schema.default = '<' + schema.format + '>';
459+
}
460+
}
461+
else {
462+
schema.default = '<' + schema.type + (schema.format ? ('-' + schema.format) : '') + '>';
463+
}
464+
}
465+
else {
466+
return {
467+
type: (typeof (schema.enum[0])),
468+
value: schema.enum[0]
469+
};
470+
}
454471
}
455472
else if (isAllOf) {
456473
return schema;

test/unit/base.test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1821,7 +1821,7 @@ describe('CONVERT FUNCTION TESTS ', function() {
18211821
});
18221822

18231823
describe('Converting swagger 2.0 files', function() {
1824-
it('should convert path paramters to postman-compatible paramters', function (done) {
1824+
it('should convert path parameters to postman-compatible parameters', function (done) {
18251825
const fileData = path.join(__dirname, SWAGGER_20_FOLDER_JSON, 'swagger2-with-params.json'),
18261826
input = {
18271827
type: 'file',

test/unit/deref.test.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -313,6 +313,30 @@ describe('DEREF FUNCTION TESTS ', function() {
313313
expect(output.required).to.not.include('tag');
314314
done();
315315
});
316+
317+
it('should handle schema with enum having no type defined for resolveTo set as schema', function(done) {
318+
var schema = {
319+
'enum': [
320+
'capsule',
321+
'probe',
322+
'satellite',
323+
'spaceplane',
324+
'station'
325+
]
326+
},
327+
resolveFor = 'CONVERSION',
328+
resolveTo = 'schema',
329+
parameterSource = 'REQUEST',
330+
output;
331+
332+
output = deref.resolveRefs(schema, parameterSource, { concreteUtils: schemaUtils30X }, {
333+
resolveFor,
334+
resolveTo
335+
});
336+
expect(output.type).to.equal('string');
337+
expect(output.default).to.equal('<string>');
338+
done();
339+
});
316340
});
317341

318342
describe('resolveAllOf Function', function () {

0 commit comments

Comments
 (0)