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

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

Open
kaitj91 opened this issue Mar 27, 2017 · 0 comments
Open

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

kaitj91 opened this issue Mar 27, 2017 · 0 comments

Comments

@kaitj91
Copy link
Contributor

kaitj91 commented Mar 27, 2017

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.

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