Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Using raw RestResponse return type has unexpected consequences #28627

Closed
knutwannheden opened this issue Oct 17, 2022 · 9 comments · Fixed by #28657
Closed

Using raw RestResponse return type has unexpected consequences #28627

knutwannheden opened this issue Oct 17, 2022 · 9 comments · Fixed by #28657
Labels
area/rest kind/bug Something isn't working
Milestone

Comments

@knutwannheden
Copy link
Contributor

knutwannheden commented Oct 17, 2022

Describe the bug

Specifying the raw RestResponse type as an endpoint's return type causes Quarkus to configure the ServerStringMessageBodyHandler for this endpoint. This will most often not be what the user wants.

In the following example, where the endpoint returns multipart/form-data responses, but has the raw RestResponse type as its return type, the POJO entity gets serialized using its toString() method rather than using MultipartMessageBodyWriter:

    @GET
    @Produces(MediaType.MULTIPART_FORM_DATA)
    @Path("file2")
    public RestResponse getFile2() {
        return RestResponse.ok(new DownloadFormData("test.txt", new File("test.txt")));
    }

The use case for returning RestResponse is to allow dynamically setting response headers, as documented. Of course the return type should be specified with a generic type parameter.

Expected behavior

Since RestResponse is the counterpart of the JAX-RS Response type, which isn't generic, I think it would make sense if Quarkus would log a build-time usage warning, whenever an endpoint returns the raw RestResponse type.

Actual behavior

Currently there is no warning and Quarkus will "silently" configure the ServerStringMessageBodyHandler.

How to Reproduce?

Create an endpoint with RestResponse as the return type.

Output of uname -a or ver

No response

Output of java -version

No response

GraalVM version (if different from Java)

No response

Quarkus version or git rev

2.13.2.Final

Build tool (ie. output of mvnw --version or gradlew --version)

No response

Additional information

No response

@knutwannheden knutwannheden added the kind/bug Something isn't working label Oct 17, 2022
@quarkus-bot
Copy link

quarkus-bot bot commented Oct 17, 2022

/cc @FroMage, @geoand, @stuartwdouglas

@geoand
Copy link
Contributor

geoand commented Oct 17, 2022

We'll should wait for #27526 to be merged before addressing this one

@knutwannheden
Copy link
Contributor Author

I just noticed that if I supply a generic type parameter (I hadn't noticed that RestResponse was generic 🤦 ) then the response looks a lot better:

❯ curl http://localhost:8080/hello/file2 -i
HTTP/1.1 200 OK
content-length: 322
Content-Type: multipart/form-data

--2b496fef-3c0a-469e-89f3-2ff04b5355d1
Content-Disposition: form-data; name="file"; filename="test.txt"
Content-Type: application/octet-stream

test test
--2b496fef-3c0a-469e-89f3-2ff04b5355d1
Content-Disposition: form-data; name="name"
Content-Type: text/plain

test.txt
--2b496fef-3c0a-469e-89f3-2ff04b5355d1--

rather than:

❯ curl http://localhost:8080/hello/file2 -i
HTTP/1.1 200 OK
content-length: 34
Content-Type: multipart/form-data

org.acme.DownloadFormData@20accfa0

I suppose that would be a good reason to log a warning (or even an error) at build-time if there is no generic type parameter.

Yet, as you may notice, the boundary parameter is missing in the content type. For reference, here is what a proper response looks like:

❯ curl http://localhost:8080/hello/file -i
HTTP/1.1 200 OK
content-length: 322
Content-Type: multipart/form-data;boundary=736e7384-ca9b-4c3b-8cec-5231fe5e1d00

--736e7384-ca9b-4c3b-8cec-5231fe5e1d00
Content-Disposition: form-data; name="file"; filename="test.txt"
Content-Type: application/octet-stream

test test
--736e7384-ca9b-4c3b-8cec-5231fe5e1d00
Content-Disposition: form-data; name="name"
Content-Type: text/plain

test.txt
--736e7384-ca9b-4c3b-8cec-5231fe5e1d00--

The only difference being the Content-Type which is missing the boundary parameter when the return type is RestResponse<DownloadFormData> instead of DownloadFormData.

@geoand I propose to change this issue to log a warning when an endpoint returns RestResponse without supplying a generic type parameter and create a new issue for the problem with the missing boundary parameter on Content-Type. What do you think?

@knutwannheden
Copy link
Contributor Author

I went ahead and filed #28634 for the problem regarding the Content-Type header.

@knutwannheden knutwannheden changed the title RESTEasy Reactive RestResponse cannot be used for multipart/form-data responses Using raw RestResponse return type has unexpected consequences Oct 17, 2022
@geoand
Copy link
Contributor

geoand commented Oct 17, 2022

@geoand I propose to change this issue to log a warning when an endpoint returns RestResponse without supplying a generic type parameter and create a new issue for the problem with the missing boundary parameter on Content-Type. What do you think?

👍🏼

@FroMage
Copy link
Member

FroMage commented Oct 17, 2022

What's weird is that the @Produces should be used for determining how to serialise the contents of the RestResponse, no? Does the same bug happen when using Response? Intuitively I say this should work OOTB and pick the right MessageBodyWriter, even with a raw RestResponse, no?

@knutwannheden
Copy link
Contributor Author

When I first looked at it, I was under the impression that this check should maybe also check for RestResponse:

I could put that theory to the test.

@geoand
Copy link
Contributor

geoand commented Oct 18, 2022

We should definitely at least log a warning.

@geoand
Copy link
Contributor

geoand commented Oct 18, 2022

#28657 adds a warning

geoand added a commit to geoand/quarkus that referenced this issue Oct 18, 2022
geoand added a commit that referenced this issue Oct 19, 2022
Provide a warning when a raw RestResponse return type is used
@quarkus-bot quarkus-bot bot added this to the 2.14 - main milestone Oct 19, 2022
tmihalac pushed a commit to tmihalac/quarkus that referenced this issue Oct 27, 2022
@gsmet gsmet modified the milestones: 2.14.0.CR1, 2.13.4.Final Oct 31, 2022
gsmet pushed a commit to gsmet/quarkus that referenced this issue Oct 31, 2022
zakkak pushed a commit to zakkak/quarkus that referenced this issue Nov 15, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area/rest kind/bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants