Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Fixed regression that tests using format still work#9615

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged

Conversation

sliverc
Copy link
Contributor

Description

This is a fix for a regression introduced in PR#6511 which we discovered in our tests run on Django REST framework JSON:API against DRF master.

The error only occurred on tests which return no content (e.g. delete), use a renderer without charset (e.g. JSONRenderer) and using format to determine the renderer.

Error only occurred on tests which return no content and usea renderer without charset (e.g. JSONRenderer)
@browniebroke
Copy link
Member

browniebroke commentedJan 7, 2025
edited
Loading

Looking at the PR that caused the regression, it seems to be related to trying to fix something else:

  • Ensure the result of_encode_data is alwaysTuple[bytes, str] rather thanTuple[Union[bytes,str], str]

We used to havean early return which was returning astr instead ofbytes as body, and the OP fixed it by moving it down, missing the edge case you're hitting. Basically, the code of the fix was:https://github.com/encode/django-rest-framework/pull/6511/files#diff-41414b310a542c20e4f2a1eac261e03c2344ac69ff2ee4607c22632e6b19ad30R159-R162

I'd rather we go closer to the original code, keeping this core change, and with the early return changed from('', content_type) to(b'', content_type). That would IMO avoid the chance of more regressions before we release.

@sliverc
Copy link
ContributorAuthor

@browniebroke Thanks for your feedback. I totally agree that we should have an early return as before, which avoids many checks on the way. I will adjust the PR later on today then.

@sliverc
Copy link
ContributorAuthor

@browniebroke Updated. It is now as close to what it was before#6511 but ensuring that the early out is also bytes. Let me know what you think.

Comment on lines 192 to 194
# Coerce text to bytes if required.
if isinstance(ret, str):
ret = ret.encode(renderer.charset)
Copy link
Member

@browniebrokebrowniebrokeJan 8, 2025
edited
Loading

Choose a reason for hiding this comment

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

Noting that this bit used to be de-dented one level before#6511:

Suggested change
# Coerce text to bytes if required.
ifisinstance(ret,str):
ret=ret.encode(renderer.charset)
# Coerce text to bytes if required.
ifisinstance(ret,str):
ret=ret.encode(renderer.charset)

On the other hand, I don't see howret.encode(renderer.charset) can work ifrenderer.charset is falsy (most likelyNone)... I'mguessing theif branch at line 187 is alwaysTrue.

All in all, I'm not sure my above suggestion matters, but if someone can think of a test case where it does, I'm open to hear it.

Copy link
ContributorAuthor

Choose a reason for hiding this comment

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

I did this as charset can be None and encode raises an exception when None is passed. As far as I understand it, when a renderer sets charset to None (e.g. JSONRenderer) it handles the encoding on its own and returns bytes. However, if the renderer faultily returns str and has charset set to None, an exception occurred before this change.

My fix indenting the str check when no charset is set, actually avoids the exception which, when thinking about it again, might make things worse as it is a misconfiguration. One could argue an assert might be better in this case. I guess though if so that should be better part of another PR and I assume it has not been an issue in the past, so let's keep it as it was.

I will remove the indent again then.

Copy link
Member

@browniebrokebrowniebroke left a comment

Choose a reason for hiding this comment

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

That looks good from my perspective. I'll leave the final word to@auvipy who reviewed#6511 to confirm my assesment of the revert.

@auvipyauvipy merged commit4a1d773 intoencode:masterJan 10, 2025
8 checks passed
@auvipy
Copy link
Member

Thanks both of you

@browniebrokebrowniebroke added this to the3.16 milestoneJan 16, 2025
@slivercsliverc deleted the test_client_no_content_by_format branchFebruary 5, 2025 18:38
Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment
Reviewers

@browniebrokebrowniebrokebrowniebroke approved these changes

@auvipyauvipyauvipy approved these changes

Assignees
No one assigned
Labels
None yet
Projects
None yet
Milestone
3.16
Development

Successfully merging this pull request may close these issues.

3 participants
@sliverc@browniebroke@auvipy

[8]ページ先頭

©2009-2025 Movatter.jp