Skip to content

Commit 27215b5

Browse files
committed
Negated produces works with no Accept header present
Issue: SPR-14299
1 parent 8343ce9 commit 27215b5

File tree

2 files changed

+29
-3
lines changed

2 files changed

+29
-3
lines changed

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

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import javax.servlet.http.HttpServletRequest;
2727

2828
import org.springframework.http.MediaType;
29+
import org.springframework.web.HttpMediaTypeException;
2930
import org.springframework.web.HttpMediaTypeNotAcceptableException;
3031
import org.springframework.web.accept.ContentNegotiationManager;
3132
import org.springframework.web.bind.annotation.RequestMapping;
@@ -193,7 +194,18 @@ public ProducesRequestCondition getMatchingCondition(HttpServletRequest request)
193194
iterator.remove();
194195
}
195196
}
196-
return (result.isEmpty()) ? null : new ProducesRequestCondition(result, this.contentNegotiationManager);
197+
if (!result.isEmpty()) {
198+
return new ProducesRequestCondition(result, this.contentNegotiationManager);
199+
}
200+
try {
201+
if (getAcceptedMediaTypes(request).contains(MediaType.ALL)) {
202+
return new ProducesRequestCondition();
203+
}
204+
}
205+
catch (HttpMediaTypeException ex) {
206+
// Ignore
207+
}
208+
return null;
197209
}
198210

199211
/**

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

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2012 the original author or authors.
2+
* Copyright 2002-2016 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.
@@ -24,9 +24,14 @@
2424
import org.springframework.mock.web.test.MockHttpServletRequest;
2525
import org.springframework.web.servlet.mvc.condition.ProducesRequestCondition.ProduceMediaTypeExpression;
2626

27-
import static org.junit.Assert.*;
27+
import static org.junit.Assert.assertEquals;
28+
import static org.junit.Assert.assertNotNull;
29+
import static org.junit.Assert.assertNull;
30+
import static org.junit.Assert.assertTrue;
31+
import static org.junit.Assert.fail;
2832

2933
/**
34+
* Unit tests for {@link ProducesRequestCondition}.
3035
* @author Arjen Poutsma
3136
* @author Rossen Stoyanchev
3237
*/
@@ -52,6 +57,15 @@ public void matchNegated() {
5257
assertNull(condition.getMatchingCondition(request));
5358
}
5459

60+
@Test
61+
public void matchNegatedWithoutAcceptHeader() {
62+
ProducesRequestCondition condition = new ProducesRequestCondition("!text/plain");
63+
MockHttpServletRequest request = new MockHttpServletRequest();
64+
65+
assertNotNull(condition.getMatchingCondition(request));
66+
assertEquals(Collections.emptySet(), condition.getProducibleMediaTypes());
67+
}
68+
5569
@Test
5670
public void getProducibleMediaTypes() {
5771
ProducesRequestCondition condition = new ProducesRequestCondition("!application/xml");

0 commit comments

Comments
 (0)