Skip to content

fix(python): validate string enum values excluded by not - #25070

Merged
wing328 merged 3 commits into
OpenAPITools:masterfrom
AndreyVMarkelov:fix/python-not-enum-validation
Oct 5, 2026
Merged

wing328 merged 3 commits into
OpenAPITools:masterfrom
AndreyVMarkelov:fix/python-not-enum-validation

Conversation

@AndreyVMarkelov

@AndreyVMarkelov AndreyVMarkelov commented Sep 30, 2026 •

Copy link
Copy Markdown
Contributor

Description

Fix Python client validation for string enum values excluded by an
OpenAPI not schema.

For a property such as:

kind:
  type: string
  not:
    enum:
      - known

the Python generator previously emitted a plain StrictStr without
enforcing the not constraint. This could cause multiple oneOf
schemas to match the same payload.

This change:

  • reads excluded string enum values from composedSchemas.not;
  • preserves them as a Python property vendor extension;
  • imports Pydantic field_validator when this validation is required;
  • generates a field validator that rejects the excluded values;
  • adds a regression test and OpenAPI 3.1 fixture.

Generated validation now includes:

@field_validator('kind')
def kind_validate_not_enum(cls, value):
    if value in set(["known"]):
        raise ValueError("must not be one of excluded enum values")
    return value

This allows oneOf variants using not: enum to remain mutually
exclusive.

Tests

Added testOneOfNotEnumValidation to PythonClientCodegenTest.

The generated client was also verified at runtime:

  • Other.from_json('{"kind":"known"}') is rejected with a validation
    error.
  • Other.from_json('{"kind":"other"}') succeeds.
  • TaggedUnion.from_json('{"kind":"known"}') resolves to Known.
  • TaggedUnion.from_json('{"kind":"other"}') resolves to Other.

Fixes #25069


Summary by cubic

Fixes Python client validation for string enum values excluded by an OpenAPI not schema. Previously, the generator emitted a plain StrictStr without enforcing the not constraint, so multiple oneOf schemas could match the same payload. The generator now reads excluded enum values from composedSchemas.not, preserves them as a vendor extension, and generates a Pydantic field_validator that rejects those values, emitted once per property. This keeps oneOf variants using not: enum mutually exclusive. Also adds a regression test and an OpenAPI 3.1 fixture.

Written for commit 1f1b1a7. Summary will update on new commits.

Review in cubic

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

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

All reported issues were addressed across 5 files

Reply with feedback, questions, or to request a fix.

Re-trigger cubic

Comment thread modules/openapi-generator/src/main/resources/python/model_generic.mustache Outdated

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

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

All reported issues were addressed across 3 files (changes from recent commits).

Reply with feedback, questions, or to request a fix.

Re-trigger cubic

@AndreyVMarkelov

Copy link
Copy Markdown
Contributor Author

@fa0311 @krjakbrjak would you mind taking a look when you have a chance? This is a small Python client bug fix with regression coverage and green CI.

This issue currently blocks our work on a new Dropbox SDK generated with OpenAPI Generator, so getting it into the next release would be very helpful. Thanks!

@wing328

wing328 commented Oct 5, 2026

Copy link
Copy Markdown
Member

python tests (with new schemas covering the change) passed via #25123

@wing328
wing328 merged commit ba7fb8b into OpenAPITools:master Oct 5, 2026
15 checks passed
@wing328 wing328 added this to the 7.26.0 milestone Oct 5, 2026
@wing328 wing328 mentioned this pull request Oct 5, 2026
3 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[BUG][Python] not with enum is ignored during model validation

2 participants