OpenAPI
Routa treats OpenAPI as a projection of the route contract rather than a document you maintain by hand. It works in both directions: scaffold source from an existing document once, then generate and check the document from source forever after.
openapi.yaml/json -> schemas -> route files -> handler stubs (once, via scaffold)routes -> OpenAPI -> baseline checks (every build)How It Works
Section titled “How It Works”Generation reads validated route metadata and each route file’s contract. For every
method it produces an operation containing the parameters and request body from input,
a response per entry in responses, plus every middleware rejects outcome in that
method’s chain. An outcome’s description becomes the response description; without one,
Routa uses the outcome name, so notFound becomes “Not found”. An outcome without a
schema is bodyless and its response has no content. An outcome’s headers become
Header Objects on its response, each with the header’s schema, required for a required
header, and the schema’s description. Header schema metadata such as examples stays on
the Header Object’s schema. An outcome’s cookies become one standard Set-Cookie Header
Object whose description lists each cookie’s name and static attributes and says each is a
separate repeated field. The header is required when the outcome declares a required cookie,
and the description then names the required cookies. It never carries a value, an example, or a vendor extension.
Middleware rejects are documented exactly like route outcomes: description, body, headers,
and cookies. Middleware openapi metadata contributes security and x-routa-authz, and
route deprecation metadata marks the operation deprecated.
Authentication
Section titled “Authentication”When src/routa.ts configures authentication,
components.securitySchemes declares each scheme under its name, in priority order:
| Descriptor | Security Scheme Object |
|---|---|
bearer() |
{ type: "http", scheme: "bearer" } |
apiKey({ header }) |
{ type: "apiKey", in: "header", name: header } |
sessionCookie({ cookie }) |
{ type: "apiKey", in: "cookie", name: cookie } |
custom({ openapi }) |
openapi, as written |
Every operation documents its security as separate alternatives, one per accepted scheme, in
priority order. OpenAPI reads them with OR semantics: any one of them suffices. With
userToken, session, and serviceKey configured:
| Operation | security |
401 outcomes |
|---|---|---|
Without requireAuth |
[{}, { userToken: [] }, { session: [] }, { serviceKey: [] }] |
authenticationFailed |
Behind requireAuth() |
[{ userToken: [] }, { session: [] }, { serviceKey: [] }] |
authenticationFailed and authenticationRequired |
Behind requireAuth({ schemes: ["session"] }) |
[{ session: [] }] |
authenticationFailed and authenticationRequired |
An operation’s requirement comes from every requireAuth on its chain, including guards a
folder middleware.ts exports, so a folder guard sets the security of every operation below
it. Inherited, route, and method guards intersect: a method’s
requireAuth({ schemes: ["session"] }) under a folder’s requireAuth() documents
[{ session: [] }], and no descendant can add an alternative its folder excludes.
The empty requirement {} says the operation also accepts requests without a credential.
Each 401 outcome documents its problem body and a required WWW-Authenticate header, and
the two compose under one status like any other outcomes. Routa places each problem body in
components.schemas as RoutaAuthenticationFailed, RoutaAuthenticationRequired, or
RoutaAmbiguousCredentials and
refers to it, so a discriminator mapping can select it beside any other component under the
same status. When a Routa body is one of the alternatives, the problem type is the
discriminator if it qualifies:
{ "oneOf": [ { "$ref": "#/components/schemas/RoutaAuthenticationFailed" }, { "$ref": "#/components/schemas/RoutaAuthenticationRequired" } ], "discriminator": { "propertyName": "type", "mapping": { "https://routa-ts.dev/problems/authentication-failed": "#/components/schemas/RoutaAuthenticationFailed", "https://routa-ts.dev/problems/authentication-required": "#/components/schemas/RoutaAuthenticationRequired" } }}A schema of yours that claims one of these component names fails with
ROUTA_SCHEMA_COMPONENT_COLLISION. With
multipleCredentials: "reject", an operation that accepts two or more schemes also documents
ambiguousCredentials as its 400 problem body, without a challenge; an operation that
accepts one scheme cannot return it, so it does not list it. A HEAD operation documents each
status as one bodyless response. Middleware openapi.security still applies: Routa enforces
authentication in addition to your middleware, so each middleware requirement combines with
each authentication alternative. [{ tenantKey: [] }] on a public operation becomes
[{ tenantKey: [] }, { tenantKey: [], userToken: [] }, { tenantKey: [], session: [] }, { tenantKey: [], serviceKey: [] }].
Outcomes That Share a Status
Section titled “Outcomes That Share a Status”OpenAPI has one response per status, so outcomes that share a status are composed into one.
The outcomes considered are the method’s own responses and the rejects of every middleware
in its chain. Composition never depends on the order they are written in:
- Body outcomes compose when every one of them has a required property with a single
string, number, or boolean literal value (
z.literal(...)or a one-valuez.enum([...])) and those values all differ. Values are compared as text, the way OpenAPI discriminator values are, so1and"1"do not differ. Routa reads the property through component references and intersections (z.intersection()or.and()). The response schema becomes aoneOfof the outcome schemas, ordered by outcome name, with that property asdiscriminator.propertyName. When several properties qualify, the alphabetically first name is the discriminator, except thattypewins when one of the outcomes is a Routa authentication outcome. - When an outcome schema is a referenced component whose name differs from its discriminator
value, such as
Createdforkind: "created", the discriminator gets amappingfrom each value, as text, to its component. Components are never renamed to match values. - Without such a property,
routa checkfails withROUTA_RESPONSE_STATUS_AMBIGUOUS. Add a literal discriminator or give each outcome its own status. Routa never falls back toanyOf. - A body outcome and a bodyless outcome cannot share a status, and neither can two bodyless outcomes; clients could not tell them apart.
- A header declared by several of the outcomes must have the same schema, metadata included
(
ROUTA_RESPONSE_HEADER_INCOMPATIBLE). Schemas are compared by content, so a component reference and an equal inline schema agree. The header is projected once, as the first outcome in name order writes it, and isrequiredonly when every outcome under the status requires it. - A cookie identity listed by several of the outcomes must come from the same
createCookiehandle (ROUTA_RESPONSE_COOKIE_CONFLICT). TheSet-Cookiedescription lists it once, as always set or cleared only when every outcome requires it. TheSet-Cookieheader isrequiredwhen every outcome requires some cookie, even when they require different ones. - The response description lists each outcome’s description in outcome-name order, one
Markdown list item per outcome:
- `created`: The project was created.
import { createRoute, createRouteRoot } from "@routa-ts/core";import { z } from "zod";
export default createRouteRoot("/projects")({ post: createRoute({ responses: { created: { status: 201, schema: z.object({ kind: z.literal("created"), id: z.string() }) }, queued: { status: 201, schema: z.object({ kind: z.literal("queued"), ticket: z.number() }) }, }, run: ({ response }) => response.created({ kind: "created", id: "p_1" }), }),});The 201 response above documents oneOf: [created, queued] with
discriminator: { propertyName: "kind" }.
The document comes from source alone. Operations, parameters, request bodies, responses,
components, security, and generated operationId values never come from
.routa/openapi-baseline.json, the contract your team has accepted. The baseline is only
what the document is compared against, and nothing is written to it unless you ask.
Every operation gets an operationId made from its method and path, so GET /users/{id}
is getUsersId. The document info is always Routa API at version 0.0.0, and no
servers are emitted.
Checking the Contract
Section titled “Checking the Contract”routa openapi checkOpenAPI check passed. No drift detected.openapi check fails when an operation, a response status, a parameter, a request body, a
schema (including the content of a component), or an operation’s security differs from
the baseline. Descriptions, operationId, info, and servers are not compared, except
the generated Set-Cookie header description, which lists cookie names and attributes.
Removing a response status or an operation’s authentication from source is drift, because the
current document no longer carries them over from the baseline. openapi breaking is
narrower: it fails only for removed operations, for inputs that became required, and for
operations whose security refuses credentials the baseline accepted, so it is the check to
run in CI on every pull request. Each requirement in security is one accepted set of
credentials, and the empty requirement {} accepts a request without any. Adding
authentication to an application flags only the operations behind requireAuth, including
ones that already required an application credential such as a tenant key.
OpenAPI to Source
Section titled “OpenAPI to Source”Scaffolding accepts .yaml, .yml, and .json documents and generates route files,
schema modules, and the initial .routa/ metadata. Generated source is meant to be
reviewed, edited, and committed — Routa does not ask you to trust a generated contract
blindly.
Supported input is deliberately narrow:
- HTTP paths and methods
operationId- path, query, header, and cookie parameters
- JSON request bodies
- JSON responses
- reusable schemas from
components.schemas
Anything else stays in the source document untouched rather than being approximated in TypeScript.
Generated Extensions
Section titled “Generated Extensions”Routa adds three vendor extensions so information that has no standard field is still visible in the document:
| Extension | Meaning |
|---|---|
x-routa-authz |
Permission strings declared by middleware openapi.permissions |
x-routa-diagnostics |
Validation errors that prevented a complete document |
x-routa-schema-warnings |
Input Routa could not describe, as ROUTA_SCHEMA_OPAQUE_INPUT |
A document containing x-routa-diagnostics means generation ran against a project that
does not validate. Fix the diagnostics before trusting the output.
Limits
Section titled “Limits”Source has no place for a title, version, or servers yet, so info is a fixed default
and servers is omitted. Editing them in the baseline changes nothing, because
--update-baseline writes the generated document over it. components.schemas comes from the exported and
.meta({ id }) schemas your routes use; see Schemas. The checks
compare schemas by content, so a schema written inline in the baseline and as a $ref in
the generated document is not drift. Content types other than JSON are not modeled in v0.