You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I am trying to deserialize and validate (schema().loads) a dataclass with datetime fields that follow the camel case style (LetterCase.CAMEL). Camel case fields without custom mm_field config get validated/deserialized just fine, as well as non-camel case fields with custom mm_field. But both together result in:
I am cracking my head up and feel a bit lost here, what am I missing 🙏? A minimal example follows...
fromdataclassesimportdataclass, fieldfromdatetimeimportdatetimefromisodateimportisodatetimefrommarshmallowimportfieldsfromdataclasses_jsonimportLetterCase, config, dataclass_jsonISO_DATETIME_CONFIG=config(
encoder=isodatetime.datetime_isoformat,
decoder=isodatetime.parse_datetime,
mm_field=fields.DateTime(format="iso"),
)
@dataclass_json(letter_case=LetterCase.CAMEL)@dataclassclassExampleClass:
working: datetime=field(metadata=ISO_DATETIME_CONFIG)
not_working: datetime=field(metadata=ISO_DATETIME_CONFIG)
object=ExampleClass(working=datetime.now(), not_working=datetime.now())
serialized=object.to_json()
deserialized_throws=ExampleClass.schema().loads(serialized)
# Exception has occurred: ValidationError# {'notWorking': ['Unknown field.']} <---- seems like the letter_case is not applied when using the schema().loads() with custom metadata / mm_field config?
The text was updated successfully, but these errors were encountered:
I have further debugged this issue. Once I supply a value for mm_field in the config, all other settings (encoder, decoder, letter_case) are not used anymore, see mm.py:287
I (at least thought that I) needed to add the mm_field config because otherwise when using schema().loads on a dataclass with datetime fields the deserialization fails (it tries to deserialize a timestamp, not an isostring)
So, I am still lost. How can I have a dataclass with datetime properties serializing into iso strings and deserializing again with proper validation errors?
I am trying to deserialize and validate (
schema().loads
) a dataclass with datetime fields that follow the camel case style (LetterCase.CAMEL
).Camel case fields without custom
mm_field
config get validated/deserialized just fine, as well as non-camel case fields with custommm_field
. But both together result in:I am cracking my head up and feel a bit lost here, what am I missing 🙏? A minimal example follows...
The text was updated successfully, but these errors were encountered: