OpenAPI 3.1 normalizer (#1)#163
Merged
Merged
Conversation
FastAPI, recent NestJS, spring-doc 3.x and Hono emit OpenAPI 3.1 natively. Our pipeline depends on swagger-parser 10.x which hard-rejects any openapi version other than 3.0.0–3.0.3 — including from dereference(). Customers got 'Unsupported OpenAPI version' and had to patch their bridge to emit 3.0 by hand. Fix: a small normalizer that rewrites the 3.1-only constructs we care about into 3.0 equivalents in place, *before* swagger-parser sees the document, and relabels openapi to 3.0.3 so the parser proceeds: type:[X,'null'] -> type:X + nullable:true anyOf+null branch -> unwrap remaining branch + nullable:true oneOf+null branch -> same const:'x' -> enum:['x'] examples:[...] (array) -> example:examples[0] exclusiveMinimum:<n> -> minimum:<n> + exclusiveMinimum:true exclusiveMaximum:<n> -> maximum:<n> + exclusiveMaximum:true The components.examples map (object form) is left alone. Also forward 'nullable' and 'example' through paramToJsonSchema and flattenSchema so the normalized fields survive into the generated tool schemas (without this, the normalizer's work was lost when the parser flattened parameters).
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.
Problem
FastAPI ≥ 0.100, recent NestJS, spring-doc 3.x and Hono all emit OpenAPI 3.1 natively. Our pipeline depends on swagger-parser 10.x which hard-rejects any `openapi` value other than 3.0.0–3.0.3 — including from `dereference()`. Customers were getting `Unsupported OpenAPI version: 3.1.0` and had to patch their bridges to emit 3.0 by hand (PR #5 in the customer's repo did this).
Fix
A small normalizer that rewrites the 3.1-only constructs we care about into their 3.0 equivalents in place, before swagger-parser sees the document, and relabels `openapi` to `3.0.3` so the parser proceeds.
Rewrites covered:
The `components.examples` map (object, not array) is preserved.
Also forwards `nullable` and `example` through `paramToJsonSchema` / `flattenSchema` so the normalized fields actually survive into the generated tool schemas. Without this, the normalizer's work was being silently dropped when the parser flattened parameters.
Out of scope
Test plan