Skip to content

Lesson 4 of 9 · 24m

Generating Clients and Servers Without Surrendering Design

Use OpenAPI generators for types, validation, routing skeletons, and clients while keeping domain logic and lifecycle ownership outside generated code.

Code generation is a power tool. Used well, it removes repetitive validation and serialization mistakes. Used badly, it creates a repository full of overwritten hand edits, bizarre class names, and a server skeleton nobody understands. The rule is simple: generated code owns shape; humans own behavior.

Pick the generation direction deliberately

There are two common workflows:

spec -> server stubs + models + client SDKs
implementation -> generated spec -> docs and validation

Contract-first work favors the first path. The second can still be useful for legacy systems, but it tends to encode implementation accidents as public truth. Hybrid systems exist, yet one artifact must be canonical. Decide which one.

Generate in CI, not by memory

For TypeScript clients, openapi-typescript plus a small fetch layer is often cleaner than a heavyweight runtime SDK:

npx openapi-typescript openapi.yaml -o src/api/schema.d.ts

For Java services, OpenAPI Generator can produce interfaces and models:

openapi-generator-cli generate   -i openapi.yaml   -g spring   -o build/generated/openapi   --additional-properties=interfaceOnly=true,useSpringBoot3=true

Exact flags differ by generator version, so pin the tool version in the repository. The failure mode you are avoiding is “works on the senior engineer's laptop.”

Keep generated files out of hand-editing reach

Use a layout like:

src/generated/api/      regenerated; never edited
src/domain/             human-owned business logic
src/http/               adapters from generated interfaces to domain services

The adapter layer matters. Generated request objects should not leak into persistence entities or domain events. Otherwise every contract evolution becomes a shotgun refactor through unrelated internals.

Validate generated output against real examples

Generated types are only as useful as the schema. Feed recorded production-like fixtures through serialization and parsing tests. Include edge cases: nulls, unknown enum values, large integers, unicode names, empty arrays, missing optional fields, and deprecated-but-still-returned properties.

Generation is not correctness

A generated server route that accepts valid input does not know whether a customer may read that object. A generated TypeScript client does not know whether a field is sensitive. The contract constrains syntax and broad transport behavior; implementation and policy still decide semantics.

Field notes

  • Pin generator versions and review diffs in generated clients; small generator upgrades can rename methods across every consumer.
  • Do not commit generated code if your build can reproduce it reliably — unless consumers need it vendored for release stability.
  • Generated validators are not a substitute for domain invariants. “maximum 100 items” is schema validation; “cannot order inactive products” is domain validation.
  • If the generated SDK is miserable to use, fix the spec or generator configuration rather than layering handwritten wrappers forever.

Was this lesson helpful?

Sign in to rate