json-api-doc icon indicating copy to clipboard operation
json-api-doc copied to clipboard

deserialization fails if member attributes is a list type

Open mikostn opened this issue 5 years ago • 1 comments

  • JSON API doc version: 0.10.0
  • Python version: 3.6.6
  • Operating System: ubuntu

Description

deserialization fails if member attributes is a list type. seems _resolve is trying to unpack items of attributes.

See example below

What I Did

json_data2 = {
  "data": [{
    "type": "articles",
    "id": "1",
    "attributes": {
      "title": "JSON API paints my bikeshed!",
      "body": "The shortest article. Ever.",
      "created": "2015-05-22T14:56:29.000Z",
      "updated": "2015-05-22T14:56:28.000Z"
    },
    "relationships": {
      "author": {
        "data": {"id": "42", "type": "people"}
      }
    }
  }],
  "included": [
    {
      "type": "people",
      "id": "42",
      "attributes": {
        "name": "John",
        "age": 80,
        "gender": "male",
        "some": ["a", "b", "c"],
        "some2": {"w":"a", "c":"b", "d":"c"},
      },
    "relationships": {
      "articles": {
        "data": {"id": "1", "type": "articles"}
      }
    }    }
  ]
}

import json_api_doc
obj = json_api_doc.deserialize(json_data2)

Traceback

Traceback (most recent call last):
  File "test_json.py", line 652, in <module>
    obj = json_api_doc.deserialize(json_data2)
  File ".virtualenvs/flask_jsonapi/lib/python3.6/site-packages/json_api_doc/deserialization.py", line 29, in deserialize
    result.append(_resolve(_flat(obj), included, set()))
  File ".virtualenvs/flask_jsonapi/lib/python3.6/site-packages/json_api_doc/deserialization.py", line 50, in _resolve
    data[key] = _resolve(value, included, resolved)
  File ".virtualenvs/flask_jsonapi/lib/python3.6/site-packages/json_api_doc/deserialization.py", line 46, in _resolve
    resolved | {type_id}
  File ".virtualenvs/flask_jsonapi/lib/python3.6/site-packages/json_api_doc/deserialization.py", line 55, in _resolve
    for item in value
  File ".virtualenvs/flask_jsonapi/lib/python3.6/site-packages/json_api_doc/deserialization.py", line 55, in <listcomp>
    for item in value
  File ".virtualenvs/flask_jsonapi/lib/python3.6/site-packages/json_api_doc/deserialization.py", line 37, in _resolve
    if set(data.keys()) == {"type", "id"}:
AttributeError: 'str' object has no attribute 'keys'

mikostn avatar Dec 04 '19 12:12 mikostn

This is fixed in 0.12.0

Grab the latest version from the git repository (not from pypi.

SomewhatDamaged avatar Jan 08 '20 06:01 SomewhatDamaged