Skip to content

Commit da799bc

Browse files
committed
Wrap InvalidMimeTypeException in HttpMediaTypeNotAcceptableException
The fix for #31254 resulted in an InvalidMimeTypeException being thrown by MimeTypeUtils.sortBySpecificity() instead of an IllegalArgumentException. However, InvalidMimeTypeException extends IllegalArgumentException. Consequently, the change from IllegalArgumentException to InvalidMimeTypeException did not result in the desired effect in HeaderContentNegotiationStrategy. HeaderContentNegotiationStrategy.resolveMediaTypes() still allows the InvalidMimeTypeException to propagate as-is without wrapping it in an HttpMediaTypeNotAcceptableException. To address this issue, this commit catches InvalidMediaTypeException and InvalidMimeTypeException in HeaderContentNegotiationStrategy and wraps the exception in an HttpMediaTypeNotAcceptableException. See gh-31254 See gh-31769 Closes gh-32483 (cherry picked from commit ef02f0b)
1 parent 755968f commit da799bc

File tree

2 files changed

+25
-2
lines changed

2 files changed

+25
-2
lines changed

Diff for: spring-web/src/main/java/org/springframework/web/accept/HeaderContentNegotiationStrategy.java

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2018 the original author or authors.
2+
* Copyright 2002-2024 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -23,6 +23,7 @@
2323
import org.springframework.http.InvalidMediaTypeException;
2424
import org.springframework.http.MediaType;
2525
import org.springframework.util.CollectionUtils;
26+
import org.springframework.util.InvalidMimeTypeException;
2627
import org.springframework.util.MimeTypeUtils;
2728
import org.springframework.web.HttpMediaTypeNotAcceptableException;
2829
import org.springframework.web.context.request.NativeWebRequest;
@@ -55,7 +56,7 @@ public List<MediaType> resolveMediaTypes(NativeWebRequest request)
5556
MimeTypeUtils.sortBySpecificity(mediaTypes);
5657
return !CollectionUtils.isEmpty(mediaTypes) ? mediaTypes : MEDIA_TYPE_ALL_LIST;
5758
}
58-
catch (InvalidMediaTypeException ex) {
59+
catch (InvalidMediaTypeException | InvalidMimeTypeException ex) {
5960
throw new HttpMediaTypeNotAcceptableException(
6061
"Could not parse 'Accept' header " + headerValues + ": " + ex.getMessage());
6162
}

Diff for: spring-web/src/test/java/org/springframework/web/accept/HeaderContentNegotiationStrategyTests.java

+22
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
*
3535
* @author Rossen Stoyanchev
3636
* @author Juergen Hoeller
37+
* @author Sam Brannen
3738
*/
3839
public class HeaderContentNegotiationStrategyTests {
3940

@@ -63,6 +64,27 @@ void resolveMediaTypesFromMultipleHeaderValues() throws Exception {
6364
.containsExactly("text/html", "text/x-c", "text/x-dvi;q=0.8", "text/plain;q=0.5");
6465
}
6566

67+
@Test // gh-32483
68+
void resolveMediaTypesWithMaxElements() throws Exception {
69+
String acceptHeaderValue = "text/plain, text/html,".repeat(25);
70+
this.servletRequest.addHeader("Accept", acceptHeaderValue);
71+
List<MediaType> mediaTypes = this.strategy.resolveMediaTypes(this.webRequest);
72+
73+
assertThat(mediaTypes).hasSize(50);
74+
assertThat(mediaTypes.stream().map(Object::toString).distinct())
75+
.containsExactly("text/plain", "text/html");
76+
}
77+
78+
@Test // gh-32483
79+
void resolveMediaTypesWithTooManyElements() {
80+
String acceptHeaderValue = "text/plain,".repeat(51);
81+
this.servletRequest.addHeader("Accept", acceptHeaderValue);
82+
assertThatExceptionOfType(HttpMediaTypeNotAcceptableException.class)
83+
.isThrownBy(() -> this.strategy.resolveMediaTypes(this.webRequest))
84+
.withMessageStartingWith("Could not parse 'Accept' header")
85+
.withMessageEndingWith("Too many elements");
86+
}
87+
6688
@Test
6789
public void resolveMediaTypesParseError() throws Exception {
6890
this.servletRequest.addHeader("Accept", "textplain; q=0.5");

0 commit comments

Comments
 (0)