Skip to content

fix(marshmallow): serialize OneOf and Equal validator choices to OpenAPI enum (closes #679) - #1085

Closed
saifmodan2006 wants to merge 1 commit into
marshmallow-code:devfrom
saifmodan2006:fix/oneof-validator-choices
Closed

saifmodan2006 wants to merge 1 commit into
marshmallow-code:devfrom
saifmodan2006:fix/oneof-validator-choices

Conversation

@saifmodan2006

Copy link
Copy Markdown

Summary

Fixes #679.

When using marshmallow fields with \�alidate.OneOf\ or \�alidate.Equal\ constraints containing deserialized objects (such as \Enum\ instances, \datetime.date, \datetime.datetime, \uuid.UUID, \datetime.timedelta, etc.), \ ield2choices\ was previously inserting the raw Python objects directly into the OpenAPI \enum\ list. This resulted in non-serializable objects and broke JSON schema generation.

Changes

  1. Added _serialize_choice(field, choice)\ helper in \FieldConverterMixin:
    • Serializes choices using \ arget_field._serialize(choice, None, None)\ (using \ ield.inner\ when \ ield\ is a \ ields.List).
    • Ensures the serialized choice is hashable and JSON-compatible, falling back safely to \choice\ if serialization is not supported or raises an exception.
  2. Updated \ ield2choices:
    • Serializes validator choices for both \�alidate.OneOf\ (\�alidator.choices) and \�alidate.Equal\ (\�alidator.comparable).
  3. Updated \enum2properties:
    • Preserves restricted \enum\ lists populated by \ ield2choices\ from field validators rather than unconditionally overwriting them with all enum members.
  4. Added comprehensive regression tests in \ ests/test_ext_marshmallow_field.py\ covering choice serialization for dates, datetimes, UUIDs, Equal validators, labels, and Enum fields with OneOf restrictions.

@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.

Generating labels of validate.OneOf(labels=[..]) in the json schema

2 participants