Skip to content

Conversation

@josemonteiro
Copy link

@josemonteiro josemonteiro commented Dec 5, 2022

If specified, enum will fall back to this value if input value is not part of enum list.

Spec file:

openapi: 3.0
components:
  schemas:
    enum_type_with_fallback:
      type: string
      description: Enum field with fallback value 'unknown'.
      enum:
        - 'a'
        - 'b'
      x-enum-fallback-value: 'unknown'

Generated code:

class EnumTypeWithFallback(Enum):
    a = 'a'
    b = 'b'
    unknown = 'unknown'

    @classmethod
    def _missing_(cls, value):
        return EnumTypeWithFallback.unknown

Usage:

>>> EnumFieldWithFallback("a")
<EnumFieldWithFallback.a: 'a'>

>>> EnumFieldWithFallback("c")
<EnumFieldWithFallback.unknown: 'unknown'>

If specified, enum will fall back to this value if input value
is not part of enum list.
@josemonteiro josemonteiro force-pushed the feature-x-enum-fallback-value branch from e40dea0 to 54777ca Compare December 5, 2022 15:42
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.

2 participants