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.

Closes gh-29794
This commit is contained in:
Arjen Poutsma 2023-01-18 10:38:09 +01:00
parent 64de6de725
commit 9ebd1e8d64
4 changed files with 33 additions and 4 deletions

View File

@ -1,5 +1,5 @@
/*
* Copyright 2002-2022 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.
@ -248,6 +248,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) {

View File

@ -1,5 +1,5 @@
/*
* Copyright 2002-2022 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 {
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");

View File

@ -1,5 +1,5 @@
/*
* Copyright 2002-2022 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.
@ -254,6 +254,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) {

View File

@ -1,5 +1,5 @@
/*
* Copyright 2002-2022 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.
@ -309,6 +309,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");