Skip to content

Don't force translation of request attribute names #284

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

Conversation

amw
Copy link
Contributor

@amw amw commented Sep 22, 2016

This attribute key translation was advertised to be "off by default", but was in fact forced in parser. This commit makes it so that request attributes and relations are only translated back to snake_case format if JSON_API_FORMAT_KEYS setting is set.

I'm not mad – I don't have my model/serializer attributes named someField or SomeField. My use case is quite simple: I have a JSONField where I accept some black-box content for storage. I need to be able to return the data as it was submitted to me by users of the API.

@codecov-io
Copy link

codecov-io commented Sep 22, 2016

Codecov Report

Merging #284 into develop will decrease coverage by <.01%.
The diff coverage is 100%.

Impacted file tree graph

@@             Coverage Diff             @@
##           develop     #284      +/-   ##
===========================================
- Coverage    76.35%   76.34%   -0.01%     
===========================================
  Files           50       50              
  Lines         6014     6021       +7     
===========================================
+ Hits          4592     4597       +5     
- Misses        1422     1424       +2
Impacted Files Coverage Δ
rest_framework_json_api/parsers.py 54.04% <100%> (ø) ⬆️
example/api/serializers/identity.py 85.71% <0%> (-14.29%) ⬇️
example/models.py 67.52% <0%> (+0.17%) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update cdb8a52...2c8f50d. Read the comment docs.

Copy link
Member

@jerel jerel left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This change looks good to me. However I suppose it should go in the next major release since it changes default behavior.

@amw
Copy link
Contributor Author

amw commented Oct 3, 2016

@jerel Is there a timeline for this?

@jerel
Copy link
Member

jerel commented Oct 3, 2016

@amw not really, would it help you out if it was merged soon or are you working from your fork? I'm guessing there's other things that we should include in a v2.2 but don't have to

@amw
Copy link
Contributor Author

amw commented Oct 3, 2016

I am working from a fork, but it would comfort me if you had a current-release branch and next-release branch. Having this merged into next-release branch would:
– prevent the PR from diverging and getting conflicts
– make me worry less that this will accidentally be omitted
– allow me to point my package install to your next-release branch and get other fixes that you apply in between now and next major release (which as you said doesn't even have the timeline)

@amw amw force-pushed the configurable-attribute-name-translation branch from 71fae0b to cb08ab5 Compare October 10, 2016 21:24
@mblayman
Copy link
Collaborator

mblayman commented May 6, 2017

Hi all, since this PR is quite old now and seems to have stalled, I'd like to consider closing this PR unless it finds a champion who can bring it to closure.

What needs to be done to bring this to a conclusion?

@amw amw force-pushed the configurable-attribute-name-translation branch from cb08ab5 to 2c8f50d Compare May 6, 2017 19:27
@amw
Copy link
Contributor Author

amw commented May 6, 2017

I'm not sure what's the reason this has stalled. I thought it was scheduled for 2.2, but that has come and gone. I think we should just merge it.

@mblayman
Copy link
Collaborator

mblayman commented May 6, 2017

After looking at this a bit more closely, it appears that this fixes a behavior that wasn't working as expected so I'm happy to merge it. Before I merge this, may I make a couple of requests?

  • Could you start a section in the CHANGELOG for v2.3.0 and put an appropriate line for this fix?
  • Could you add yourself to the AUTHORS file? This project has moved to be more community maintained and I'd like to see each contributor recorded there for their efforts. I don't care if you include your email or not, but I thinking adding your name would be a nice addition.

This attribute key translation was advertised to be "off by default",
but was in fact forced in parser. This commit makes it so that request
attributes and relations are only translated back to snake_case format
if JSON_API_FORMAT_KEYS setting is set.
@amw amw force-pushed the configurable-attribute-name-translation branch from 2c8f50d to b7844e9 Compare May 7, 2017 10:07
@amw amw changed the title Don't force translation of attribute names Don't force translation of request attribute names May 7, 2017
@amw
Copy link
Contributor Author

amw commented May 7, 2017

I had to revise my PR. My initial version used the same conversion that was specified by JSON_API_FORMAT_KEYS, but that didn't make any sense. If you have attribute author_name and you set conversion to camelize then you generate authorName in API responses. In the other direction you want to read requests that contain authorName and translate it back to Django's author_name.

So I've left original author's underscore as the only possible request translation format, but I only use it when JSON_API_FORMAT_KEYS is set (default is off).

I've added changelog and authors entry. I don't want to give my email in plain text so I've used what is common in Django's AUTHORS file – website address. There is info that people can use to contact me.

@@ -1,3 +1,4 @@
Jerel Unruh <[email protected]>
Greg Aker <[email protected]>
Adam Wróbel <https://adamwrobel.com>
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks. I totally understand not wanting to put your email address. I don't like doing that either for the same reason you described. Thanks for adding your name.

@@ -4,6 +4,8 @@
from rest_framework import parsers
from rest_framework.exceptions import ParseError

from django.conf import settings
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you move this import line above the rest_framework lines without a newline between them? This project groups third party libraries together alphabetically.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, the style is a bit of a mess because it's not consistent. Don't worry about it. I created issue #344 after I left this comment above so it can be cleaned up in the future. I'll get this line later.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I recommend using isort for this kind of churn. Give it a try.

@mblayman
Copy link
Collaborator

mblayman commented May 7, 2017

👍 This looks good to me. Thanks for being patient with this and for your contribution. Once the import is moved as commented, I'll merge the PR.

@mblayman mblayman merged commit da97874 into django-json-api:develop May 7, 2017
@amw amw deleted the configurable-attribute-name-translation branch May 7, 2017 15:01
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

Successfully merging this pull request may close these issues.

4 participants