Skip to content

Empty list attributes not serialized correctly #23

Open
@rogerrohrbach

Description

@rogerrohrbach
  • 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'}}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions