Support filename hint for client side use of ResourceDecoder #25516
Labels
in: web
Issues in web modules (web, webmvc, webflux, websocket)
type: enhancement
A general enhancement
Milestone
With #22267 the
ResourceDecoder
was extended to support a filename hint that allows setting the filename of theResource
. However, this does not work correctly with using the Spring WebClient.e.g.
The resource returned by this response will have
null
when invokinggetFileName()
.The reason for that is the following:
When invoking
response.ToEntity(ByteArrayResource.class)
. We will reach:spring-framework/spring-webflux/src/main/java/org/springframework/web/reactive/function/BodyExtractors.java
Lines 83 to 89 in 87c3bb5
Which calls
readToMono
:spring-framework/spring-webflux/src/main/java/org/springframework/web/reactive/function/BodyExtractors.java
Lines 206 to 211 in 87c3bb5
There are now 2 possibilities for invoking the reader:
reader.readMono(type, type, (ServerHttpRequest) message, response, context.hints())
- When theBodyExtractor.Context#serverResponse
is not empty, which is the case when usingServerRequest
. This case is correct.reader.readMono(type, message, context.hints())
- When theBodyExtractor.Context#serverResponse
is empty which is the case for aClientResponse
Since we are using
WebClient
we will skip the analysis for 1. And go to 2.The
serverResponse
on the extractor is set to empty here:spring-framework/spring-webflux/src/main/java/org/springframework/web/reactive/function/client/DefaultClientResponse.java
Lines 82 to 97 in 72895f0
When we look into the implementation of
reader.readMono(type, message, context.hints())
. More precisely the implementation ofspring-framework/spring-web/src/main/java/org/springframework/http/codec/HttpMessageReader.java
Line 76 in 72895f0
In
DecoderHttpMessageReader
:spring-framework/spring-web/src/main/java/org/springframework/http/codec/DecoderHttpMessageReader.java
Lines 102 to 106 in dbec16d
We can see that the hints are passed as is without creating new hints based on the message.
The fix would be to also get new hints from
ReactiveHttpInputMessage
when reading a flux or mono without aServerHttpResponse
.More precisely in this location:
spring-framework/spring-web/src/main/java/org/springframework/http/codec/DecoderHttpMessageReader.java
Lines 96 to 106 in dbec16d
This can be done only for the
ResourceHttpMessageReader
and not forDecoderHttpMessageReader
Reproducable Test case
I am not providing a PR, because I am not sure how you would best like this to be fixed.
Edit: I submitted the issue before having the chance to clean it up.
The text was updated successfully, but these errors were encountered: