The OpenAPI Code Generation Guide: Clients, Servers, Types
Quick answer: OpenAPI code generation turns a contract into typed clients, server stubs, or models, so the types your code compiles against are the same types the service serves. The direction that pays off is spec-first: author the OpenAPI, generate the client and stubs, and never hand-edit the generated output. The reverse direction — generating a spec from running code — is useful for recovering a legacy service, but it is a starting point to verify, not a substitute for design.
This guide covers both directions, the naming decisions that make generated code usable, and the ways generated types silently drift or get hollowed out with any. Follow the links for the concrete setups.
All articles in this guide
Each article below is a focused, pain-point-driven read that belongs to this hub. New ones are added on a regular schedule; the ones still in progress are marked.
Committed Generated Types Silently Drift from the Spec
Generated types checked into the repo fall behind the spec. Regenerating in CI instead.
Read the article →The Agent Adds Types That Are Mostly "any"
New interfaces that compile while everything stays any. How to tell real types from decorative ones.
Read the article →The Agent Fixed a TypeScript Error with "as any"
The fastest way to silence tsc is also the one that throws away the type. Better fixes, in order.
Read the article →Typecheck Green, but the Runtime Shape Was Never Validated
Types pass but the live payload has a different shape. Validating runtime data, not just tsc.
Read the article →Generate a TypeScript Client from OpenAPI
Generating a typed client from the contract, and what to do when the spec is incomplete.
Read the article →Generate OpenAPI from Express and NestJS Code
Recovering a spec from running Express and NestJS code without hand-writing it.
Read the article →AST vs AI Reverse Engineering: The Honest Gaps
Static analysis gets the structure; AI fills descriptions. What each still misses.
Read the article →int64, bigint, and Decimal Money Numbers
Large integers and money break JS number precision. Modeling them safely.
Read the article →Generated Enums Break When the API Adds a New Value
Generated enums fail on new values; distinguish closed state machines from open categories and preserve unknown strings.
Read the article →Codegen vs additionalProperties: When Free-Form Maps Break Typed Clients
Keep map keys open but pin the value type, and verify the generated client preserves it.
Read the article →Codegen Turned Every date-time into a String and Nobody Parsed It
Why string date-time fields cause timezone and ordering bugs, and how to map them to real types.
Read the article →Codegen on oneOf: Discriminated Unions and Where Clients Flatten Them
How discriminators produce tagged unions and where generators flatten them into permissive objects.
Read the article →Codegen and readOnly/writeOnly: Why Create Models Include Server Fields
Why readOnly and writeOnly should split create models so clients never fake server-owned fields.
Read the article →Codegen and Circular References: Where Generated Types Recurse or Break
How generators handle circular OpenAPI references, where they fall back to any, and how to validate recursive output.
Read the article →Generated Clients and Error Responses: Typing 4xx/5xx Bodies Instead of Throwing Blind
Configuring codegen so documented 4xx and 5xx bodies become typed errors rather than generic exceptions.
Read the article →Codegen for Binary Downloads: Streaming a File Response Instead of Decoding It as Text
Choosing blob, byte, and stream return types so binary downloads survive.
Read the article →When Codegen Ignores format: uuid, email, and uri and Gives You a Plain String
Why codegen turns format hints into plain strings and how to validate UUIDs, emails, and URIs.
Read the article →The two directions, and why they differ
Generating code from a spec is a build step; generating a spec from code is a recovery step. Conflating them is where teams get into trouble. The distinction is argued in generating code from the spec, not reverse engineering it. In a spec-first workflow, the contract is reviewed and versioned, and the generated code is a disposable artifact; in a code-first workflow, the generated spec is a draft that must be checked for things the framework cannot infer.
Generating a typed client
The most common generation target is a client that encodes every operation, parameter, and response type, so calling the wrong shape is a compile error. The TypeScript case is in generate a TypeScript client from OpenAPI. For the generated names to be stable and sensible, the contract metadata has to be right; operationId and tag naming conventions explains how codegen turns those into function names.
Recovering a spec from running code
When the service already exists and there is no contract, generate a draft from the framework's routes and then verify it against real traffic. The routes differ by stack: generate OpenAPI from Express and NestJS, and generate OpenAPI from FastAPI and Django REST, with the general approach in generate OpenAPI from existing code. The honest limits of automated recovery — where a tool or an AI cannot know the intended constraints — are in AST vs AI reverse engineering and the gaps.
Scanning without hallucinating
AI can accelerate recovery, but it also invents endpoints and fields that do not exist. The safe approach is to derive structure from the actual code and let the AI only fill in descriptions, then verify every operation against the running service. The method is in scan code into OpenAPI without hallucinating, and the full workflow of turning that result into tests and mocks is in from a scanned spec to mocks, tests, and an MCP server.
The silent failures of generated code
Generated code only helps if it stays in sync and keeps its types. Three failure modes recur and are worth a CI check:
- Generated types silently drift from the spec — someone hand-edits output or skips regeneration
- Types are added but mostly
any, so the safety is cosmetic - A type error is fixed by adding
as any, which silences the compiler instead of fixing the shape
Even a green typecheck is not proof of runtime correctness; typecheck green while the runtime value was never validated is the gap between compile-time and runtime. Numeric edge cases also need explicit handling in generated clients; int64, bigint, and decimal money covers the values that overflow a JS number.
Where to go next
Powerduck is the local-first, OpenAPI-first workspace that carries this whole flow — from the contract through debug, test, mock, and the tools an agent can call. See it on the Powerduck homepage.
Code generation is downstream of a good contract. Return to the OpenAPI guide for the modeling decisions, and verify the generated client with the API testing guide. When you want the same contract to drive AI agents rather than only typed code, read the MCP server guide.
Frequently asked questions
Should I commit generated code?
Either regenerate in CI and leave it out of the commit, or commit it and fail the build on a diff — the key is one authoritative source. Never hand-edit generated files, because the next regeneration overwrites them.
Why are my generated types full of any?
Usually because the schema uses free-form or untyped objects, or the generator is configured loosely. Tighten the schema with explicit types and additionalProperties before blaming the generator.
Can codegen replace writing tests?
No. It guarantees the types match the contract; it does not verify the business behavior. Keep scenario tests on top of the generated client.