s-knibbs / dataclasses-jsonschema

JSON schema generation from dataclasses
MIT License
166 stars 38 forks source link

Improved behaviour when decoding nullable values that did not come from the encoder #140

Open amigold opened 3 years ago

amigold commented 3 years ago

If a type is nullable and we got None input, we should decode as None and not the _NULL_TYPE class.

Otherwise it causes strange behaviour

jsangilve commented 3 years ago

I think this would break the from_dict. For example:

  @dataclass
  class PayloadWithNullable(InputModel):
      required: str
      opt_nullable: Nullable[Optional[str]] = None

# ...
assert PayloadWithNullable.from({'required': 'foo'}) == PayloadWithNullable(required='foo', opt_nullable=None)
assert PayloadWithNullable.from({'required': 'foo', 'opt_nullable': None}) == PayloadWithNullable(required='foo', opt_nullable=NULL)
amigold commented 3 years ago

Those asserts are both true @jsangilve ? The second one should also be

assert PayloadWithNullable.from({'required': 'foo', 'opt_nullable': None}) == PayloadWithNullable(required='foo', opt_nullable= None)