awesome-everything RU
↑ Back to the climb

APIs

OpenAPI: spec and diff reading

Crux Read real OpenAPI YAML fragments, a oasdiff verdict, and a generated client, then pick the highest-leverage fix to keep the contract honest.
Your altitude — climbing toward senior
ZeroJuniorMiddleSenior
You are at senior altitude — in orbit
◷ 14 min

Contract problems are caught in the spec file, the diff output, and the generated client. Read each fragment, predict what it does to consumers, and choose the fix a senior engineer makes first.

Goal

Practise the loop every API change runs through: read the schema, predict the breaking-change and codegen consequences, and reach for the fix that keeps the spec load-bearing.

Snippet 1 — the loose schema

components:
  schemas:
    Order:
      type: object
      properties:
        id: { type: string }
        items:
          type: object          # untyped blob
        total: {}               # no type at all
      additionalProperties: true # anything else is allowed
      # no `required:` array
Quiz

A generator turns this Order schema into a client. What will the generated type look like, and what is the fix?

Snippet 2 — the oasdiff verdict

$ oasdiff breaking main.yaml pr.yaml
1 breaking changes: 1 error, 0 warning
error  api-required-request-property-added
       in API POST /orders
       added the required request property 'tenantId'
Quiz

The PR adds tenantId as a required field on POST /orders and oasdiff reports this in CI. What should the pipeline do, and what is the compatible alternative?

Snippet 3 — the 3.0 to 3.1 nullability rewrite

# OpenAPI 3.0
status:
  type: string
  nullable: true

# OpenAPI 3.1 (JSON Schema 2020-12)
status:
  type: [string, "null"]
Quiz

You migrate the spec to 3.1 and rewrite nullability like this. What must you watch for in tooling and diffs?

Snippet 4 — code-first annotations

class CreateOrder(BaseModel):
    id: str
    items: list[Item]
    coupon: str | None = None   # optional, nullable

# FastAPI generates the OpenAPI spec from this model.
# A later edit makes coupon required:
    coupon: str                 # now required, no default
Quiz

In a code-first setup, an engineer makes coupon required by removing its default. The generated spec updates automatically. What is the risk, and what closes it?

Recap

Every contract change is read in three places: the schema (loose types and open additionalProperties produce any-riddled clients — tighten with explicit types, required arrays, and $ref), the diff (oasdiff classifies adding a required field, removing a response field, or narrowing a type as breaking — fail the PR and migrate compatibly), and the generation direction (code-first stays in sync with code but not with consumers, so it needs the same gate spec-first does). OpenAPI 3.1 moves nullability into the type array and aligns with JSON Schema 2020-12 — verify tool support before you migrate. Read the artifact, predict the consumer impact, then fix at the contract level.

Continue the climb ↑OpenAPI: make the contract load-bearing
shortcuts expand
search
K
prev piece
k
next piece
j
cycle tier
t
this menu
?
sources3
expand
  1. 01
  2. 02
  3. 03

Trademarks belong to their respective owners. Editorial reference only.