- edited description
required vs. nullable: required but nullable field reported as error
We have an schema like:
type: object
required:
- id
- name
- type
- company
- primaryContact
properties:
id:
type: string
name:
type: string
type:
type: string
enum: [INDIVIDUAL, BUSINESS]
company:
type: object
nullable: true
$ref: '../openapi.yml#/components/schemas/company'
primaryContact:
$ref: '../openapi.yml#/components/schemas/contact'
and when the Spring Controller returns:
{
"id": "300910001",
"name": "Miriam Carlson",
"type": "INDIVIDUAL",
"company": null,
"primaryContact": {
"id": "contact910001",
"birthDate": "1987-05-17",
"salutation": "MADAM",
"initials": "M",
"firstName": "Miriam",
"middleName": null,
"lastName": "Carlson",
"email": null,
"phone": null
}
}
We get the swagger-request-validator error:
com.atlassian.oai.validator.mockmvc.OpenApiMatchers$OpenApiValidationException: {
"messages" : [ {
"key" : "validation.response.body.schema.type",
"level" : "ERROR",
"message" : "[Path '/company'] Instance type (null) does not match any allowed primitive type (allowed: [\"object\"])",
"context" : {
"requestPath" : "/rest/customers/300910001",
"responseStatus" : 200,
"location" : "RESPONSE",
"requestMethod" : "GET"
}
} ]
}
We are interpreting required as that the field should be in de response, and nullable if the value of the field could be null
or not.
In this case the company
field is defined as required but nullable.
As the response contains "company": null
we would expect this to be valid.
Are we misunderstanding the meaning of required and nullable or is this a bug in swagger-request-validator?
Tested with 2.0.2
and 2.1.0
.
Comments (7)
-
reporter -
reporter - edited description
-
Is issue #1368 'Reference objects don't combine well with “nullable"' of OpenAPI is related to this? Would
anyOf
be a solution? -
Account Deactivated I have also recently run into this issue. Essentially, we ended up declaring all of our nullable fields in the following way:
finalized: description: Date and time of when the record was finalized. oneOf: - type: string - type: null format: date-time
instead of using
nullable: true
which is supposed to be the correct solution: https://swagger.io/docs/specification/data-models/data-types/#nullable -
This topic seems to be hotly debated on the OpenAPI side. See https://github.com/OAI/OpenAPI-Specification/blob/master/proposals/003_Clarify-Nullable.md
I will put this issue on hold until the matter is resolved on the spec side, and then we can implement whatever semantics are landed on.
-
- changed status to on hold
-
Any updates on this issue?
As per github …we have https://github.com/OAI/OpenAPI-Specification/blob/main/proposals/2019-10-31-Clarify-Nullable.md and https://github.com/OAI/OpenAPI-Specification/pull/2529. This should have been working.
We still see this as a problem
- Log in to comment