- Notifications
You must be signed in to change notification settings - Fork302
Closed
Labels
Description
Description of the Bug Report
As of release 7.0.0 a sparse fields request throws a KeyError forurl
at
django-rest-framework-json-api/rest_framework_json_api/renderers.py
Lines 485 to 486 in366769e
ifapi_settings.URL_FIELD_NAMEinresourceandisinstance( | |
fields[api_settings.URL_FIELD_NAME],relations.RelatedField |
I expect that this is related to this change identified in the CHANGELOG:
* Adjusted that sparse fields properly removes meta fields when not defined. |
How to reproduce:
git clone https://github.com/columbia-it/django-jsonapi-trainingcd django-jsonapi-trainingpython -m venv venvsource venv/bin/activatepip install -r requirements.txtpip freeze | grep json# see the version is djangorestframework-jsonapi==7.0.0./manage.py migrate./manage.py loaddata myapp/fixtures/*yamlopenssl genrsa -out oidc.key 4096OIDC_RSA_PRIVATE_KEY_FILE=oidc.key ./manage.py runserver
Use Postman with OAuth2 asdescribed here:
Token Name: *pick a name*Grant Type: Authorization Code (With PKCE)Callback URL: https://www.getpostman.com/oauth2/callbackAuth URL: http://localhost:8000/authorize/Access Token URL: http://localhost:8000/token/Client ID: demo_djt_web_clientClient Secret: demo_djt_web_secretScope: auth-columbia demo-djt-sla-bronze read openid profile email https://api.columbia.edu/scope/groupClient Authentication: Send as Basic Auth header
ClickGet New Access Token and login as useradmin
passadmin123
andAuthorize then proceed andUse Token
Send GEThttp://localhost:8000/v1/courses/?fields[courses]=course_name
observe this error:
File "/Users/ac45/src/django-jsonapi-training/venv/lib/python3.12/site-packages/rest_framework_json_api/renderers.py", line 486, in build_json_resource_obj fields[api_settings.URL_FIELD_NAME], relations.RelatedField ~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^KeyError: 'url'
Then send GEThttp://localhost:8000//v1/courses/?fields[courses]=course_name,url
and get back a correct JSONAPI response.
Now ^C the runserver, downgrade to v6.1.0, and try again.
pip install djangorestframework-jsonapi==6.1.0OIDC_RSA_PRIVATE_KEY_FILE=oidc.key ./manage.py runserverSend GET http://localhost:8000/v1/courses/?fields[courses]=course_nameand get back a correct JSONAPI response.## Checklist- [x] Certain that this is a bug (if unsure or you have a question use [discussions](https://github.com/django-json-api/django-rest-framework-json-api/discussions) instead)- [x] Code snippet or unit test added to reproduce bug