Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

Module madsci.common.openapi_utils

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) ‑> dict

Extract repeated inline discriminated unions into named schemas.

Walks an OpenAPI spec dict and:

  1. Finds all inline discriminated unions (objects with both oneOf and discriminator keys whose oneOf entries are all $ref s).

  2. Groups them by fingerprint (the sorted set of $ref targets).

  3. For groups with 2+ occurrences, creates a named schema under components/schemas and replaces every inline occurrence with a single $ref.

The spec is mutated in place and also returned for convenience.