flask-rest-jsonapi icon indicating copy to clipboard operation
flask-rest-jsonapi copied to clipboard

Serialized relationship id should be string

Open vladmunteanu opened this issue 6 years ago • 2 comments
trafficstars

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.

vladmunteanu avatar May 08 '19 10:05 vladmunteanu

@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)
    […]

kumy avatar May 08 '19 10:05 kumy

@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

vladmunteanu avatar May 08 '19 12:05 vladmunteanu