miLibris / flask-rest-jsonapi

Flask extension to build REST APIs around JSONAPI 1.0 specification.
http://flask-rest-jsonapi.readthedocs.io
MIT License
597 stars 153 forks source link

Serialized relationship id should be string #152

Open vladmunteanu opened 5 years ago

vladmunteanu commented 5 years ago

According to JSON:API, the id field should always be a string, but this isn't the case when fetching relationships through SqlalchemyDataLayer.

I suspect the problem originates here: https://github.com/miLibris/flask-rest-jsonapi/blob/b4cb5576b75ffaf6463abb8952edc8839b03463f/flask_rest_jsonapi/data_layers/alchemy.py#L256

@akira-dev I would be happy to contribute with a pull request if you consider this correct.

kumy commented 5 years ago

@vladmunteanu did you tried to declare your id field as Str in your schema?


class SomeSchema(Schema):
    class Meta:
        type_ = 'some-type'
        self_view = 'v1.some_type_details'
        self_view_kwargs = {'id': '<id>'}
        self_view_many = 'v1.some_type_list'
        inflect = dasherize
        strict = True
        ordered = True

    id = fields.Str(dump_only=True)
    […]
vladmunteanu commented 5 years ago

@kumy Yes, the id is already a fields.Str. LE: this should be enforced by the library anyway, since the spec requires it.

To be clear, this doesn't happen for a get on /some-resource, but only for relationships returned by the ResourceRelationship. I suspect this is because there is no cast done here: https://github.com/miLibris/flask-rest-jsonapi/blob/b4cb5576b75ffaf6463abb8952edc8839b03463f/flask_rest_jsonapi/data_layers/alchemy.py#L290