required vs. nullable: required but nullable field reported as error

Issue #193 on hold
Marcel Overdijk created an issue

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)

  1. Log in to comment