Browse Source

Make sure that MediaType comparators are transitive

Previous to this commit, the specificity and quality comparators
(used by MediaType::sortByQualityValue and MediaType::sortBySpecificity)
could result in IllegalArgumentExceptions when used for sorting.
The underlying reason was that the comparators were not transitive, and
both media types with the same type, and types with the same amount of
parameters, would be considered identical by the comparator (result 0).

This commit ensures that the comparators are transitive.

Closes gh-27488
pull/27555/head
Arjen Poutsma 3 years ago
parent
commit
388c8e4aa5
  1. 6
      spring-core/src/main/java/org/springframework/util/MimeType.java
  2. 6
      spring-web/src/main/java/org/springframework/http/MediaType.java

6
spring-core/src/main/java/org/springframework/util/MimeType.java

@ -637,9 +637,6 @@ public class MimeType implements Comparable<MimeType>, Serializable { @@ -637,9 +637,6 @@ public class MimeType implements Comparable<MimeType>, Serializable {
else if (mimeType2.isWildcardType() && !mimeType1.isWildcardType()) { // audio/* > */*
return -1;
}
else if (!mimeType1.getType().equals(mimeType2.getType())) { // audio/basic == text/html
return 0;
}
else { // mediaType1.getType().equals(mediaType2.getType())
if (mimeType1.isWildcardSubtype() && !mimeType2.isWildcardSubtype()) { // audio/* < audio/basic
return 1;
@ -647,9 +644,6 @@ public class MimeType implements Comparable<MimeType>, Serializable { @@ -647,9 +644,6 @@ public class MimeType implements Comparable<MimeType>, Serializable {
else if (mimeType2.isWildcardSubtype() && !mimeType1.isWildcardSubtype()) { // audio/basic > audio/*
return -1;
}
else if (!mimeType1.getSubtype().equals(mimeType2.getSubtype())) { // audio/basic == audio/wave
return 0;
}
else { // mediaType2.getSubtype().equals(mediaType2.getSubtype())
return compareParameters(mimeType1, mimeType2);
}

6
spring-web/src/main/java/org/springframework/http/MediaType.java

@ -798,9 +798,6 @@ public class MediaType extends MimeType implements Serializable { @@ -798,9 +798,6 @@ public class MediaType extends MimeType implements Serializable {
else if (mediaType2.isWildcardType() && !mediaType1.isWildcardType()) { // audio/* > */*
return -1;
}
else if (!mediaType1.getType().equals(mediaType2.getType())) { // audio/basic == text/html
return 0;
}
else { // mediaType1.getType().equals(mediaType2.getType())
if (mediaType1.isWildcardSubtype() && !mediaType2.isWildcardSubtype()) { // audio/* < audio/basic
return 1;
@ -808,9 +805,6 @@ public class MediaType extends MimeType implements Serializable { @@ -808,9 +805,6 @@ public class MediaType extends MimeType implements Serializable {
else if (mediaType2.isWildcardSubtype() && !mediaType1.isWildcardSubtype()) { // audio/basic > audio/*
return -1;
}
else if (!mediaType1.getSubtype().equals(mediaType2.getSubtype())) { // audio/basic == audio/wave
return 0;
}
else {
int paramsSize1 = mediaType1.getParameters().size();
int paramsSize2 = mediaType2.getParameters().size();

Loading…
Cancel
Save