Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Empty list attributes not serialized correctly #23

Open
rogerrohrbach opened this issue Dec 4, 2020 · 0 comments
Open

Empty list attributes not serialized correctly #23

rogerrohrbach opened this issue Dec 4, 2020 · 0 comments

Comments

@rogerrohrbach
Copy link
Contributor

  • 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'}}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant