Instance value (null) not found in enum
Trying out swagger-request-validator
and I’m getting:
{
"messages" : [ {
"key" : "validation.request.body.schema.allOf",
"level" : "ERROR",
"message" : "Instance failed to match all required schemas (matched only 0 out of 2)",
"additionalInfo" : [ "/allOf/0: Instance type (null) does not match any allowed primitive type (allowed: [\"string\"])", "/allOf/0: Instance value (null) not found in enum (possible values: [\"TWENTY_PAY\",\"LIFE_PAY\"])", "/allOf/0: Instance type (null) does not match any allowed primitive type (allowed: [\"string\"])", "/allOf/1: Instance type (null) does not match any allowed primitive type (allowed: [\"object\"])" ],
"context" : {
"requestPath" : "/quotes",
"apiRequestContentType" : "application/json",
"location" : "REQUEST",
"requestMethod" : "POST"
}
} ]
}
I've got an OpenAPI spec, the request has an enum, the property isn’t required … so why is swagger-request-validator
complaining about a null?
Comments (7)
-
reporter -
reporter Nope, can’t really do that in OpenAPI:
https://github.com/OAI/OpenAPI-Specification/issues/229
Suggestions if I have an OpenAPI spec with nullable enums and I’d like to use this validator are welcome.
-
reporter -
reporter FWIW, I’m working around this for now by customizing Jackson within RestAssured to not submit properties (inclusion set to ignore nulls), in case anyone else hits this and there isn’t a validator solution yet.
-
That a field can be null has to be specifically set on the interface definition.
In OpenAPI 3 it’s done with
nullable
(https://swagger.io/docs/specification/data-models/data-types/#null). In OpenAPI 2 / Swagger it’s a vendor extension withx-nullable
. -
reporter Yeah, I think we moved to OAS 3 and added nullability since then; not sure what the right move would be in OAS 2, given that it’s a vendor extension?
-
Just use
x-nullable
instead ofnullable
on the OAS2 definitions. That’s all. - Log in to comment
Ah – wait; am I supposed to include null in the enum list even if the property isn’t required?
e.g:
https://github.com/epoberezkin/ajv/issues/824