From 05c3ffb2fbdf358c6a23309a3118b0a64ecb4b40 Mon Sep 17 00:00:00 2001 From: rstoyanchev Date: Wed, 15 Nov 2023 18:57:09 +0000 Subject: [PATCH] Use InvalidMimeTypeException in MimeTypeUtils#sortBySpecificity Closes gh-31254 --- .../main/java/org/springframework/util/MimeTypeUtils.java | 4 +++- .../test/java/org/springframework/util/MimeTypeTests.java | 5 +++-- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/spring-core/src/main/java/org/springframework/util/MimeTypeUtils.java b/spring-core/src/main/java/org/springframework/util/MimeTypeUtils.java index 27bbc609fc..fb23809994 100644 --- a/spring-core/src/main/java/org/springframework/util/MimeTypeUtils.java +++ b/spring-core/src/main/java/org/springframework/util/MimeTypeUtils.java @@ -362,7 +362,9 @@ public abstract class MimeTypeUtils { */ public static void sortBySpecificity(List mimeTypes) { Assert.notNull(mimeTypes, "'mimeTypes' must not be null"); - Assert.isTrue(mimeTypes.size() <= 50, "Too many elements"); + if (mimeTypes.size() >= 50) { + throw new InvalidMimeTypeException(mimeTypes.toString(), "Too many elements"); + } bubbleSort(mimeTypes, MimeType::isLessSpecific); } diff --git a/spring-core/src/test/java/org/springframework/util/MimeTypeTests.java b/spring-core/src/test/java/org/springframework/util/MimeTypeTests.java index 90b1fea3fa..e582bc60ec 100644 --- a/spring-core/src/test/java/org/springframework/util/MimeTypeTests.java +++ b/spring-core/src/test/java/org/springframework/util/MimeTypeTests.java @@ -451,8 +451,9 @@ class MimeTypeTests { MimeType audioWave = new MimeType("audio", "wave"); MimeType audioBasicLevel = new MimeType("audio", "basic", singletonMap("level", "1")); - List mimeTypes = new ArrayList<>(List.of(MimeTypeUtils.ALL, audio, audioWave, audioBasic, - audioBasicLevel)); + List mimeTypes = new ArrayList<>( + List.of(MimeTypeUtils.ALL, audio, audioWave, audioBasic, audioBasicLevel)); + MimeTypeUtils.sortBySpecificity(mimeTypes); assertThat(mimeTypes).containsExactly(audioWave, audioBasicLevel, audioBasic, audio, MimeTypeUtils.ALL);