Skip to content

feat(openapi): make OpenAPI output version configurable (3.2 / 3.1) - #218

Merged
Kilerd merged 2 commits into
mainfrom
feat/openapi-output-version
Dec 3, 2026
Merged

Kilerd merged 2 commits into
mainfrom
feat/openapi-output-version

Conversation

@Kilerd

@Kilerd Kilerd commented Dec 3, 2026 •

Copy link
Copy Markdown
Owner

Motivation

Generated OpenAPI documents currently hardcode "openapi": "3.2.0" and emit OpenAPI 3.2 streaming fields (MediaType.itemSchema). Downstream tooling and viewers that only accept OpenAPI 3.1 — including the bundled Redoc UI and strict 3.1 validators — reject 3.2 documents. Allowing applications to opt into OpenAPI 3.1.0 output lets them interoperate with 3.1-only toolchains while keeping 3.2.0 as the default.

Tooling Compatibility Verification

Verified against a generated 3.2.0 spec (from cargo run -p openapi -- --export-openapi and a streaming/SSE + binary fixture) vs its 3.1.0 counterpart:

  1. openapi-typescript@7 (v7.13.0)
    • npx openapi-typescript@7 spec-3.2.json -o out-3.2.d.ts: accepted (EXIT=0, 24.2ms); emits identical .d.ts output to 3.1.0 (diff empty). Ignores itemSchema (types text/event-stream as unknown) and handles contentMediaType/contentSchema cleanly as string.
    • npx openapi-typescript@7 spec-3.1.json -o out-3.1.d.ts: accepted (EXIT=0, 21.6ms).
  2. Bundled Redoc (gotcha/statics/redoc.html, Redoc 2.0.0, rev 5fb4daa)
    • 3.2.0: rejected — Error: Unsupported OpenAPI Version: 3.2.0 at detectOpenAPI (only 3.0*, 3.1*, and Swagger 2.0 are accepted).
    • 3.1.0: accepted — renders cleanly (rendered HTML length: 55365 / 32230, contains /pets).
  3. Scalar (@scalar/openapi-parser, underlying @scalar/api-reference)
    • 3.2.0 (including itemSchema, contentMediaType, contentSchema): accepted (valid: true, version: "3.2", errors: []).
    • 3.1.0 with itemSchema still present: rejected (valid: false, version: "3.1", errors: ["Property itemSchema is not expected to be here"]).
    • 3.1.0 with itemSchema omitted (keeping contentMediaType / contentSchema): accepted (valid: true, version: "3.1", errors: []).

Design and Tradeoffs

Public API

  • Added #[non_exhaustive] enum OpenApiVersion (V3_1, #[default] V3_2) with as_str() and Display, re-exported from gotcha::openapi, gotcha, and gotcha::prelude behind #[cfg(feature = "openapi")].
  • Added .openapi_version(OpenApiVersion) on Gotcha and GotchaRouter, and default method GotchaApp::openapi_version() for builder/trait parity.
  • Both offline export (into_openapi() / openapi_document()) and /openapi.json honour the configured version.
  • Application-level setting: nest and merge keep the parent's version, and the last call wins (matching openapi_info and document_extractor_errors).

OpenAPI 3.2 → 3.1 Conversion Rules

Audited OpenAPI 3.2.0 additions against oas 0.3 and Gotcha's generated output:

Field / Keyword 3.2 → 3.1 Behavior Rationale
openapi "3.2.0" → "3.1.0" (set before .openapi(..) transforms and enforced for V3_1). Required by OpenAPI 3.1.0 (^3\.1\.\d+(-.+)?$).
MediaType.itemSchema Omitted (None) after .openapi(..) transforms across paths, operations, parameters, request bodies, responses, headers, callbacks, and components. Item-only component schemas (not registered outside itemSchema) are pruned unless referenced anywhere in the final document or by any retained component. itemSchema is 3.2-only and disallowed by the OpenAPI 3.1.0 schema. Omitting it rather than promoting it to schema keeps the contract truthful (itemSchema describes one streamed item, not the whole body).
Root $self Removed from OpenAPIV3.extras after .openapi(..) transforms if present. Introduced in 3.2.0; disallowed in 3.1.0.
discriminator.defaultMapping Recursively removed from schema discriminator objects after .openapi(..) transforms. Introduced in 3.2.0; disallowed in 3.1.0.
xml.nodeType Recursively removed from schema xml objects after .openapi(..) transforms. Introduced in 3.2.0; disallowed in 3.1.0.
contentMediaType, contentEncoding, contentSchema (e.g. Binary, SSE data), anyOf nullability, numeric exclusiveMinimum/exclusiveMaximum, schema examples, $ref siblings, prefixItems, unevaluatedProperties Preserved unchanged. Standard JSON Schema Draft 2020-12 keywords explicitly enabled by OpenAPI 3.1's https://spec.openapis.org/oas/3.1/dialect/base (vocab/content: true).
in: querystring, PathItem.query/additionalOperations, components.mediaTypes, Tag.summary/parent/kind, Encoding.itemEncoding/prefixEncoding, SecurityScheme.deprecated/oauth2MetadataUrl, OAuthFlows.deviceAuthorization, Example.dataValue/serializedValue Not applicable on oas 0.3. Not modeled by oas 0.3 and never emitted by Gotcha.

Key Tradeoffs

  1. Omitting itemSchema vs promoting it to schema: Promoting itemSchema to schema for text/event-stream or application/jsonl would claim the entire HTTP body is a single event object rather than a stream. Omitting itemSchema stays accurate and handles both item-only and body + item declarations uniformly.
  2. Safe item-only component pruning: Reachability during pruning is seeded from both non-schema parts of the document and every retained component outside the eligible prune set, so pruning can never leave a dangling $ref (e.g. when an overridden inferred response or transform-added component references a schema also used as an SSE item). In addition, schemas registered outside itemSchema scopes (such as custom Responsible implementations registering types for client codegen) are excluded from item-only pruning regardless of route order.
  3. Transform ordering: spec.openapi is set before .openapi(..) transforms so callbacks observe "3.1.0", while 3.2-only field stripping runs after transforms and before final schema pruning so 3.2 fields introduced by transforms ($self, itemSchema, discriminator.defaultMapping, xml.nodeType) do not leak into 3.1 documents.
  4. Redoc remains opt-in: Selecting OpenApiVersion::V3_1 does not implicitly mount /redoc; applications opt in via OpenApiEndpoints.

Testing

  • Unit tests in gotcha/src/openapi/version.rs for version strings, Display, Default, and stripping 3.2-only fields (itemSchema, $self, top-level and nested discriminator.defaultMapping, top-level and nested xml.nodeType) while preserving 3.1 keywords (contentMediaType, contentEncoding, contentSchema).
  • Integration tests in gotcha/tests/openapi_version.rs:
    • Default version remains 3.2.0.
    • Full document snapshot comparisons for both 3.2.0 (fixtures/openapi_version_3_2.json) and 3.1.0 (fixtures/openapi_version_3_1.json).
    • Official OpenAPI 3.1.0 JSON Schema (https://spec.openapis.org/oas/3.1/schema/2022-10-07) validation via jsonschema, JSON Schema Draft 2020-12 meta-schema validation on every component, recursive dangling-$ref verification, and negative controls rejecting 3.2.0 and 3.1.0 with itemSchema.
    • Streaming (application/jsonl with body + item) and SSE (text/event-stream with item only) behaviour, component pruning/retention, and Binary / contentMediaType / contentSchema preservation.
    • Dangling-$ref prevention for overridden inferred responses and transform-added components referencing item-only schemas, plus retention of intentionally registered schemas also used as SSE items (in both registration orders).
    • Post-transform stripping of $self, transform-added itemSchema, nested discriminator.defaultMapping, and nested xml.nodeType.
    • Byte-for-byte parity between Gotcha::into_openapi(), GotchaApp::openapi_document(), and served /openapi.json, plus Redoc endpoint serving.
  • Updated README.md (with compiled doctest) and CHANGELOG.md ([Unreleased]).

Backward Compatibility

Fully backward compatible:

  • Default output remains OpenAPI 3.2.0, unchanged from 0.5.3.
  • Existing generate_openapi, generate_openapi_with_configs_and_info, into_openapi, and GotchaApp implementations require no changes (GotchaApp::openapi_version has a default implementation returning OpenApiVersion::V3_2).
  • Safe to ship as a patch release (0.5.4) on its own, or as part of 0.6.0 alongside breaking changes.

Known Limitations

  • Item-only streaming media types become {} in OpenAPI 3.1: Because OpenAPI 3.1 has no itemSchema keyword and promoting per-item schemas to whole-body schema would misrepresent streamed payloads, endpoints declaring only item = "..." (such as SSE text/event-stream) emit an empty Media Type object {} in 3.1 mode and lose per-item payload metadata. Applications that need streaming item schemas in OpenAPI should stay on the default OpenApiVersion::V3_2.
  • oas 0.3 downgrade coverage: Downgrade traversal covers every 3.2-only field representable in oas 0.3 plus JSON Schema extras ($self, discriminator.defaultMapping, xml.nodeType). Upgrading oas to 0.4+ (which adds typed fields for more 3.2 constructs) will require extending downgrade_* in gotcha/src/openapi/version.rs.

@Kilerd
Kilerd force-pushed the feat/openapi-output-version branch from d982823 to f2dff63 Compare December 3, 2026 10:36
@Kilerd
Kilerd merged commit 594394a into main Dec 3, 2026
13 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant