Skip to content

Commit b3d56eb

Browse files
committed
Restore StringUtils.hasLength check
Update `MimeTypeUtils` so that the StringUtils.hasLength check is performed immediately on the incoming argument, rather than in `parseMimeTypeInternal`. This restores the `IllegalArgumentException` rather than the `NullPointerException` which is thrown by the `ConcurrentHashMap`. Closes gh-23215 See gh-23211
1 parent 87c15ba commit b3d56eb

File tree

2 files changed

+9
-4
lines changed

2 files changed

+9
-4
lines changed

spring-core/src/main/java/org/springframework/util/MimeTypeUtils.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -189,14 +189,13 @@ public abstract class MimeTypeUtils {
189189
* @throws InvalidMimeTypeException if the string cannot be parsed
190190
*/
191191
public static MimeType parseMimeType(String mimeType) {
192-
return cachedMimeTypes.get(mimeType);
193-
}
194-
195-
private static MimeType parseMimeTypeInternal(String mimeType) {
196192
if (!StringUtils.hasLength(mimeType)) {
197193
throw new InvalidMimeTypeException(mimeType, "'mimeType' must not be empty");
198194
}
195+
return cachedMimeTypes.get(mimeType);
196+
}
199197

198+
private static MimeType parseMimeTypeInternal(String mimeType) {
200199
int index = mimeType.indexOf(';');
201200
String fullType = (index >= 0 ? mimeType.substring(0, index) : mimeType).trim();
202201
if (fullType.isEmpty()) {

spring-core/src/test/java/org/springframework/util/MimeTypeTests.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -285,6 +285,12 @@ public void parseMimeTypeIllegalQuotedParameterValue() {
285285
MimeTypeUtils.parseMimeType("audio/*;attr=\""));
286286
}
287287

288+
@Test
289+
public void parseMimeTypeNull() {
290+
assertThatExceptionOfType(InvalidMimeTypeException.class).isThrownBy(() ->
291+
MimeTypeUtils.parseMimeType(null));
292+
}
293+
288294
@Test
289295
public void parseMimeTypes() {
290296
String s = "text/plain, text/html, text/x-dvi, text/x-c";

0 commit comments

Comments
 (0)