Skip to content

Resteasy Reactive: httpHeaders.getAcceptableMediaTypes() return media types in wrong order #51077

@gako

Description

@gako

Describe the bug

httpHeaders.getAcceptableMediaTypes() in the end calls MediaTypeHelper.sortByWeight which ignores the third rule if all other rules are equal:

  1. Quality values (q) are checked first. WORKS
  2. If there's a tie, the most specific range wins (e.g., text/html wins over text/*). WORKS
  3. If there's still a tie, the order of appearance in the header is used. IS MISSING

E.g the following header "application/json,text/html; charset=UTF-8,text/plain; charset=UTF-8,/;q=0.8" should return "application/json" as first element of the list since the three types have the same quality and specifity.

The problem in the code is that the sort is done with a "simple" MediaTypeComparator on the list. But this comparator is only comparing two elements against each other without any context about their position in the list.

Expected behavior

The method httpHeaders.getAcceptableMediaTypes() should return the media types in correct order with application/json being the first etc...

Actual behavior

At the moment "text/plain" is being returned, probably because it has the most additional parameters.

How to Reproduce?

Define a rest resource that accepts multiple media types:

@produces({MediaType.APPLICATION_JSON, MediaType.TEXT_PLAIN, "text/csv"})

then use httpHeaders to get the prefered media type from call,
In the rest call use the header "accept" "application/json,text/html; charset=UTF-8,text/plain; charset=UTF-8,/;q=0.8"

Output of uname -a or ver

No response

Output of java -version

No response

Quarkus version or git rev

3.29.2

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

No response

Additional information

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions