sqlalchemy-jsonapi
sqlalchemy-jsonapi copied to clipboard
Library not serializing 'id' as string which goes against spec
It is important that the library abides by the JSONAPI spec.
The JSONAPI spec says that "Every resource object MUST contain an id member and a type member. The values of the id and type members MUST be strings."
Although someone might have the id stored as an integer in the database, it is important that is converted to string during serialization.
To bring this up to spec, we would need to change two functions:
_render_full_resource_render_short_instance
The serialization that occurs in _render_full_resource associates the 'id' to the instance.id rather than the str(instance.id).
def _render_full_resource(self, instance, include, fields):
"""
Generate a representation of a full resource to match JSON API spec.
:param instance: The instance to serialize
:param include: Dictionary of relationships to include
:param fields: Dictionary of fields to filter
"""
api_type = instance.__jsonapi_type__
orm_desc_keys = instance.__mapper__.all_orm_descriptors.keys()
to_ret = {
'id': instance.id,
'type': api_type,
'attributes': {},
'relationships': {},
'included': {}
}
...
The same occurs in _render_short_instance. During serialization, the library 'id' corresponds to the instance.id rather than the str(instance.id).
def _render_short_instance(self, instance):
"""
For those very short versions of resources, we have this.
:param instance: The instance to render
"""
check_permission(instance, None, Permissions.VIEW)
return {'type': instance.__jsonapi_type__, 'id': instance.id}
These are easy fixes by simply doing str(instance.id) instead of instance.id.