fix: guard against a null response Content-Type in BaseClient.makeRequest - #338
Closed
cheekychops wants to merge 1 commit into
Closed
fix: guard against a null response Content-Type in BaseClient.makeRequest#338cheekychops wants to merge 1 commit into
cheekychops wants to merge 1 commit into
Conversation
…uest ResponseBody.contentType() returns null when a response carries no Content-Type header (a 204) or a malformed one, since MediaType.parse also returns null on a malformed value. makeRequest dereferenced it without a null check on both the error path and the success path, so such responses failed with a NullPointerException instead of a RecurlyException. On a non-2xx response the guard lets the existing fallback run, so callers get the status-appropriate typed exception from ExceptionFactory rather than an NPE with the status code lost. On a 2xx response the body is treated as non-binary and deserialized, so a 204 yields null instead of throwing. Responses that do carry a Content-Type are unaffected. Adds two BaseClientTest cases following the existing testNonJsonError pattern, using the MockClient.buildResponse overload that accepts a MediaType. Fixes recurly#261
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #261.
BaseClient.makeRequestreads the response content type and then dereferences it without a null check, in two places:ResponseBody.contentType()returns null whenever the response has noContent-Typeheader — a 204, or any response where an intermediary drops or malforms the header, sinceMediaType.parsealso returns null on a malformed value. Both branches then throw:Issue #261 reported the success-path occurrence in February 2024 and proposed the same one-line guard; this PR adds it on both paths, since we hit the error path in production too.
Why the error path matters as much as the success path
On a non-2xx response the null guard is what lets the existing fallback do its job. With it,
ExceptionFactory.getExceptionClass(response)runs and callers get the correct typed exception carrying the status —BadGatewayExceptionfor a 502, and so on. Without it, a 502 that happens to arrive without aContent-Typereaches the caller as a bareNullPointerExceptionwith the status code lost, which is indistinguishable from a client bug.Behaviour after the change
ExceptionFactory.getExceptionClass(response), so the status-appropriateRecurlyExceptionis thrown instead of an NPE.jsonSerializer.deserialize, which returnsnullfor an empty body. A 204 therefore yieldsnullrather than throwing.Impact seen in production
This reached real customers for us on a Recurly-side response we did not control. In one case Recurly accepted a subscription change and answered with a response the client could not read, so our service raised the NPE and returned a 500 without recording the change. Every subsequent customer retry then got a 422
"The submitted values match the current subscriptions values.", because the change was already applied, and the customer could not complete it at all.Separately, one occurrence was an
Idempotency-Keyretry: the original write timed out client-side and the same-key replay came back without a usable content type, so the retry surfaced as an NPE rather than the replayed outcome. The guard matters more than the line count suggests, because an NPE is not aRecurlyExceptionand therefore escapes everycatchblock that callers write around this SDK.Tests
Two cases added to
BaseClientTest, following the existingtestNonJsonError*pattern and using theMockClient.buildResponse(..., MediaType)overload with a null content type:testMissingContentTypeError502— assertsBadGatewayExceptionrather thanNullPointerException.testMissingContentTypeNoContent— asserts a 204 returnsnullrather than throwing.Both fail with the exact production NPE when the guard is reverted, and
mvn -Dtest=BaseClientTest testis green with it (28 tests, 0 failures). OnlyBaseClient.javais touched, which carries no generated-code disclaimer.