Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bugfix/2098 allows null values when nullable #2101

9 changes: 8 additions & 1 deletion src/plugins/validate-semantic/validators/2and3/schemas.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,18 +23,24 @@ export const validate2And3TypeArrayRequiresItems = () => (system) => {
})
}



export const validate2And3TypesInDefaultValuesMatchesWithEnum = () => (system) => {
return system.validateSelectors
.allSchemas()
.then(nodes => {
return nodes.reduce((acc, node) => {
const schemaObj = node.node
const { type } = schemaObj || {}
const isNullable = !!schemaObj.nullable
const enumeration = schemaObj.enum
if (enumeration !== null && typeof enumeration !== "undefined") {
var enumIndex = 0
enumeration.forEach((element, index) => {
var isValidFormat = true
if (element === null && isNullable) {
return
}
ralphdoe marked this conversation as resolved.
Show resolved Hide resolved
if (type === "array" && (!Array.isArray(element) || element === null)) {
isValidFormat = false
enumIndex = index
Expand All @@ -55,9 +61,10 @@ export const validate2And3TypesInDefaultValuesMatchesWithEnum = () => (system) =
level: "warning",
})
}

})
}
return acc
}, [])
})
}
}
29 changes: 29 additions & 0 deletions test/unit/plugins/validate-semantic/2and3/schemas.js
Original file line number Diff line number Diff line change
Expand Up @@ -514,4 +514,33 @@ describe(`values in Enum must be instance of the defined type`, () => {
expect(allErrors.length).toEqual(0)
})
})

it("should not return an error for a null value in a enum object when nullable is true type in OpenApi 3", () => {
const spec = {
openapi: "3.0.0",
"paths": {
"/pets": {
"get": {
"parameters": [
{
name: "objectSample",
in: "query",
schema: {
type: "object",
nullable: true,
enum: [null]
}
},
]
}
}
}
}

return validateHelper(spec)
.then(system => {
const allErrors = system.errSelectors.allErrors().toJS()
expect(allErrors.length).toEqual(0)
})
})
})