Skip to content

Lesson 2 of 9 · 27m

Modeling OpenAPI Schemas That Survive Real Clients

Design precise schemas for identifiers, enums, money, time, validation errors, and polymorphism so generated clients remain useful instead of merely compileable.

Most schemas fail quietly. They compile, they generate clients, and then production discovers that amount is sometimes a string, status gained a new enum value, or customer can be either an object or null depending on tenant configuration. A schema is not a type annotation for your current server. It is the boundary condition for every future client.

Start with stable identifiers

Use opaque identifiers and say so; consumers should never parse meaning from them. Prefer UUIDv4/UUIDv7, ULIDs, or random URL-safe IDs consistently. Opaque IDs reduce guessing and accidental leakage, but object authorization still belongs server-side.

Money is not a float

This rule has survived decades because teams keep violating it:

Money:
  type: object
  required: [amountMinor, currency]
  properties:
    amountMinor: {type: integer, format: int64, minimum: 0}
    currency: {type: string, minLength: 3, maxLength: 3, example: USD}

A float eventually rounds wrong, serializes wrong, or differs between JavaScript and the backend. Minor units are boring and reliable. If decimal quantities are required, transmit decimal strings with explicit scale semantics.

Time, duration, and timezone truth

Use RFC 3339 UTC timestamps for instants, for example 2026-11-30T18:45:00Z with type: string and format: date-time. A date-time is not enough for scheduling semantics. "Every day at 09:00" needs a timezone and recurrence rules. "Trial lasts 14 days" needs duration or explicit end time. Do not encode business meaning in examples alone.

Validation errors deserve a schema too

Consumers need machine-actionable errors, not a human paragraph:

Problem:
  type: object
  required: [type, title, status]
  properties:
    type: {type: string, format: uri}
    title: {type: string}
    status: {type: integer}
    detail: {type: string}
    invalidParams:
      type: array
      items:
        type: object
        properties:
          name: {type: string}
          reason: {type: string}

This shape follows RFC 7807 closely enough to be familiar while giving clients field-level errors. Make detail safe to display to operators, but never assume it is safe for end users.

Be careful with polymorphism

oneOf and discriminator are useful, but generated clients vary widely in quality. If two shapes are truly different domain objects, consider separate endpoints or explicit type fields with flat variants. Deep inheritance trees in schemas often reflect server implementation convenience, not consumer clarity.

Field notes

  • additionalProperties: false catches drift but can make additive evolution harder for older strict clients; choose deliberately.
  • Enums are contracts too. Consumers need a forward-compatibility policy for unknown values.
  • Examples become tests in practice. Keep them realistic enough that documentation is not misleading.
  • Integer format: int64 matters in JavaScript consumers; values above Number.MAX_SAFE_INTEGER lose precision.

Was this lesson helpful?

Sign in to rate