From 9ebd1e8d64726237d64750c3c07a697a8d5cb428 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. Closes gh-29794 --- .../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 7a915fcaa7..56d75796be 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-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 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 edfcc46adb..4463475a4e 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-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"); 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 5d7c5f7f0e..adb876d27b 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-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 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 2043142c5d..5ba7224833 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-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");