Skip to content

Commit

Permalink
Revert MediaType being comparable (#11500)
Browse files Browse the repository at this point in the history
Fixes #11414
  • Loading branch information
dstepanov authored Jan 14, 2025
1 parent 40f5e30 commit 2e6dc9f
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 49 deletions.
44 changes: 21 additions & 23 deletions http/src/main/java/io/micronaut/http/MediaType.java
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@
* @since 1.0
*/
@TypeHint(value = MediaType[].class)
public class MediaType implements CharSequence, Comparable<MediaType> {
public class MediaType implements CharSequence {

/**
* Default file extension used for JSON.
Expand Down Expand Up @@ -1273,10 +1273,29 @@ public static List<MediaType> orderedOf(List<? extends CharSequence> values) {
}
}
}
mediaTypes.sort(null);
mediaTypes.sort(MediaType::naturalSort);
return Collections.unmodifiableList(mediaTypes);
}

private static int naturalSort(MediaType o1, MediaType o2) {
//The */* type is always last
boolean fullWildcard1 = o1.type.equals(WILDCARD);
boolean fullWildcard2 = o2.type.equals(WILDCARD);
if (fullWildcard1 && fullWildcard2) {
return 0;
} else if (fullWildcard1) {
return 1;
} else if (fullWildcard2) {
return -1;
}
if (o2.subtype.equals(WILDCARD) && !o1.subtype.equals(WILDCARD)) {
return -1;
} else if (o1.subtype.equals(WILDCARD) && !o2.subtype.equals(WILDCARD)) {
return 1;
}
return o2.getQualityAsNumber().compareTo(o1.getQualityAsNumber());
}

/**
* Create a new {@link MediaType} from the given text.
*
Expand Down Expand Up @@ -1397,25 +1416,4 @@ private static Map<String, String> loadMimeTypes() {

return Collections.emptyMap();
}

@Override
public int compareTo(@NonNull MediaType o2) {
//The */* type is always last
MediaType o1 = this;
boolean fullWildcard1 = o1.type.equals(WILDCARD);
boolean fullWildcard2 = o2.type.equals(WILDCARD);
if (fullWildcard1 && fullWildcard2) {
return 0;
} else if (fullWildcard1) {
return 1;
} else if (fullWildcard2) {
return -1;
}
if (o2.subtype.equals(WILDCARD) && !o1.subtype.equals(WILDCARD)) {
return -1;
} else if (o1.subtype.equals(WILDCARD) && !o2.subtype.equals(WILDCARD)) {
return 1;
}
return o2.getQualityAsNumber().compareTo(o1.getQualityAsNumber());
}
}
33 changes: 7 additions & 26 deletions http/src/test/groovy/io/micronaut/http/MediaTypeSpec.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -189,32 +189,6 @@ class MediaTypeSpec extends Specification {
"text/plain, text/html, application/json;q=1" | [new MediaType("text/plain"), new MediaType("text/html"), new MediaType("application/json;q=1")]
}

@Unroll
void "test natural order types: #commaSeparatedList"() {
given:
List<MediaType> orderedList = MediaType.orderedOf(commaSeparatedList.split(','))

when:
orderedList = new ArrayList<>(orderedList)
Collections.shuffle(orderedList)
orderedList.sort(null)

then:
orderedList.size() == expectedList.size()
for (int i = 0; i < orderedList.size(); i++) {
assert orderedList.get(i) == expectedList.get(i)
}

where:
commaSeparatedList | expectedList
"audio/basic;q=.5, application/json" | [new MediaType("application/json"), new MediaType("audio/basic;q=.5")]
"text/html" | [new MediaType("text/html")]
"*/*, text/*, text/html" | [new MediaType("text/html"), new MediaType("text/*"), new MediaType("*/*")]
"text/html;level=1, text/html;level=2;q=.3" | [new MediaType("text/html;level=1"), new MediaType("text/html;level=2;q=.3")]
"text/*;blah=1, text/html;q=.3, audio/basic;q=.4" | [new MediaType("audio/basic;q=.4"), new MediaType("text/html;q=.3"), new MediaType("text/*;blah=1")]
"text/plain, text/html, application/json;q=1" | [new MediaType("text/plain"), new MediaType("text/html"), new MediaType("application/json;q=1")]
}

@Unroll
void "test type match #desiredType"() {
given:
Expand All @@ -231,4 +205,11 @@ class MediaTypeSpec extends Specification {
"text/plain" | "text/hml" | false
"text/*" | "application/json" | false
}

@Unroll
void "compare media type"() {
expect:
!MediaType.APPLICATION_JSON_TYPE.equals(MediaType.TEXT_XML_TYPE)
MediaType.APPLICATION_JSON_TYPE != MediaType.TEXT_XML_TYPE
}
}

0 comments on commit 2e6dc9f

Please sign in to comment.