Utilities for post-processing OpenAPI specs generated by FastAPI/Pydantic.
Pydantic v2 inlines discriminated unions (oneOf + discriminator) at
every usage site. When the union is recursive (e.g. Container.children
references ResourceDataModels which includes Container), documentation
renderers like Redoc and Swagger UI choke on the circular inline schemas.
The single public function :func:deduplicate_discriminated_unions fixes this
by extracting repeated inline unions into named $ref schemas.
Functions¶
deduplicate_discriminated_unions(spec: dict) ‑> dictExtract repeated inline discriminated unions into named schemas.
Walks an OpenAPI spec dict and:
Finds all inline discriminated unions (objects with both
oneOfanddiscriminatorkeys whoseoneOfentries are all$refs).Groups them by fingerprint (the sorted set of
$reftargets).For groups with 2+ occurrences, creates a named schema under
components/schemasand replaces every inline occurrence with a single$ref.
The spec is mutated in place and also returned for convenience.