Repository navigation
feat(openapi): make OpenAPI output version configurable (3.2 / 3.1) - #218
Merged
Merged
Conversation
Kilerd
force-pushed
the
feat/openapi-output-version
branch
from
December 3, 2026 10:36
d982823 to
f2dff63
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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-openapiand a streaming/SSE + binary fixture) vs its 3.1.0 counterpart: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.tsoutput to 3.1.0 (diffempty). IgnoresitemSchema(typestext/event-streamasunknown) and handlescontentMediaType/contentSchemacleanly asstring.npx openapi-typescript@7 spec-3.1.json -o out-3.1.d.ts: accepted (EXIT=0,21.6ms).gotcha/statics/redoc.html, Redoc2.0.0, rev5fb4daa)Error: Unsupported OpenAPI Version: 3.2.0atdetectOpenAPI(only3.0*,3.1*, and Swagger2.0are accepted).rendered HTML length: 55365/32230, contains/pets).@scalar/openapi-parser, underlying@scalar/api-reference)itemSchema,contentMediaType,contentSchema): accepted (valid: true, version: "3.2", errors: []).itemSchemastill present: rejected (valid: false, version: "3.1", errors: ["Property itemSchema is not expected to be here"]).itemSchemaomitted (keepingcontentMediaType/contentSchema): accepted (valid: true, version: "3.1", errors: []).Design and Tradeoffs
Public API
#[non_exhaustive]enumOpenApiVersion(V3_1,#[default] V3_2) withas_str()andDisplay, re-exported fromgotcha::openapi,gotcha, andgotcha::preludebehind#[cfg(feature = "openapi")]..openapi_version(OpenApiVersion)onGotchaandGotchaRouter, and default methodGotchaApp::openapi_version()for builder/trait parity.into_openapi()/openapi_document()) and/openapi.jsonhonour the configured version.nestandmergekeep the parent's version, and the last call wins (matchingopenapi_infoanddocument_extractor_errors).OpenAPI 3.2 → 3.1 Conversion Rules
Audited OpenAPI 3.2.0 additions against
oas 0.3and Gotcha's generated output:openapi"3.2.0"→"3.1.0"(set before.openapi(..)transforms and enforced forV3_1).^3\.1\.\d+(-.+)?$).MediaType.itemSchemaNone) after.openapi(..)transforms across paths, operations, parameters, request bodies, responses, headers, callbacks, and components. Item-only component schemas (not registered outsideitemSchema) are pruned unless referenced anywhere in the final document or by any retained component.itemSchemais 3.2-only and disallowed by the OpenAPI 3.1.0 schema. Omitting it rather than promoting it toschemakeeps the contract truthful (itemSchemadescribes one streamed item, not the whole body).$selfOpenAPIV3.extrasafter.openapi(..)transforms if present.discriminator.defaultMappingdiscriminatorobjects after.openapi(..)transforms.xml.nodeTypexmlobjects after.openapi(..)transforms.contentMediaType,contentEncoding,contentSchema(e.g.Binary, SSEdata),anyOfnullability, numericexclusiveMinimum/exclusiveMaximum, schemaexamples,$refsiblings,prefixItems,unevaluatedPropertieshttps://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/serializedValueoas 0.3.oas 0.3and never emitted by Gotcha.Key Tradeoffs
itemSchemavs promoting it toschema: PromotingitemSchematoschemafortext/event-streamorapplication/jsonlwould claim the entire HTTP body is a single event object rather than a stream. OmittingitemSchemastays accurate and handles both item-only andbody + itemdeclarations uniformly.$ref(e.g. when an overridden inferred response or transform-added component references a schema also used as an SSEitem). In addition, schemas registered outsideitemSchemascopes (such as customResponsibleimplementations registering types for client codegen) are excluded from item-only pruning regardless of route order.spec.openapiis 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.OpenApiVersion::V3_1does not implicitly mount/redoc; applications opt in viaOpenApiEndpoints.Testing
gotcha/src/openapi/version.rsfor version strings,Display,Default, and stripping 3.2-only fields (itemSchema,$self, top-level and nesteddiscriminator.defaultMapping, top-level and nestedxml.nodeType) while preserving 3.1 keywords (contentMediaType,contentEncoding,contentSchema).gotcha/tests/openapi_version.rs:fixtures/openapi_version_3_2.json) and 3.1.0 (fixtures/openapi_version_3_1.json).https://spec.openapis.org/oas/3.1/schema/2022-10-07) validation viajsonschema, JSON Schema Draft 2020-12 meta-schema validation on every component, recursive dangling-$refverification, and negative controls rejecting 3.2.0 and 3.1.0 withitemSchema.application/jsonlwithbody+item) and SSE (text/event-streamwithitemonly) behaviour, component pruning/retention, andBinary/contentMediaType/contentSchemapreservation.$refprevention 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).$self, transform-addeditemSchema, nesteddiscriminator.defaultMapping, and nestedxml.nodeType.Gotcha::into_openapi(),GotchaApp::openapi_document(), and served/openapi.json, plus Redoc endpoint serving.README.md(with compiled doctest) andCHANGELOG.md([Unreleased]).Backward Compatibility
Fully backward compatible:
3.2.0, unchanged from 0.5.3.generate_openapi,generate_openapi_with_configs_and_info,into_openapi, andGotchaAppimplementations require no changes (GotchaApp::openapi_versionhas a default implementation returningOpenApiVersion::V3_2).0.5.4) on its own, or as part of0.6.0alongside breaking changes.Known Limitations
{}in OpenAPI 3.1: Because OpenAPI 3.1 has noitemSchemakeyword and promoting per-item schemas to whole-bodyschemawould misrepresent streamed payloads, endpoints declaring onlyitem = "..."(such as SSEtext/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 defaultOpenApiVersion::V3_2.oas 0.3downgrade coverage: Downgrade traversal covers every 3.2-only field representable inoas 0.3plus JSON Schema extras ($self,discriminator.defaultMapping,xml.nodeType). Upgradingoasto0.4+(which adds typed fields for more 3.2 constructs) will require extendingdowngrade_*ingotcha/src/openapi/version.rs.