Skip to content

Commit b266983

Browse files
authored
Merge pull request #682 from aman-v-singh/fix/issue#401
ISSUE #401 - Fix for schema having enum but no type
2 parents 3245254 + b796539 commit b266983

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
@@ -432,7 +432,7 @@ module.exports = {
432432
}
433433
else if (!schema.hasOwnProperty('default')) {
434434
if (schema.hasOwnProperty('type')) {
435-
// Override default value to schema for CONVERSION only for parmeter resolution set to schema
435+
// Override default value to schema for CONVERSION only for parameter resolution set to schema
436436
if (resolveFor === 'CONVERSION' && resolveTo === 'schema') {
437437
if (!schema.hasOwnProperty('format')) {
438438
schema.default = '<' + schema.type + '>';
@@ -452,10 +452,27 @@ module.exports = {
452452
}
453453
}
454454
else if (schema.enum && schema.enum.length > 0) {
455-
return {
456-
type: (typeof (schema.enum[0])),
457-
value: schema.enum[0]
458-
};
455+
if (resolveFor === 'CONVERSION' && resolveTo === 'schema') {
456+
schema.type = (typeof (schema.enum[0]));
457+
if (!schema.hasOwnProperty('format')) {
458+
schema.default = '<' + schema.type + '>';
459+
}
460+
else if (typesMap.hasOwnProperty(schema.type)) {
461+
schema.default = typesMap[schema.type][schema.format];
462+
if (!schema.default && schema.format) {
463+
schema.default = '<' + schema.format + '>';
464+
}
465+
}
466+
else {
467+
schema.default = '<' + schema.type + (schema.format ? ('-' + schema.format) : '') + '>';
468+
}
469+
}
470+
else {
471+
return {
472+
type: (typeof (schema.enum[0])),
473+
value: schema.enum[0]
474+
};
475+
}
459476
}
460477
else if (isAllOf) {
461478
return schema;

test/unit/base.test.js

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

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

test/unit/deref.test.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -314,6 +314,30 @@ describe('DEREF FUNCTION TESTS ', function() {
314314
done();
315315
});
316316

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+
});
340+
317341
it('should return schema with example parameter(if given) for $ref just like inline schema', function(done) {
318342
var schema = {
319343
$ref: '#/components/schemas/schema1',

0 commit comments

Comments
 (0)