Skip to content

Conversation

@saleweaver
Copy link

Hey,

I've been trying to add basic support for parsing enums as string or integer values and added tests. All pass. :-)

Happy to change things if needed.

@GabrielCappelli
Copy link
Owner

Hi, thanks for your contribution.

I'm having a look at the openapi docs for enums and it seems we may go a bit further and also specify all possible values for this enum.

This will give a nice dropdown in Swagger to pick the valid values for the Enum:
image

As far as implementation goes. I would take a slightly different approach, where we use openapi_schema_resolver to recursively resolve int or str based on the Enum class we got.

def resolve_enum(openapi_schema_resolver: "OpenApiSchemaResolver", data_type: type):
    if not isinstance(data_type, enum.EnumMeta):
        return

    openapi_schema = openapi_schema_resolver.get_schema(data_type.__base__)
    openapi_schema["enum"] = [member.value for member in data_type]

    return openapi_schema

@saleweaver
Copy link
Author

Hi,

yes - makes sense. Do you want me to add something?

Comment on lines +101 to +107
def resolve_enum(openapi_schema_resolver: "OpenApiSchemaResolver", data_type: type):
if not isinstance(data_type, enum.EnumMeta):
return

openapi_type = OPENAPI_TYPE_MAP.get(data_type.__base__)

return {"type": openapi_type}
Copy link
Owner

Choose a reason for hiding this comment

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

Suggested change
def resolve_enum(openapi_schema_resolver: "OpenApiSchemaResolver", data_type: type):
if not isinstance(data_type, enum.EnumMeta):
return
openapi_type = OPENAPI_TYPE_MAP.get(data_type.__base__)
return {"type": openapi_type}
def resolve_enum(openapi_schema_resolver: "OpenApiSchemaResolver", data_type: type):
if not isinstance(data_type, enum.EnumMeta):
return
openapi_schema = openapi_schema_resolver.get_schema(data_type.__base__)
openapi_schema["enum"] = [member.value for member in data_type]
return openapi_schema

if you can apply these changes I'll merge, test and release

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