Skip to content

Library not serializing 'id' as string which goes against spec #56

@kaitj91

Description

@kaitj91

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.

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