marshmallow-jsonschema icon indicating copy to clipboard operation
marshmallow-jsonschema copied to clipboard

Feature Request: Support enums when their value is a string type

Open palchicz opened this issue 4 years ago • 1 comments

When serializing an enum to json schema, there is a check to make sure the LoadDumpOption is not by value

https://github.com/fuhrysteve/marshmallow-jsonschema/blob/81eada1a0c42ff67de216923968af0a6b54e5dcb/marshmallow_jsonschema/base.py#L227

The code comment states:

Python allows enum values to be almost anything, so it's easier to just load from the names of the enum's which will have to be strings.

Could this restriction be loosened to include when all the enum values are strings? That way, enums like...

class MyEnum(Enum):
    STR1 = "str1"
    STR2 = "str2"
    STR3 = "str3"

...would be supported when the field is set by value.

palchicz avatar Oct 05 '21 17:10 palchicz

I think an alternative solution could be the following:

class MyEnum(str, Enum):
    STR1 = "str1"
    STR2 = "str2"
    STR3 = "str3"

This seems to work for my use cases

stretch4x4 avatar Apr 06 '23 02:04 stretch4x4