dataclasses-json
dataclasses-json copied to clipboard
schema().load in combination with custom field metadata (mm_field) and letter_case throws 'Unknown field'
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:
Exception has occurred: ValidationError
{'notWorking': ['Unknown field.']}
I am cracking my head up and feel a bit lost here, what am I missing 🙏? A minimal example follows...
from dataclasses import dataclass, field
from datetime import datetime
from isodate import isodatetime
from marshmallow import fields
from dataclasses_json import LetterCase, config, dataclass_json
ISO_DATETIME_CONFIG = config(
encoder=isodatetime.datetime_isoformat,
decoder=isodatetime.parse_datetime,
mm_field=fields.DateTime(format="iso"),
)
@dataclass_json(letter_case=LetterCase.CAMEL)
@dataclass
class ExampleClass:
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?
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 have a similar problem
Problem still persists.
Still seeing this on 0.6.3