diff --git a/lib/commands/resolve-spec.js b/lib/commands/resolve-spec.js index 3c0b8cd6..1cbe6656 100644 --- a/lib/commands/resolve-spec.js +++ b/lib/commands/resolve-spec.js @@ -55,7 +55,7 @@ exports.builder = { }, n: { alias: 'nullable', - describe: 'Should nullable be resolved?', + describe: 'Should nullable types be resolved?', boolean: true, default: false }, diff --git a/lib/util/utils.js b/lib/util/utils.js index a124aa35..a32bfcd8 100644 --- a/lib/util/utils.js +++ b/lib/util/utils.js @@ -611,15 +611,14 @@ exports.allowNullType = function allowNullType(entity, isPropRequired) { } } + // handling nullable parameters let isPropNullable = entity['x-nullable'] && typeof entity['x-nullable'] === 'boolean'; if ((isPropNullable === undefined && !isPropRequired) || isPropNullable) { let savedEntity = entity if (savedEntity.in) { entity.oneOf = [{ "type": entity.type }, { "type": "null" }]; delete entity.type - - } - else { + } else { entity = {}; entity.oneOf = [savedEntity, { "type": "null" }]; } diff --git a/lib/validate.js b/lib/validate.js index af60458f..d4723c49 100644 --- a/lib/validate.js +++ b/lib/validate.js @@ -66,6 +66,10 @@ exports.validateSpec = function validateSpec(specPath, options) { // validation as that would not match the path parameters defined in the path template // and cause the semantic validation to fail. options.shouldResolveParameterizedHost = false; + + // We shoudln't be resolving nullable types for semantic validaiton as we'll replace nodes + // with oneof arrays which are not semantically valid in swagger 2.0 schema. + options.shouldResolveNullableTypes = false; let validator = new SpecValidator(specPath, null, options); exports.finalValidationResult[specPath] = validator.specValidationResult; return validator.initialize().then(function () { diff --git a/lib/validators/specResolver.js b/lib/validators/specResolver.js index e0068908..381e80c2 100644 --- a/lib/validators/specResolver.js +++ b/lib/validators/specResolver.js @@ -80,19 +80,16 @@ class SpecResolver { if (options.shouldResolveDiscriminator === null || options.shouldResolveDiscriminator === undefined) { options.shouldResolveDiscriminator = true; } - if (options.shouldResolveNullableTypes === null || options.shouldResolveNullableTypes === undefined) { - options.shouldResolveNullableTypes = true; - } - if (options.shouldResolveParameterizedHost === null || options.shouldResolveParameterizedHost === undefined) { options.shouldResolveParameterizedHost = true; } - // Resolving allOf is a neccessary precondition for resolving discriminators. Hence hard setting this to true if (options.shouldResolveDiscriminator) { options.shouldResolveAllOf = true; } - + if (options.shouldResolveNullableTypes === null || options.shouldResolveNullableTypes === undefined) { + options.shouldResolveNullableTypes = true; + } this.options = options; } @@ -163,7 +160,6 @@ class SpecResolver { } else { return Promise.resolve(self); } - // }).catch(function (err) { }).then(() => { if (self.options.shouldResolveNullableTypes) { return self.resolveNullableTypes(); diff --git a/lib/validators/specValidator.js b/lib/validators/specValidator.js index 153420e7..b1aa2267 100644 --- a/lib/validators/specValidator.js +++ b/lib/validators/specValidator.js @@ -46,6 +46,8 @@ class SpecValidator { * * @param {object} [options.shouldResolveParameterizedHost] Should 'x-ms-parameterized-host' be resolved? Default: true * + * @param {object} [options.shouldResolveNullableTypes] Should we allow null values to match any type? Default: true + * * @return {object} An instance of the SpecValidator class. */ constructor(specPath, specInJson, options) {