Skip to content

Add support for marshmallow-oneofschema polymorphic schemas (#1012) - #1082

Closed
ansh-rohilla wants to merge 1 commit into
marshmallow-code:devfrom
ansh-rohilla:feat/support-marshmallow-oneofschema-1012
Closed

ansh-rohilla wants to merge 1 commit into
marshmallow-code:devfrom
ansh-rohilla:feat/support-marshmallow-oneofschema-1012

Conversation

@ansh-rohilla

Copy link
Copy Markdown

Summary

This PR adds support for polymorphic schemas defined with marshmallow-oneofschema.OneOfSchema in MarshmallowPlugin, addressing #1012.

Context & Design

marshmallow-oneofschema multiplexes schemas based on an object type discriminator field (type_field) and mapping (type_schemas). Previously, apispec had no handling for OneOfSchema, translating it to an empty object schema with empty properties and dropping subschemas.

Following the discussion in #1012:

  • Availability of marshmallow-oneofschema is checked conditionally at module import time (ONEOFSCHEMA_AVAILABLE). When unavailable, apispec behaves as before without requiring an external runtime dependency.
  • In OpenAPIConverter.schema2jsonschema, when schema is an instance or subclass of OneOfSchema, it delegates to _oneof_schema2jsonschema.
  • Each subschema in type_schemas is resolved through self.resolve_nested_schema, registering named subschemas in components.schemas and generating references for oneOf.
  • A discriminator object is generated with propertyName set to type_field and mapping associating each discriminator type key with its schema reference.
  • Class Meta options title and description are preserved when defined on the schema.
  • Nested OneOfSchema (e.g. inside fields.Nested) and anonymous inline schemas (when schema_name_resolver returns None) are supported.
  • marshmallow-oneofschema is added to the tests dependency group in pyproject.toml and locked in uv.lock.

Validation

  • All 655 test cases pass (pytest).
  • Verified OpenAPI 3.0.x and 3.1.x generated specs validate against the OpenAPI schema via openapi-spec-validator.
  • All pre-commit hooks pass (ruff, ruff format, mypy, zizmor, blacken-docs, uv-lock).
  • Added changelog entry in CHANGELOG.rst.

Closes #1012

…low-code#1012)

Support polymorphic schemas defined with marshmallow-oneofschema.OneOfSchema in MarshmallowPlugin:
- Check for marshmallow-oneofschema availability at import time in OpenAPIConverter.
- Convert OneOfSchema instances and classes to OpenAPI 3 oneOf schemas with a discriminator object mapping type names to component schema references.
- Preserve schema title and description from class Meta when present.
- Support nested OneOfSchema instances and anonymous/inline schemas.
- Add unit and integration tests covering OneOfSchema conversion and OpenAPI validation.
- Add marshmallow-oneofschema as an optional test dependency in pyproject.toml and uv.lock.
@sloria

sloria commented Oct 13, 2026

Copy link
Copy Markdown
Member

please see our AI policy

@sloria sloria closed this Oct 13, 2026
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.

apispec ignores oneOf from marshmallow-oneofschema

2 participants