fuhrysteve / marshmallow-jsonschema

JSON Schema Draft v7 (http://json-schema.org/) formatting with marshmallow
MIT License
209 stars 72 forks source link

Fix marshmallow type subclass check #102

Closed atmo closed 4 years ago

atmo commented 4 years ago

Hi @fuhrysteve ! #81 along with #86 introduced a bug that causes builds to fail. This PR fixes it.

What happened

81 added sequential subclass check that iterated over the mapping from marshmallow types to python types. #86 added fields.Number as a key and decimal.Decimal as a corresponding value. Since only Python 3.6 introduced ordered dictionaries, in previous versions order was not guaranteed.

This means that sometimes the keys in that dictionary were ordered in such a way that fields.Number came first. That meant that python type corresponding to fields.Integer became decimal.Decimal and not int as expected. As JSON schema output for Decimal and int differs too, that lead to failing tests.

How it was fixed

Instead of dict I iterate over a tuple of tuples - this will guarantee the order of subclass checks. I also reworked the mapping and handcrafted it into the library so that we don't depend on what the order was in marshmallow TYPE_MAPPING dictionary: note that we couldn't, for example, reason about corresponding Python type for fields.Raw as it could be either list, set or tuple.

coveralls commented 4 years ago

Coverage Status

Coverage remained the same at ?% when pulling 24799897dd09d0f38c09475499fff7fcdfb77d6f on atmo:fix_marshmallow_type_check into 3c579c7d3b9c579e8610f7e698d4589604d4884c on fuhrysteve:master.

fuhrysteve commented 4 years ago

Thanks for the pull!!!!