17 Optimizing an OpenAPI document

 

This chapter covers

  • Defining consistent elements with JSON Schema and OpenAPI
  • Sharing components across OpenAPI documents
  • Defining OpenAPI guidelines

We may have hesitated between an integer and a string when modeling a product ID and used one or the other in various path and query parameters or properties in body data models in our OpenAPI document. We will need to update each instance of the many 500 errors if we intend to add an Error-Id response header after describing numerous operations.

Authoring OpenAPI documents can lead to cumbersome information duplication and, more worryingly, API design inconsistency. Additionally, unoptimized OpenAPI documents that don’t use all of the format’s possibilities are more complex to author and maintain, leading to more inconsistencies. By using OpenAPI and JSON Schema extensively, we can optimize our OpenAPI documents to reduce the risk of inconsistencies and facilitate our work. That must be done separately from the first pass through the design so we don’t mix design- and OpenAPI-related discussions. However, with experience, we’ll be able to introduce optimization, speed up authoring, and ensure consistency from the start.

17.1 An overview of OpenAPI document optimization

17.2 Defining consistent data models

17.2.1 Reusing schemas

17.2.2 Defining subschemas

17.2.3 Targeting part of a schema with a deep reference

17.2.4 Overriding descriptions when using a $ref

17.2.5 Creating unique read-and-write models

17.2.6 Defining a complete schema from its summary

17.2.7 Considering schema optimizations

17.3 Defining consistent parameters

17.3.1 Using path-level parameters

17.3.2 Reusing parameters

17.3.3 Defining reusable groups of query parameters

17.4 Defining consistent request bodies

17.5 Defining consistent responses

17.5.1 Reusing response headers

17.5.2 Reusing responses

17.6 Ensuring cross-API consistency with external shared components

17.6.1 Defining a library of reusable components

17.6.2 Using a shared component in an API