From 60c89dd2df3c0cbb33ada883a125cae94df0f7af Mon Sep 17 00:00:00 2001 From: Arjen Poutsma Date: Wed, 18 Jan 2023 10:38:09 +0100 Subject: [PATCH] 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 --- .../result/condition/ProducesRequestCondition.java | 5 ++++- .../condition/ProducesRequestConditionTests.java | 14 +++++++++++++- .../mvc/condition/ProducesRequestCondition.java | 5 ++++- .../condition/ProducesRequestConditionTests.java | 13 ++++++++++++- 4 files changed, 33 insertions(+), 4 deletions(-) diff --git a/spring-webflux/src/main/java/org/springframework/web/reactive/result/condition/ProducesRequestCondition.java b/spring-webflux/src/main/java/org/springframework/web/reactive/result/condition/ProducesRequestCondition.java index 6fa70307ad..59d2ab895f 100644 --- a/spring-webflux/src/main/java/org/springframework/web/reactive/result/condition/ProducesRequestCondition.java +++ b/spring-webflux/src/main/java/org/springframework/web/reactive/result/condition/ProducesRequestCondition.java @@ -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 acceptedMediaTypes = getAcceptedMediaTypes(exchange); for (MediaType acceptedMediaType : acceptedMediaTypes) { diff --git a/spring-webflux/src/test/java/org/springframework/web/reactive/result/condition/ProducesRequestConditionTests.java b/spring-webflux/src/test/java/org/springframework/web/reactive/result/condition/ProducesRequestConditionTests.java index 3b9e7e9753..62c6821999 100644 --- a/spring-webflux/src/test/java/org/springframework/web/reactive/result/condition/ProducesRequestConditionTests.java +++ b/spring-webflux/src/test/java/org/springframework/web/reactive/result/condition/ProducesRequestConditionTests.java @@ -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 { 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"); diff --git a/spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/condition/ProducesRequestCondition.java b/spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/condition/ProducesRequestCondition.java index 7a6af22725..412bd1885f 100644 --- a/spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/condition/ProducesRequestCondition.java +++ b/spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/condition/ProducesRequestCondition.java @@ -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 acceptedMediaTypes = getAcceptedMediaTypes(request); for (MediaType acceptedMediaType : acceptedMediaTypes) { diff --git a/spring-webmvc/src/test/java/org/springframework/web/servlet/mvc/condition/ProducesRequestConditionTests.java b/spring-webmvc/src/test/java/org/springframework/web/servlet/mvc/condition/ProducesRequestConditionTests.java index fc1d77166a..feee0152ab 100644 --- a/spring-webmvc/src/test/java/org/springframework/web/servlet/mvc/condition/ProducesRequestConditionTests.java +++ b/spring-webmvc/src/test/java/org/springframework/web/servlet/mvc/condition/ProducesRequestConditionTests.java @@ -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 { 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");