🐛 bug: add nil-safety to response decode helpers#4081
Conversation
Summary of ChangesHello @gaby, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed! This pull request enhances the robustness of the Highlights
🧠 New Feature in Public Preview: You can now enable Memory to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console. Changelog
Activity
Using Gemini Code AssistThe full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips. Invoking Gemini You can request assistance from Gemini at any point by creating a comment using either
Customization To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a Limitations & Feedback Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here. You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension. Footnotes
|
WalkthroughThis pull request adds defensive nil-checking to the Response decode methods (JSON, CBOR, XML) that return ErrClientNil before delegating to unmarshal operations. Comprehensive test coverage verifies these nil-safety checks prevent panics and validate normal operation with initialized clients. Changes
Estimated code review effort🎯 2 (Simple) | ⏱️ ~12 minutes Suggested reviewers
Poem
🚥 Pre-merge checks | ✅ 1 | ❌ 2❌ Failed checks (1 warning, 1 inconclusive)
✅ Passed checks (1 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing touches
🧪 Generate unit tests (beta)
No actionable comments were generated in the recent review. 🎉 🧹 Recent nitpick comments
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #4081 +/- ##
==========================================
+ Coverage 90.99% 91.04% +0.05%
==========================================
Files 119 119
Lines 11256 11277 +21
==========================================
+ Hits 10242 10267 +25
+ Misses 642 638 -4
Partials 372 372
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
There was a problem hiding this comment.
Code Review
This pull request effectively addresses a potential panic in the response decode helpers (JSON, CBOR, XML) by adding nil-safety checks for the client. The introduction of the ErrResponseClientNil error and the corresponding guards in the decode methods are well-implemented. The accompanying tests are thorough, covering both the new error condition and ensuring existing functionality remains intact. My feedback includes a suggestion to refactor the new tests into a table-driven format to improve maintainability and reduce code duplication.
There was a problem hiding this comment.
Pull request overview
This PR hardens the client.Response decode helper methods so they no longer panic when called on pooled responses that have no associated Client, and instead return a stable exported error.
Changes:
- Added a new exported error
ErrResponseClientNilfor missing response client state. - Added nil checks to
Response.JSON,Response.CBOR, andResponse.XMLto return the exported error instead of panicking. - Added a regression test covering nil-safety and normal decode behavior when a client is present.
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 2 comments.
| File | Description |
|---|---|
client/response.go |
Adds r.client == nil guards to decode helpers to prevent nil dereference panics. |
client/core.go |
Introduces exported sentinel error ErrResponseClientNil used by decode helpers. |
client/response_test.go |
Adds coverage ensuring decode helpers don’t panic without a client and still succeed with one. |
### Motivation - Response decode helpers (`JSON`, `CBOR`, `XML`) could panic when used on a pooled `Response` without an attached `Client`, producing unpredictable behavior. - Introduce a stable exported error to make the API predictable and easier to handle by callers. - Harden response helper behavior so decode helpers are safe to call even when `Client` was not set. ### Description - Add a new exported error `ErrResponseClientNil` in `client/core.go`. - Guard `Response.JSON`, `Response.CBOR`, and `Response.XML` in `client/response.go` to check `r.client == nil` and return `ErrResponseClientNil` instead of dereferencing a nil `client`. - Add `Test_Response_DecodeHelpers_ClientNilSafety` in `client/response_test.go` to assert there is no panic and that the exported error is returned when client is nil, and to verify normal decode still works when a `Client` is attached. ### Testing - Ran `go test ./client -run Test_Response_DecodeHelpers_ClientNilSafety -count=1` which passed. - Ran the full test suite via `make test` and it passed (`PASS` for the client package and the repository tests ran to completion with tests passing; new test included).</n- Ran `make generate`, `make betteralign`, `make modernize`, `make format`, and `make lint` and all succeeded. - Ran `make audit` which failed in this environment because `govulncheck` reported Go standard library vulnerabilities for the environment toolchain (`go1.25.1`); this is a tooling/environment issue and not caused by the change in this PR.
Motivation
JSON,CBOR,XML) could panic when used on a pooledResponsewithout an attachedClient, producing unpredictable behavior.Clientwas not set.Description
ErrResponseClientNilinclient/core.go.Response.JSON,Response.CBOR, andResponse.XMLinclient/response.goto checkr.client == niland returnErrResponseClientNilinstead of dereferencing a nilclient.Test_Response_DecodeHelpers_ClientNilSafetyinclient/response_test.goto assert there is no panic and that the exported error is returned when client is nil, and to verify normal decode still works when aClientis attached.