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

Empty list attributes not serialized correctly

Open rogerrohrbach opened this issue 4 years ago • 0 comments

  • JSON API doc version: 0.15.0
  • Python version: n/a
  • Operating System: n/a

Summary

json-api-doc incorrectly serializes an attribute that is an empty list as as an empty many-to-many relationship.

Description

The JSON API specification states:

Complex data structures involving JSON objects and arrays are allowed as attribute values.

json-api-doc will properly serialize an attribute that is a list, provided it is not empty. If it is empty, it will incorrectly serialize it as a relationship object representing an empty many-to-many relationship.

import json_api_doc

good = json_api_doc.serialize(
    {"$type": "basket", "id": "1", "fruits": ["apple", "pear"]}
)

bad = json_api_doc.serialize(
    {"$type": "basket", "id": "1", "fruits": []}
)

print(good)  // {'data': {'attributes': {'fruits': ['apple', 'pear']}, 'type': 'basket', 'id': '1'}}

print(bad)   // {'data': {'relationships': {'fruits': {'data': []}}, 'type': 'basket', 'id': '1'}}

rogerrohrbach avatar Dec 04 '20 01:12 rogerrohrbach