Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enum fields always cast as JSON instead of native type #233

Open
dgaedcke opened this issue Jan 22, 2023 · 0 comments
Open

Enum fields always cast as JSON instead of native type #233

dgaedcke opened this issue Jan 22, 2023 · 0 comments

Comments

@dgaedcke
Copy link

dgaedcke commented Jan 22, 2023

I'm using a combination of flask_smorest (swagger), native dataclasses and marshmallow_dataclass and I can't seem to figure out the syntax make the schema cast to and from native python Enum types when it updates my dataclass ... it always builds my payload at JsonObject instead of str (see below)

@BuiltValueField(wireName: r'sex1') JsonObject? get sex1;

Can you tell me what I'm doing wrong ... I've tried all the following:

@unique
class Sex(IntEnum):

NEVERSET = 0
UNKNOWN = 1
FEMALE = 2
MALE = 3

`class _SexSerialized(fields.Field):
"""Field that serializes to a string of sex name"""

def _serialize(self: _SexSerialized, value: Sex, attr, obj, **kwargs) -> str:
    if value is None:
        return ""
    print("_SexSerialized:")
    print(type(value))
    return value.name

def _deserialize(self: _SexSerialized, value: str, attr, data, **kwargs) -> Sex:
    try:
        return Sex[value]
    except ValueError as error:
        raise ValidationError("Pin codes must contain only digits.") from error

def dump_default(self: _SexSerialized) -> Sex:
    return Sex.UNKNOWN

SexSerializedMsg = NewType("SexSerialized", Sex, field=fields.Enum)

@DataClass()
class PersonRowMsg():
sex1: Sex = Sex.UNKNOWN
sex2: _SexSerialized = field()
sex3: SexSerializedMsg = field()
`

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

No branches or pull requests

1 participant