fix(dio): avoid FormatException on empty JSON body with a custom responseDecoder#2550
Open
tonytonycoder11 wants to merge 1 commit into
Open
Conversation
…onseDecoder
FusedTransformer (the default transformer) fed an empty decoded body into
jsonDecode when a custom responseDecoder was set and the response had a JSON
content-type with an empty body (e.g. 200/204/304). jsonDecode('') throws
FormatException: Unexpected end of input.
SyncTransformer and BackgroundTransformer already guard the empty case with
isNotEmpty and return an empty result (normalized to null downstream for typed
JSON). Align FusedTransformer's custom-decoder slow path with the same guard.
Completes the graceful empty-body handling from cfug#2285 (which only covered the
fast, no-custom-decoder path); see cfug#2279.
Contributor
Code Coverage Report: Only Changed Files listed
Minimum allowed coverage is |
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.
Problem
FusedTransformeris the defaultTransformer. When a request sets a customresponseDecoderand the server returns an empty body with a JSON content-type (a normal200/204/304), the transformer's slow path runsjsonDecode(''), throwingFormatException: Unexpected end of input.SyncTransformerandBackgroundTransformerhandle the identical input gracefully — they guard the empty string and return it (which dio normalizes tonullfor typed JSON responses). Only the default transformer crashes.This is the residual half of #2279: PR #2285 fixed the empty-body → null behavior for the fast path (no custom decoder) but never guarded the custom-decoder slow path.
Fix
Add an
isNotEmptycheck to the slow-path guard so an empty decoded body falls through to the existingreturn decodedResponse;branch, matchingSyncTransformer/BackgroundTransformer:Non-empty malformed bodies still throw, exactly as before and as the sibling transformers do (the fix is intentionally scoped to match their
isNotEmpty-only guard).New Pull Request Checklist
mainbranch to avoid conflicts (via merge from master or rebase)CHANGELOG.mdin the corresponding packageAdditional context and info (if any)
Reproduction (no network needed), calling the default transformer directly:
The added regression test (
dio/test/transformer_test.dart→ "empty JSON body with a custom responseDecoder does not throw") fails without the fix and passes with it.