Browse Source

Fix IllegalStateException in empty ProducesRequestCondition

When comparing empty ProducesRequestCondition, compareTo would throw an
IllegalStateException if the Accept header was invalid. This commit
fixes that behavior.

See gh-29794
Closes gh-29836
pull/29935/head
Arjen Poutsma 3 years ago
parent
commit
60c89dd2df
  1. 5
      spring-webflux/src/main/java/org/springframework/web/reactive/result/condition/ProducesRequestCondition.java
  2. 14
      spring-webflux/src/test/java/org/springframework/web/reactive/result/condition/ProducesRequestConditionTests.java
  3. 5
      spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/condition/ProducesRequestCondition.java
  4. 13
      spring-webmvc/src/test/java/org/springframework/web/servlet/mvc/condition/ProducesRequestConditionTests.java

5
spring-webflux/src/main/java/org/springframework/web/reactive/result/condition/ProducesRequestCondition.java

@ -1,5 +1,5 @@ @@ -1,5 +1,5 @@
/*
* Copyright 2002-2020 the original author or authors.
* Copyright 2002-2023 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -247,6 +247,9 @@ public final class ProducesRequestCondition extends AbstractRequestCondition<Pro @@ -247,6 +247,9 @@ public final class ProducesRequestCondition extends AbstractRequestCondition<Pro
*/
@Override
public int compareTo(ProducesRequestCondition other, ServerWebExchange exchange) {
if (this.expressions.isEmpty() && other.expressions.isEmpty()) {
return 0;
}
try {
List<MediaType> acceptedMediaTypes = getAcceptedMediaTypes(exchange);
for (MediaType acceptedMediaType : acceptedMediaTypes) {

14
spring-webflux/src/test/java/org/springframework/web/reactive/result/condition/ProducesRequestConditionTests.java

@ -1,5 +1,5 @@ @@ -1,5 +1,5 @@
/*
* Copyright 2002-2019 the original author or authors.
* Copyright 2002-2023 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -297,6 +297,18 @@ public class ProducesRequestConditionTests { @@ -297,6 +297,18 @@ public class ProducesRequestConditionTests {
assertThat(result > 0).as("Should have used MediaType.equals(Object) to break the match").isTrue();
}
@Test
public void compareEmptyInvalidAccept() {
MockServerWebExchange exchange = MockServerWebExchange.from(get("/").header("Accept", "foo"));
ProducesRequestCondition condition1 = new ProducesRequestCondition();
ProducesRequestCondition condition2 = new ProducesRequestCondition();
int result = condition1.compareTo(condition2, exchange);
assertThat(result).isEqualTo(0);
}
@Test
public void combine() {
ProducesRequestCondition condition1 = new ProducesRequestCondition("text/plain");

5
spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/condition/ProducesRequestCondition.java

@ -1,5 +1,5 @@ @@ -1,5 +1,5 @@
/*
* Copyright 2002-2020 the original author or authors.
* Copyright 2002-2023 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -253,6 +253,9 @@ public final class ProducesRequestCondition extends AbstractRequestCondition<Pro @@ -253,6 +253,9 @@ public final class ProducesRequestCondition extends AbstractRequestCondition<Pro
*/
@Override
public int compareTo(ProducesRequestCondition other, HttpServletRequest request) {
if (this.expressions.isEmpty() && other.expressions.isEmpty()) {
return 0;
}
try {
List<MediaType> acceptedMediaTypes = getAcceptedMediaTypes(request);
for (MediaType acceptedMediaType : acceptedMediaTypes) {

13
spring-webmvc/src/test/java/org/springframework/web/servlet/mvc/condition/ProducesRequestConditionTests.java

@ -1,5 +1,5 @@ @@ -1,5 +1,5 @@
/*
* Copyright 2002-2019 the original author or authors.
* Copyright 2002-2023 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -310,6 +310,17 @@ public class ProducesRequestConditionTests { @@ -310,6 +310,17 @@ public class ProducesRequestConditionTests {
assertThat(result > 0).as("Should have used MediaType.equals(Object) to break the match").isTrue();
}
@Test
public void compareEmptyInvalidAccept() {
HttpServletRequest request = createRequest("foo");
ProducesRequestCondition condition1 = new ProducesRequestCondition();
ProducesRequestCondition condition2 = new ProducesRequestCondition();
int result = condition1.compareTo(condition2, request);
assertThat(result).isEqualTo(0);
}
@Test
public void combine() {
ProducesRequestCondition condition1 = new ProducesRequestCondition("text/plain");

Loading…
Cancel
Save