Schemas and Data Types
One of the most important parts of OpenAPI is the schema object. Schema objects are used to describe HTTP request and response bodies, parameters, headers, and all sorts of other data, whether its JSON, XML, or primitive types like integers and strings.
If you’re familiar with JSON Schema, you’ll be right at home here, because OpenAPI v3.1 uses JSON Schema (draft 2020-12). For those who have not used JSON Schema before, that’s ok, follow along.
The first thing to learn about a schema is the type keyword, which can be one or more of the following types:
- null
- boolean
- object
- array
- number
- string
You can also use “integer” for the sake of convenience, but “integer” and “number” are basically identical because JSON itself does not make that distinction. Since there is no distinct JSON integer type, JSON Schema defines integers mathematically. This means that both 1 and 1.0 are equivalent, and are both considered to be integers.
A schema object looks like this:
type: string
It can also define multiple types, for instance making a property nullable:
type: [string, null]
It also allows you to support properties that could be either a number or a numeric string.
type: [number, string]
Describing an object will often involve the use of properties to define what each of the objects properties should look like, and each of those properties will have a “subschema” that describes it.
type: object
required:
- name
properties:
name:
type: string
age:
type: integer
Arrays work in a similar way, with an items keyword allowing each item in the array to be described.
type: array
items:
type: string
Arrays of objects can be described by combining the two concepts:
type: array
items:
type: object
required:
- name
properties:
name:
type: string
age:
type: integer
Data Formats #
The type keyword sets out the basic data type, but knowing something is a string or an integer is just the first step to understanding what that data is all about.
First of all there’s the format keyword, which covers a predefined set of common formats:
type: array
items:
type: object
required:
- name
properties:
name:
type: string
email:
type: string
format: email
age:
type: integer
format: int32
The full list of formats defined in the JSON Schema Validation that OpenAPI v3.1 relies upon:
-
date-time: A string instance is valid against this attribute if it is a valid representation according to the “date-time” production as defined in RFC3339.
-
date: A string instance is valid against this attribute if it is a valid representation according to the “full-date” production as defined in RFC3339.
-
time: A string instance is valid against this attribute if it is a valid representation according to the “full-time” production as defined in RFC3339.
-
duration: A string instance is valid against this attribute if it is a valid representation according to the “duration” production as defined in RFC3339.
-
email: As defined by the “Mailbox” ABNF rule in RFC 5321, section 4.1.2 RFC5321.
-
idn-email: As defined by the extended “Mailbox” ABNF rule in RFC 6531, section 3.3 RFC6531.
-
hostname: As defined by RFC 1123, section 2.1 RFC1123, including host names produced using the Punycode algorithm specified in RFC 5891, section 4.4 RFC5891.
-
idn-hostname: As defined by either RFC 1123 as for hostname, or an internationalized hostname as defined by RFC 5890, section 2.3.2.3 RFC5890.
-
ipv4: An IPv4 address according to the “dotted-quad” ABNF syntax as defined in RFC 2673, section 3.2 RFC2673.
-
ipv6: An IPv6 address as defined in RFC 4291, section 2.2 RFC4291.
-
uri: A string instance is valid against this attribute if it is a valid URI, according to RFC3986.
-
uri-reference: A string instance is valid against this attribute if it is a valid URI Reference (either a URI or a relative- reference), according to RFC3986.
-
iri: A string instance is valid against this attribute if it is a valid IRI, according to RFC3987.
-
iri-reference: A string instance is valid against this attribute if it is a valid IRI Reference (either an IRI or a relative- reference), according to RFC3987.
-
uuid: A string instance is valid against this attribute if it is a valid string representation of a UUID, according to RFC4122.
-
uri-template: A string instance is valid against this attribute if it is a valid URI Template (of any level), according to RFC6570.
-
json-pointer: A string instance is valid against this attribute if it is a valid JSON string representation of a JSON Pointer, according to RFC 6901, section 5 RFC6901.
-
relative-json-pointer: A string instance is valid against this attribute if it is a valid Relative JSON Pointer