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

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

Open
mtin opened this issue Jul 21, 2022 · 4 comments

Comments

@mtin
Copy link

mtin commented Jul 21, 2022

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
Copy link
Author

mtin commented Jul 22, 2022

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?

@leontev-vyacheslav
Copy link

I have a similar problem

@rynkk
Copy link

rynkk commented Dec 6, 2022

Problem still persists.

@allen-pattern
Copy link

Still seeing this on 0.6.3

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

4 participants