Skip to content

fix(python): serialize unrecognized open enum values - #145

Open
2ynn wants to merge 1 commit into
mainfrom
qynn/fix/python-unrecognized-enum-serialization
Open

2ynn wants to merge 1 commit into
mainfrom
qynn/fix/python-unrecognized-enum-serialization

Conversation

@2ynn

@2ynn 2ynn commented Oct 23, 2026 •

Copy link
Copy Markdown
Contributor

Why

UnrecognizedStr and UnrecognizedInt hold values of open enums (x-speakeasy-unknown-values: allow) that the SDK does not know. They had no serialization schema, so:

  • dumping a model holding an unknown enum value emitted Pydantic serialization warnings;
  • in an optional nullable open enum field, the unknown value was serialized by the Unset member of the union and then dropped: {"stringOrNull": "future"} was sent as {}.

This affects every Python SDK with open enums and needs no configuration.

What changed

  • Give both types an explicit serializer:
    • Python-mode dumps return the original object, so type(...) is UnrecognizedStr still holds;
    • JSON-mode dumps return a plain str or int;
    • values of another type raise PydanticSerializationUnexpectedValue, so union serialization moves on to the remaining members.
  • Add the open-enum-scalar-unions primary fragment: an open string or integer enum combined with number, boolean or null. The number and boolean cases guard that values of other types still keep their JSON type now that the enum types serialize themselves.
  • Review SDK: only types/basemodel.py changes.

Stack

  1. This PR: unrecognized enum serialization
  2. fix(python): honour include/exclude filters in model dumps #149: honour include/exclude filters in model dumps
  3. feat(python): add preserveUnknownFields option #146: preserveUnknownFields
  4. feat(python): add modelSerializationMethods option #147: modelSerializationMethods

Together these contain the change from #133, split into two fixes and two independent options.

Testing

  • TARGET=primary make test-pythonv2: 679 passed, including the new open-enum cases with warnings raised as errors.
  • With the previous basemodel.py, the unknown-value cases fail: two on warnings, and stringOrNull serializes to {}.
  • TARGET=review make build-pythonv2: integrity validation passes; only types/basemodel.py and gen.lock change.
  • git diff --check passes.

Public-safety check

  • This change contains no credentials, customer documents, private repository URLs, private filesystem paths, or unredacted private logs.
  • Title, body, comments, and commit messages name no customers or customer-derived identifiers, private paths or trackers, or workflow provenance, and are understandable without private context (.claude/skills/public-repo-communication/SKILL.md).
  • Generated fixtures and review SDK changes are public-safe.
  • I reviewed git diff --check.

@github-actions github-actions Bot added the python Trigger (106) snapshot tests for python label Oct 23, 2026

@cubic-dev-ai cubic-dev-ai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No issues found across 6 files

Confidence score: 5/5

  • Automated review surfaced no issues in the provided summaries.
  • No files require special attention.

Shadow auto-approve: would auto-approve. Fixes Python SDK serialization of unrecognized open enum values by adding an explicit serializer that preserves python-mode values and emits plain str/int in JSON, with new tests covering enum-scalar unions; bounded bug fix with tests.

View guided diff | Turn on auto-fix | Re-trigger cubic

@2ynn
2ynn marked this pull request as ready for review October 23, 2026 15:24
@2ynn
2ynn requested a review from a team as a code owner October 23, 2026 15:24
@2ynn
2ynn requested a review from ThomasRooney October 23, 2026 15:24
UnrecognizedStr and UnrecognizedInt had no serialization schema, so
dumping a model holding an unknown open enum value emitted Pydantic
serialization warnings. In an optional nullable open enum field, the
value was serialized by the Unset member of the union and then dropped
from the output, so the request body omitted it.

Give both types an explicit serializer: Python-mode dumps return the
original object, JSON-mode dumps return a plain str or int, and values
of any other type are declined so union serialization tries the
remaining members.
@2ynn
2ynn force-pushed the qynn/fix/python-unrecognized-enum-serialization branch from e86efdc to dce293e Compare October 23, 2026 15:55

@cubic-dev-ai cubic-dev-ai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

0 issues found across 5 reviewed files. 1 file intentionally excluded from review.

Confidence score: 5/5

  • Automated review surfaced no issues in the provided summaries.
  • No files require special attention.

Shadow auto-approve: would auto-approve. Adds explicit serialization for unrecognized open enum values in the Python SDK so dumps emit plain str/int without Pydantic warnings and unknown values survive in nullable union fields; a focused, tested template bug fix.

View guided diff | Turn on auto-fix | Re-trigger cubic

@speakeasy-generator-bot

speakeasy-generator-bot Bot commented Oct 23, 2026 •

Copy link
Copy Markdown

Generator Snapshot Testing

Selector(s) Result Summary
snapshot-python ✅ Snapshot run completed

Snapshot results: https://github.com/speakeasy-api/openapi-generation-snapshots/issues/134#issuecomment-6369044276

Snapshot results are summarized above. Additional run context is linked for maintainers with access.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

python Trigger (106) snapshot tests for python

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant