Skip to content

fix json emitter for empty response bodies #178

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
merged 5 commits into from
Jul 22, 2025

Conversation

demianbrecht
Copy link
Member

  • Update CHANGELOG.md
  • Update README.md (as needed)
  • New dependencies were added to pyproject.toml
  • pip install succeeds with a clean virtualenv
  • There are new or modified tests that cover changes
  • Test coverage is maintained or expanded

@bgrant
Copy link
Collaborator

bgrant commented Jul 22, 2025

Can you add a CHANGELOG entry?

)
seria = self.decode(self.construct())
if isinstance(seria, list):
if len(seria) == 0 or (len(seria) == 1 and len(seria[0]) == 0):
Copy link
Collaborator

@bgrant bgrant Jul 22, 2025

Choose a reason for hiding this comment

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

I assume in some cases self.decode returns a tuple or something and that's the cause of the clause after the or? Is there a possibility of guessing this wrong and getting an IndexError on that seria[0]?

Copy link
Member Author

Choose a reason for hiding this comment

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

The base case (len == 0) is a just in case. What we actually see with our current issue is [""]. No, if we have a list of length 1, it would always be accessible and have a length given it's a string (val.decode("utf8") in self.decode)

@@ -109,6 +109,11 @@ def test_decode(self):
resp = em.render(django.test.RequestFactory().get("/"))
self.assertEqual(json.loads(resp), ["foo", "bar"])

def test_decode_empty_list(self):
em = emitters.JSONEmitter([""], lambda: None)
Copy link
Collaborator

Choose a reason for hiding this comment

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

for completeness don't we want another test with a truly empty list?

@bgrant bgrant self-requested a review July 22, 2025 19:22
bgrant
bgrant previously approved these changes Jul 22, 2025
lukka5
lukka5 previously approved these changes Jul 22, 2025
@demianbrecht demianbrecht dismissed stale reviews from lukka5 and bgrant via aef8eb9 July 22, 2025 19:31
@demianbrecht demianbrecht merged commit a87d604 into main Jul 22, 2025
5 checks passed
@demianbrecht demianbrecht deleted the fix-empty-body-response branch July 22, 2025 19:34
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