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

Mediatype defaults to the first listed mediatype in MessageBodyWriter (resteasy-reactive) #22119

Closed
carlevert opened this issue Dec 10, 2021 · 6 comments · Fixed by #24687
Closed
Assignees
Labels
area/rest kind/bug Something isn't working
Milestone

Comments

@carlevert
Copy link

carlevert commented Dec 10, 2021

Describe the bug

The MediaType argument in a MessageBodyWriter<> defaults to the first listed mediatype in the resource.

@Path("")
public interface MyResource {

    @GET
    @Produces({MediaType.APPLICATION_JSON, MediaType.TEXT_PLAIN})
    Response sayHello();

}
public class MyResourceImpl implements MyResource {

    @Context
    HttpHeaders headers;

    public Response sayHello() {
        // Acceptable is set correctly according to the request
        var accept = headers.getAcceptableMediaTypes();

        return Response.ok().entity("hello").build();
    }
}
@Provider
@Produces(MediaType.TEXT_PLAIN)
public class MyMessageBodyWriter implements MessageBodyWriter<String> {

    public boolean isWriteable(Class<?> aClass, Type type, Annotation[] annotations, MediaType mediaType) {
        // MediaType is always the first listed mediatype set in @Produces
        return aClass == String.class && mediaType.getType().equals("text") && mediaType.getSubtype().equals("plain");
    }

    public void writeTo(String message, Class<?> aClass, Type type, Annotation[] annotations, MediaType mediaType, MultivaluedMap<String, Object> multivaluedMap, OutputStream outputStream) throws IOException, WebApplicationException {
        outputStream.write(0);
    }
}

Expected behavior

Expecting behaviour according to JAX-RS standard.

In the example above, the MediaType argument is always application/json in the MessageBodyWriter.
In the resource method the acceptable media types is handled correctly.

Actual behavior

The media type in the MessageBodyWriter<> is always the first listed media type from the resources methods @provides list of media types.

How to Reproduce?

No response

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

No response

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

No response

Additional information

Sorry for the bad formatting of the code.
https://stackoverflow.com/questions/69844238/mediatype-is-always-application-json-in-messagebodywriter-in-quarkus-with-quarku

@carlevert carlevert added the kind/bug Something isn't working label Dec 10, 2021
@quarkus-bot
Copy link

quarkus-bot bot commented Dec 10, 2021

/cc @FroMage, @geoand, @stuartwdouglas

@carlevert
Copy link
Author

Problem reproduced with several Quarkus versions, latest version 2.5.0.Final

@geoand
Copy link
Contributor

geoand commented Dec 13, 2021

What HTTP request are you sending to the endpoint?

@geoand
Copy link
Contributor

geoand commented Jan 24, 2022

Closing as we did not get feedback.

@geoand geoand closed this as completed Jan 24, 2022
@geoand geoand added the triage/out-of-date This issue/PR is no longer valid or relevant label Jan 24, 2022
@carlevert
Copy link
Author

Heres a sample project showing the problem https://github.com/carlevert/resteasy-reactive-mediatype-bug.
Still present in Quarkus 2.8 RC.

@geoand geoand reopened this Mar 31, 2022
@geoand geoand removed the triage/out-of-date This issue/PR is no longer valid or relevant label Mar 31, 2022
@geoand geoand self-assigned this Apr 1, 2022
geoand added a commit to geoand/quarkus that referenced this issue Apr 1, 2022
@geoand
Copy link
Contributor

geoand commented Apr 1, 2022

#24687 fixes the problem

geoand added a commit to geoand/quarkus that referenced this issue Apr 1, 2022
@quarkus-bot quarkus-bot bot added this to the 2.9 - main milestone Apr 1, 2022
geoand added a commit that referenced this issue Apr 1, 2022
Improve dynamic resolution of MessageBodyWriter providers
@gsmet gsmet modified the milestones: 2.9 - main, 2.8.1.Final Apr 11, 2022
gsmet pushed a commit to gsmet/quarkus that referenced this issue Apr 11, 2022
@gsmet gsmet modified the milestones: 2.8.1.Final, 2.7.6.Final May 12, 2022
gsmet pushed a commit to gsmet/quarkus that referenced this issue May 12, 2022
michalvavrik added a commit to michalvavrik/quarkus-test-suite that referenced this issue Jun 2, 2022
Verifies [Quarkus Issue 22119](quarkusio/quarkus#22119). According to JAX-RS standard, if request specifies accepted Media type, the type should be passed to `javax.ws.rs.ext.MessageBodyWriter`, however for use case when `javax.ws.rs.core.Response` is returned, that was only fixed in Quarkus 2.8.1 and backported to 2.7.6. Previously, if multiple media types were defined in `@Produces`, the first one was taken. This test verifies that a value of `Accept` HTTP header is same as the media type passed to `MessageBodyWriter` when endpoint response type is `Response`.
michalvavrik added a commit to michalvavrik/quarkus-test-suite that referenced this issue Jun 2, 2022
Verifies [Quarkus Issue 22119](quarkusio/quarkus#22119). According to JAX-RS standard, if request specifies accepted Media type, the type should be passed to `javax.ws.rs.ext.MessageBodyWriter`, however for use case when `javax.ws.rs.core.Response` is returned, that was only fixed in Quarkus 2.8.1 and backported to 2.7.6. Previously, if multiple media types were defined in `@Produces`, the first one was taken. This test verifies that a value of `Accept` HTTP header is same as the media type passed to `MessageBodyWriter` when endpoint response type is `Response`.
michalvavrik added a commit to michalvavrik/quarkus-test-suite that referenced this issue Jun 3, 2022
Verifies [Quarkus Issue 22119](quarkusio/quarkus#22119). According to JAX-RS standard, if request specifies accepted Media type, the type should be passed to `javax.ws.rs.ext.MessageBodyWriter`, however for use case when `javax.ws.rs.core.Response` is returned, that was only fixed in Quarkus 2.8.1 and backported to 2.7.6. Previously, if multiple media types were defined in `@Produces`, the first one was taken. This test verifies that a value of `Accept` HTTP header is same as the media type passed to `MessageBodyWriter` when endpoint response type is `Response`.
pjgg pushed a commit to pjgg/quarkus-test-suite that referenced this issue Jun 6, 2022
Verifies [Quarkus Issue 22119](quarkusio/quarkus#22119). According to JAX-RS standard, if request specifies accepted Media type, the type should be passed to `javax.ws.rs.ext.MessageBodyWriter`, however for use case when `javax.ws.rs.core.Response` is returned, that was only fixed in Quarkus 2.8.1 and backported to 2.7.6. Previously, if multiple media types were defined in `@Produces`, the first one was taken. This test verifies that a value of `Accept` HTTP header is same as the media type passed to `MessageBodyWriter` when endpoint response type is `Response`.

(cherry picked from commit 7f245d1)
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.

3 participants