Skip to content

Check resource name on included serializer in to_internal_value #306

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

Merged

Conversation

czosel
Copy link
Contributor

@czosel czosel commented Dec 2, 2016

Fixes #302 and #303

@codecov-io
Copy link

codecov-io commented Dec 2, 2016

Codecov Report

Merging #306 into develop will increase coverage by 0.06%.
The diff coverage is 92.42%.

Impacted file tree graph

@@             Coverage Diff             @@
##           develop     #306      +/-   ##
===========================================
+ Coverage    76.38%   76.45%   +0.06%     
===========================================
  Files           50       50              
  Lines         6027     6036       +9     
===========================================
+ Hits          4604     4615      +11     
+ Misses        1423     1421       -2
Impacted Files Coverage Δ
...mple/tests/integration/test_model_resource_name.py 100% <100%> (ø) ⬆️
rest_framework_json_api/relations.py 86.48% <88.37%> (-0.03%) ⬇️
example/models.py 67.08% <0%> (-0.45%) ⬇️
rest_framework_json_api/mixins.py 98.75% <0%> (+10%) ⬆️

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 8970398...fec7e0a. Read the comment docs.

@czosel czosel force-pushed the bug_relations_to_internal_value branch from c0bce86 to 76caa82 Compare December 2, 2016 12:34
@czosel
Copy link
Contributor Author

czosel commented May 5, 2017

@mblayman Did you get a chance to take a look at this one yet? Please let me know if there's anything that needs more clarification or work.
Please don't feel pressured by my question - it's just that since we're starting to depend on this fix more and more internally I decided to ask, to get a feeling for our chances of this being merged in the future 😄

PS: Thank you for taking over the maintenance for this repo, you're doing a great job! 👍

@mblayman
Copy link
Collaborator

mblayman commented May 6, 2017

@czosel this PR pre-dated my start as a maintainer role so I haven't really looked at it closely. I'll try to get some time to take a look at it soon.

Thanks for the contribution!

@czosel
Copy link
Contributor Author

czosel commented May 6, 2017

@mblayman Awesome, thanks for your quick reply!

def test_serializer_resource_name_create(self, client, monkeypatch):
monkeypatch.setattr(serializers.CommentSerializer.Meta, 'resource_name', 'renamed_comments', False)
monkeypatch.setattr(serializers.EntrySerializer.Meta, 'resource_name', 'renamed_entries', False)
self.create_data['data']['type'] = 'renamed_comments'
Copy link
Collaborator

Choose a reason for hiding this comment

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

I think you should probably deepcopy self.create_data. Otherwise, I think you're modifying the class state for all tests.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Agreed!

def teardown_method(self, method):
models.Comment.__bases__ = (models.Comment.__bases__[0],)
try:
Copy link
Collaborator

Choose a reason for hiding this comment

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

I'm not super familiar with these tests. Would you mind explaining why this tear down code is not needed?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

No problem!
Instead of setting the resource_name properties directly, i'm using monkeypatch.setattr, which will remember the previous state of the attribute and undo the change after the test ran. For more info on monkeypatch see this blog post or the docs.

return None

def is_serializer(self, candidate):
return hasattr(candidate, 'included_serializers')
Copy link
Collaborator

Choose a reason for hiding this comment

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

Is this a true test of whether something is a serializer? A serializer could be somethign that donesn't have included_serializers, right? Am I mistaken?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

You're right - i just verified that isinstance(candidate, Serializer) also works in this case, which is certainly cleaner.

@mblayman
Copy link
Collaborator

@czosel This stuff looks pretty good to me. Thanks for your effort. I have a couple of questions which I added as comments.

Could I also request that you merge the latest develop branch, add your name to the AUTHORS file (since this is a community project and I think everyone involved should get some credit), and add an entry to the CHANGELOG for v2.3.0?

Also, I've heard that features that aren't documented don't exist. Could we find an appropriate place to document this functionality so others might benefit from your work?

Thanks!

@czosel
Copy link
Contributor Author

czosel commented May 10, 2017

@mblayman Thanks for your review! I just pushed some minor changes and hope to have answered your questions. The only thing i didn't do was change the documentation, because i think the "setting the resource name chapter" in the docs pretty much says it all - this PR is basically just fixing this feature for two previously broken cases (nested serializers, POST requests).

@mblayman
Copy link
Collaborator

👍 Thanks for your patience, @czosel! Sorry it took 6 months to get this work merged.

@czosel
Copy link
Contributor Author

czosel commented May 10, 2017

@mblayman No worries, thanks for merging it! 👍

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.

3 participants