dataclasses-json icon indicating copy to clipboard operation
dataclasses-json copied to clipboard

schema().load in combination with custom field metadata (mm_field) and letter_case throws 'Unknown field'

Open mtin opened this issue 1 year ago • 4 comments

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?

mtin avatar Jul 21 '22 17:07 mtin

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?

mtin avatar Jul 22 '22 11:07 mtin

I have a similar problem

leontev-vyacheslav avatar Aug 21 '22 13:08 leontev-vyacheslav

Problem still persists.

rynkk avatar Dec 06 '22 15:12 rynkk

Still seeing this on 0.6.3

allen-pattern avatar Dec 14 '23 15:12 allen-pattern