marshmallow-oneofschema icon indicating copy to clipboard operation
marshmallow-oneofschema copied to clipboard

0 does not work as schema key

Open coding4food opened this issue 5 years ago • 3 comments

Suppose I want to map schemas like this:

class MySchema(OneOfSchema):
  type_schemas = {
    0: Schema1,
    1: Schema2
  }

When I try to load data with type 0, I get exception 'Missing data for required field.'.

coding4food avatar Feb 28 '19 12:02 coding4food

Not sure that alone would solve your case but in those two lines:

https://github.com/marshmallow-code/marshmallow-oneofschema/blob/master/marshmallow_oneofschema/one_of_schema.py#L98

https://github.com/marshmallow-code/marshmallow-oneofschema/blob/master/marshmallow_oneofschema/one_of_schema.py#L174

if not type_schema:

could be replaced with

if type_schema is None:

Would you like to give it a try?

lafrech avatar Feb 28 '19 14:02 lafrech

@lafrech the problem occurs during schema load.

coding4food avatar Feb 28 '19 15:02 coding4food

👍 for the suggested solution, would also solve my use case with:

class MySchema(OneOfSchema):
    type_schemas = {
        True: Schema1,
        False: Schema2
    }

But I think this should be generalized to support None values as well, so we should consider using a sentinel like marshmallow.missing to mark that the value wasn't found

mm-arnvind avatar May 20 '19 11:05 mm-arnvind