null
OpenAPI 3.0.X
OpenAPI 3.0.X doesn’t support a null
type but instead allows you to mark a schema as being nullable
. This allows that type to either contain a valid value or null.
# A nullable stringschema:type: stringnullable: true# A nullable integerschema:type: integerformat: int32nullable: true# A nullable booleanschema:type: booleannullable: true# A nullable arrayschema:type: arrayitems:type: stringnullable: true# A nullable objectschema:type: objectproperties:foo:type: stringnullable: true
OpenAPI 3.1.X
OpenAPI 3.1 aligned describing null
with JSON Schema. This allows for more precise API definitions, especially for APIs that need to explicitly support null values as valid inputs or outputs.
To specify that a property, item, or response can be null
, you can use the type
keyword with a value of null
or combine null with other types using the oneOf
or type array syntax. This flexibility makes it easier to accurately model your data.
# A nullable string using array syntaxschema:type: [ 'null', 'string' ]# A nullable field using an arrayschema:type: objectproperties:foo:type: ['null', 'string']# A nullable field using oneOfschema:type: objectproperties:foo:oneOf:- type: null- type: string