Table of contents
Introduction
OpenAPI (formerly known as Swagger) [↗] is a tool for defining APIs. It allows you to describe your API structure and data types in a standardized format. One common requirement is to allow attributes to have null values, representing an absence of data.
Setting an Attribute to Null
To set an attribute to null in OpenAPI, you need to use the nullable
property. This property is a boolean that, when set to true
, indicates that the attribute can accept a null value.
Example
Here is an example of how to set an attribute to null in an OpenAPI schema:
01: openapi: 3.0.0
02: info:
03: title: Example API
04: version: 1.0.0
05: paths:
06: /example:
07: get:
08: summary: Get example
09: responses:
10: '200':
11: description: Successful response
12: content:
13: application/json:
14: schema:
15: type: object
16: properties:
17: name:
18: type: string
19: nullable: true
20: description: The name of the example
21: age:
22: type: integer
23: nullable: true
24: description: The age of the example
25: ...
In this example, both the name
[→] and age
[→] attributes are defined with the nullable
[→] property set to true
, allowing them to have null values.
Conclusion
Allowing attributes to be null
in OpenAPI is straightforward with the nullable
property. This ensures that your API can handle cases where certain data may not be available.
Here is another article you might like 😊 How To Override The Base URL For A Specific Endpoint In An OpenAPI Spec