Skip to content

Migrate MediaType.sortByQualityValue() to local implementation#3856

Merged
strehle merged 5 commits intocloudfoundry:developfrom
gdgenchev:migrate-media-type-sort
May 7, 2026
Merged

Migrate MediaType.sortByQualityValue() to local implementation#3856
strehle merged 5 commits intocloudfoundry:developfrom
gdgenchev:migrate-media-type-sort

Conversation

@gdgenchev
Copy link
Copy Markdown
Contributor

@gdgenchev gdgenchev commented Apr 22, 2026

MediaType.sortByQualityValue() was deprecated in Spring 6 and later removed in Spring 7. Copy the sorting logic into a utility class to preserve content negotiation behavior that respects client quality value preferences from Accept headers.

Exact copy of Spring 6:

https://github.com/spring-projects/spring-framework/blob/9f431e2eac1b6d8d5ca385d0cc367bac94dd37e7/spring-web/src/main/java/org/springframework/http/MediaType.java#L906-L911

https://github.com/spring-projects/spring-framework/blob/9f431e2eac1b6d8d5ca385d0cc367bac94dd37e7/spring-web/src/main/java/org/springframework/http/MediaType.java#L927-L965

https://github.com/spring-projects/spring-framework/blob/9f431e2eac1b6d8d5ca385d0cc367bac94dd37e7/spring-web/src/test/java/org/springframework/http/MediaTypeTests.java#L390-L444 (+ some additional tests for the utility method)

This maintains backward compatibility.


The other approach would be to switch to https://github.com/spring-projects/spring-framework/blob/c6096ff6e5e7de92cf6a3466e211d72b401a0178/spring-core/src/main/java/org/springframework/util/MimeTypeUtils.java#L351-L358

but it will not be backward compatible and we will have to change some ITs that cover this.

…ng 7

Spring 7 removed MediaType.sortByQualityValue() and QUALITY_VALUE_COMPARATOR
that were deprecated in Spring 6. Copy the sorting logic into a new
MediaTypeComparators utility class to preserve content negotiation behavior
that respects client quality value preferences from Accept headers.

https://github.com/spring-projects/spring-framework/blob/9f431e2eac1b6d8d5ca385d0cc367bac94dd37e7/spring-web/src/main/java/org/springframework/http/MediaType.java#L927-L965
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR preserves Spring content-negotiation behavior after MediaType.sortByQualityValue() was removed (Spring 7) by introducing a local MediaType quality-value comparator and updating call sites to use it.

Changes:

  • Added MediaTypeComparators.BY_QUALITY_VALUE, mirroring Spring 6’s quality-value comparator logic.
  • Replaced MediaType.sortByQualityValue(...) with acceptedMediaTypes.sort(MediaTypeComparators.BY_QUALITY_VALUE) in two renderers/views.

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 3 comments.

File Description
server/src/main/java/org/cloudfoundry/identity/uaa/web/ConvertingExceptionView.java Switches Accept media type sorting to the new local comparator.
server/src/main/java/org/cloudfoundry/identity/uaa/util/MediaTypeComparators.java Introduces a Spring-6-compatible MediaType quality-value comparator.
server/src/main/java/org/cloudfoundry/identity/uaa/oauth/provider/error/DefaultOAuth2ExceptionRenderer.java Switches Accept media type sorting to the new local comparator.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread server/src/main/java/org/cloudfoundry/identity/uaa/util/MediaTypeUtils.java Outdated
- Rename MediaTypeComparators to MediaTypeUtils for better semantics
- Add sortByQualityValue() utility method to handle immutable lists
- Fix usages to create mutable copies before sorting
- Remove unnecessary if-else in ConvertingExceptionView
- Add comprehensive unit tests including parameterized tests
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR preserves UAA’s existing content-negotiation behavior by replacing the removed MediaType.sortByQualityValue(...) API with an equivalent local implementation, ensuring Accept header q (quality) preferences are still respected when selecting response converters.

Changes:

  • Added MediaTypeUtils with a BY_QUALITY_VALUE comparator and sortByQualityValue(...) method matching Spring 6 behavior.
  • Updated server-side rendering paths to use MediaTypeUtils.sortByQualityValue(...) instead of the removed Spring API.
  • Added unit tests covering sorting behavior and comparator edge cases (including stable ordering expectations).

Reviewed changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated no comments.

File Description
server/src/main/java/org/cloudfoundry/identity/uaa/util/MediaTypeUtils.java Introduces the local comparator + sort helper to replace removed Spring API.
server/src/main/java/org/cloudfoundry/identity/uaa/web/ConvertingExceptionView.java Switches Accept sorting to the new local utility before choosing a message converter.
server/src/main/java/org/cloudfoundry/identity/uaa/oauth/provider/error/DefaultOAuth2ExceptionRenderer.java Switches Accept sorting to the new local utility before choosing a message converter.
server/src/test/java/org/cloudfoundry/identity/uaa/util/MediaTypeUtilsTest.java Adds unit coverage to validate ordering, stability, and comparator semantics.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR preserves UAA’s Accept-header content negotiation behavior after Spring Framework removed MediaType.sortByQualityValue() by introducing a local equivalent and updating call sites to use it.

Changes:

  • Added MediaTypeUtils with a Spring 6–compatible quality-value comparator and in-place sort helper.
  • Replaced usages of MediaType.sortByQualityValue(...) with MediaTypeUtils.sortByQualityValue(...).
  • Added unit tests covering null handling and comparator behavior.

Reviewed changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated 3 comments.

File Description
server/src/main/java/org/cloudfoundry/identity/uaa/util/MediaTypeUtils.java New local Spring-6-equivalent comparator + sorting helper for MediaType.
server/src/test/java/org/cloudfoundry/identity/uaa/util/MediaTypeUtilsTest.java New unit tests for the utility comparator/sort behavior.
server/src/main/java/org/cloudfoundry/identity/uaa/web/ConvertingExceptionView.java Switched Accept sorting to use the local utility.
server/src/main/java/org/cloudfoundry/identity/uaa/oauth/provider/error/DefaultOAuth2ExceptionRenderer.java Switched Accept sorting to use the local utility.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread server/src/main/java/org/cloudfoundry/identity/uaa/util/MediaTypeUtils.java Outdated
Comment thread server/src/test/java/org/cloudfoundry/identity/uaa/util/MediaTypeUtilsTest.java Outdated
Comment thread server/src/test/java/org/cloudfoundry/identity/uaa/util/MediaTypeUtilsTest.java Outdated
fhanik
fhanik previously approved these changes May 5, 2026
@github-project-automation github-project-automation Bot moved this from Inbox to Pending Merge | Prioritized in Foundational Infrastructure Working Group May 5, 2026
@fhanik
Copy link
Copy Markdown
Contributor

fhanik commented May 5, 2026

I'd backfill the test copilot is asking for, then we can merge

@fhanik fhanik added the accepted Accepted the issue label May 5, 2026
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 4 out of 4 changed files in this pull request and generated 1 comment.

Comment thread server/src/test/java/org/cloudfoundry/identity/uaa/util/MediaTypeUtilsTest.java Outdated
@strehle
Copy link
Copy Markdown
Member

strehle commented May 6, 2026

alternative #3901

Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 4 out of 4 changed files in this pull request and generated no new comments.

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 4 out of 4 changed files in this pull request and generated no new comments.

Copy link
Copy Markdown
Member

@strehle strehle left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have an alternative PR #3901 but not sure if this is 100% compatible, however others can decide which variant we should use. both allow spring 7

@strehle strehle merged commit bc2bd0f into cloudfoundry:develop May 7, 2026
35 checks passed
@github-project-automation github-project-automation Bot moved this from Pending Merge | Prioritized to Done in Foundational Infrastructure Working Group May 7, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

accepted Accepted the issue

Projects

Development

Successfully merging this pull request may close these issues.

5 participants