Overview

7 Describing data with JSON Schema in OpenAPI

This chapter moves from describing HTTP operations to describing the data those operations exchange, showing how OpenAPI leverages JSON Schema to model, validate, and document API data. It argues against spreadsheets and ad‑hoc documents for data modeling, favoring first‑class, tool‑friendly schemas embedded in the OpenAPI file. The chapter also clarifies practical authoring choices (JSON vs. YAML) and version alignment, noting that OpenAPI 3.1 pairs with a recent JSON Schema edition while most fundamentals remain consistent across versions.

The core practice is to define complete, reusable resource models under components.schemas, independent of any single operation. Schemas typically start with a type, then describe objects via a properties map and required list, covering atomic types (string, integer, number, boolean) and optional formats, as well as nested objects and arrays through items. Marking which fields are mandatory is explicit, and naming conventions for models are flexible. Keeping canonical models centralized improves consistency, accelerates iteration, and provides a clearer view of the API’s subject matter.

With resource models in place, the chapter shows how to wire data into operations. Non‑body elements (path and query parameters, request and response headers) can use inline schemas or reference pieces derived from resource models; array query parameters can have their URL serialization tuned with style and explode. Request and response bodies typically reference reusable schemas through $ref and JSON Pointers, avoiding duplication and drift. The chapter also demonstrates deriving specialized models (for creation/replacement or summaries) and mixing inline structures with references (for example, arrays of referenced items). It advises prioritizing clarity over premature optimization now, with deeper reuse patterns and consolidation left for a later pass.

Using a spreadsheet for the data models of our API was only to simplify our learning. We can describe data models with JSON Schema in our OpenAPI document once we start designing them.
CH07 F01 lauret3
No need for the API spreadsheet when modeling data, we can use JSON Schema within our OpenAPI document instead.
CH07 F02 lauret3
We describe data using OpenAPI and JSON Schema while modeling it. We proceed with the same steps: modeling the resources and deriving them.
CH07 F03 lauret3
We’ll reproduce (and replace) the "Product" resource complete model tables with a JSON schema.
CH07 F04 lauret3
We’ll add the atomic properties of the Product resource to the Product schema without caring that they are required or optional.
CH07 F05 lauret3
We’ll add an object property and its properties without caring that they are required or optional.
CH07 F06 lauret3
We’ll add an array property to the Product schema without caring if it’s required or optional.
CH07 F07 lauret3
We’ll indicate which properties of the Product resource are required.
CH07 F08 lauret3
In OpenAPI non-body and body data of HTTP request and response are not defined in the same location, but all leverage JSON Schema.
CH07 F09 lauret3
We’ll derive the Product model into the Product Creation or Replacement model.
CH07 F10 lauret3
The Product Summary model is a subset of the Product model for lists.
CH07 F11 lauret3

FAQ

What is JSON Schema and how does OpenAPI use it?JSON Schema is an independent standard for validating and annotating JSON (and YAML) data. OpenAPI leverages JSON Schema to describe data models everywhere data appears in an API description: resource models, request and response bodies, parameters, and headers. This enables consistent design, documentation, mocking, and testing.
Which JSON Schema and OpenAPI versions should I be aware of?OpenAPI 3.1 uses JSON Schema 2020-12; OpenAPI 3.0 uses draft-5. Most basic schemas look the same across these versions, but advanced features and tooling support can differ. In YAML OpenAPI documents, schemas can be authored in YAML (or JSON).
Where should reusable data models live in an OpenAPI document?Place reusable models under components.schemas. Defining resource models there makes them independent of any single operation, improves reusability across endpoints, and gives a clearer view of the domain. Reference them from operations instead of copying. PascalCase (e.g., Product) is a common naming style.
How do I describe an object and its properties with JSON Schema?Set type: object and add a properties map where each key is a property name and each value is that property’s schema. Use JSON Schema types (string, integer, number, boolean, object, array) and optional annotations like description and format (for example, date, date-time, int32, float).
How do I mark required vs. optional object properties?Add a required array to the object schema listing the names of properties that must be present. Properties not listed are optional. Each object (including nested ones) controls its own required list. Required applies both to inputs (clients must send them) and outputs (servers must return them).
How do I model arrays and their element types?Use type: array and define items with the schema of each element. Elements can be atomics (like string) or objects. Additional constraints (such as minItems or maxItems) can be added when needed.
How do I describe path and query parameters, including arrays?Parameters have a schema property holding their JSON Schema. When a parameter corresponds to a resource property, copy that property’s schema for consistency. For array query parameters, control serialization with style and explode (e.g., style=form and explode=false for CSV like keywords=anime,space). Path parameters must be marked required: true.
How do I document response headers?Response headers are defined under responses.[status].headers. Each header entry includes a schema describing its type (for example, Location with type: string). You can author inline schemas or define reusable headers if multiple responses share the same header.
How do I reuse models in request and response bodies?Put the model in components.schemas and reference it using $ref with a JSON Pointer (for example, #/components/schemas/Product). This avoids duplication and keeps responses and requests consistent. The same approach applies to shared error models.
How do I derive specialized models and mix inline schemas with references?Create a new reusable schema derived from a complete model by copying and removing properties not applicable in a context (e.g., ProductCreationOrReplacement without server-managed fields). You can also mix approaches, such as defining an inline array schema whose items use a $ref to a summary model (e.g., ProductSummary). Start simple; optimize and factor further once the design stabilizes.

pro $24.99 per month

  • access to all Manning books, MEAPs, liveVideos, liveProjects, and audiobooks!
  • choose one free eBook per month to keep
  • exclusive 50% discount on all purchases
  • renews monthly, pause or cancel renewal anytime

lite $19.99 per month

  • access to all Manning books, including MEAPs!

team

5, 10 or 20 seats+ for your team - learn more


choose your plan

team

monthly
annual
$49.99
$499.99
only $41.67 per month
  • five seats for your team
  • access to all Manning books, MEAPs, liveVideos, liveProjects, and audiobooks!
  • choose another free product every time you renew
  • choose twelve free products per year
  • exclusive 50% discount on all purchases
  • renews monthly, pause or cancel renewal anytime
  • renews annually, pause or cancel renewal anytime
  • The Design of Web APIs, Second Edition ebook for free
choose your plan

team

monthly
annual
$49.99
$499.99
only $41.67 per month
  • five seats for your team
  • access to all Manning books, MEAPs, liveVideos, liveProjects, and audiobooks!
  • choose another free product every time you renew
  • choose twelve free products per year
  • exclusive 50% discount on all purchases
  • renews monthly, pause or cancel renewal anytime
  • renews annually, pause or cancel renewal anytime
  • The Design of Web APIs, Second Edition ebook for free
choose your plan

team

monthly
annual
$49.99
$499.99
only $41.67 per month
  • five seats for your team
  • access to all Manning books, MEAPs, liveVideos, liveProjects, and audiobooks!
  • choose another free product every time you renew
  • choose twelve free products per year
  • exclusive 50% discount on all purchases
  • renews monthly, pause or cancel renewal anytime
  • renews annually, pause or cancel renewal anytime
  • The Design of Web APIs, Second Edition ebook for free