django-json-api / django-rest-framework-json-api

JSON:API support for Django REST framework
https://django-rest-framework-json-api.readthedocs.org/
BSD 2-Clause "Simplified" License
1.19k stars 295 forks source link

Ensured that URL and id field are kept when using sparse fields #1231

Closed sliverc closed 5 months ago

sliverc commented 5 months ago

Fixes #1228

Description of the Change

URL field is considered a field in DRF but it is not in JSON:API spec therefore we may not exclude it during sparse fields. ID on the other hand is a required field and may not be filtered out either as we allow in DJA to overwrite primary key of model.

Checklist

sliverc commented 5 months ago

@n2ygk I have finally figured it out. The URL field needs to be part of the fields in the serializer meta class for the error to happen. In the code where you found the error all fields are included and as it is a HyperlinkedModelSerializer URL is also part of it.

Additionally, as mentioned before custom id was also affected by the same error which should be fixed with this PR as well.

Could you try it out with the tutorial repo whether this PR works as expected?

Thanks.

sliverc commented 4 months ago

Glad that it is working. Great that you brought this issue up and found it. That was an enlightening dive into our renderer once again. As this is a regression, let me create a new version.