0
"DogRequest"{
  "type": "object",
  "properties": {
    "height": {
      "type": "string"
     }
  }
}


"DogResponse"{
  "type": "object",
  "properties": {
    "bark": {
      "type": "string"
     }
  }
}

Note that I don't have any required properties defined.

I noticed that when making a request, I can have height=None

However, in my response, if bark=None, swagger throws a validation error, that None is not of type "string".

What is the rule for having properties (and additionalProperties) being null, EVEN if they aren't defined as required?

I did notice that for properties defined as required, it must be the case that they exist AND are not null. And if I want to allow the property to be null, I must include "x-nullable": true.

Do I have to include "x-nullable": true for properties that are not required as well?

Why am I seeing an inconsistency?

aabb
  • 23
  • 3
  • Possible duplicate of [How to define a property that can be string or null in OpenAPI (Swagger)?](https://stackoverflow.com/questions/48111459/how-to-define-a-property-that-can-be-string-or-null-in-openapi-swagger) – Helen Apr 26 '18 at 18:46
  • Also related: [Additional properties not allowed: nullable Swagger](https://stackoverflow.com/q/48504816/113116) – Helen Apr 26 '18 at 18:59

1 Answers1

0

OpenAPI 2.0 does not support null as the data type. Some tools use x-nullable: true to handle nulls, but it's not part of the OpenAPI Specification, so whether or not it will work depends on the tools you use.

Support for null was added in OpenAPI 3.0, where properties can be marked as nullable: true.

Helen
  • 87,344
  • 17
  • 243
  • 314
  • Ah I see. Using Swagger 2.0 so x-nullable: true works. However, do you know why I am maybe seeing that null is ok when sending a request, but not ok when receiving a response? For some reason, sending bark=None in the request does not throw any validation errors, and I didn't need to add x-nullable: true... – aabb Apr 27 '18 at 20:24
  • @aabb it's hard to say without seeing your actual code, and it probably depends on the framework/library you are using (there are [lots of libraries](https://swagger.io/open-source-integrations/) and their functionality may be different). Maybe your framework/library validates the responses against the OpenAPI definition but does not validate requests. Maybe when serializing requests it does not include `None` values in requests. Or something like that. – Helen Apr 27 '18 at 20:56