GH-4025: Fix Gzip decompression logic for byte[] in ModifyResponseBodyGatewayFilterFactory#4040
GH-4025: Fix Gzip decompression logic for byte[] in ModifyResponseBodyGatewayFilterFactory#4040thisishwan2 wants to merge 1 commit intospring-cloud:mainfrom
Conversation
…ilterFactory Fixes spring-cloudgh-4025 Signed-off-by: thisishwan2 <fill0006@naver.com>
|
I would rather investigate why |
|
I have investigated why the GzipMessageBodyResolver is not being triggered. The problem is that the clientResponse.bodyToMono() call inside the Consequently, when the target class is byte[], the execution flow never reaches the logic that utilizes messageBodyDecoders. Instead, the request is intercepted by the standard ByteArrayDecoder (which ignores Gzip), resulting in the raw compressed bytes being returned without decompression. Removing this if block forces the flow to proceed to the subsequent logic where messageBodyDecoders is actually checked, allowing GzipMessageBodyResolver to function as intended. |
|
Why is |
|
The reason .filters(f -> f.modifyResponseBody(String.class, Map.class, ...))Because inClass is String, the condition Consequently, the execution skips that problematic block and falls through to the subsequent logic. In this path, the code correctly utilizes the GzipMessageBodyResolver retrieved from messageBodyDecoders to handle the Gzip decompression. The issue I am fixing is specific to cases where inClass is byte[], which gets trapped in that if block and bypasses the resolver. |
Fixes GH-4025
Description
This PR addresses an issue where ModifyResponseBodyGatewayFilterFactory fails to handle Gzipped responses correctly when the configuration is set to use byte[].class.
Root Cause:
Currently, there is an optimization logic in extractBody and writeBody methods that explicitly skips decompression/compression if the target class is byte[]:
While this might have been intended for performance, it prevents users from modifying the actual payload logic when the response is Gzipped, as the filter passes raw compressed bytes to the rewrite function. This leads to parsing errors when the user code expects decompressed data.
Changes
Removed the
if (byte[].class.isAssignableFrom(inClass))check in both extractBody and writeBody methods.Now, the filter consistently checks the Content-Encoding header and performs decompression/compression regardless of the target class type.
Verification
Added a new test case testModificationOfResponseBodyBytes in ModifyResponseBodyGatewayFilterFactoryGzipTests.
Verified that Gzipped responses are correctly decompressed, modified as strings, and recompressed even when inClass and outClass are set to byte[].