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

Merged
AN
10d ago
+107-0

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.