diff --git a/http/src/main/java/io/micronaut/http/MediaType.java b/http/src/main/java/io/micronaut/http/MediaType.java index 1a90d68262..a92c49d401 100644 --- a/http/src/main/java/io/micronaut/http/MediaType.java +++ b/http/src/main/java/io/micronaut/http/MediaType.java @@ -56,7 +56,7 @@ * @since 1.0 */ @TypeHint(value = MediaType[].class) -public class MediaType implements CharSequence, Comparable { +public class MediaType implements CharSequence { /** * Default file extension used for JSON. @@ -1273,10 +1273,29 @@ public static List orderedOf(List 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. * @@ -1397,25 +1416,4 @@ private static Map 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()); - } } diff --git a/http/src/test/groovy/io/micronaut/http/MediaTypeSpec.groovy b/http/src/test/groovy/io/micronaut/http/MediaTypeSpec.groovy index fbc824d80c..14fb9c8248 100644 --- a/http/src/test/groovy/io/micronaut/http/MediaTypeSpec.groovy +++ b/http/src/test/groovy/io/micronaut/http/MediaTypeSpec.groovy @@ -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 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: @@ -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 + } }