Fix null parameterName issue in content negotiation
After this change ParameterContentNegotiationStrategy no longer allows a null parameter name, ContentNegotiationManagerFactoryBean also requires it. Issue: SPR-10139
This commit is contained in:
@@ -27,6 +27,7 @@ import javax.servlet.ServletContext;
|
||||
import org.springframework.beans.factory.FactoryBean;
|
||||
import org.springframework.beans.factory.InitializingBean;
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.util.Assert;
|
||||
import org.springframework.util.CollectionUtils;
|
||||
import org.springframework.web.context.ServletContextAware;
|
||||
|
||||
@@ -55,7 +56,7 @@ public class ContentNegotiationManagerFactoryBean
|
||||
|
||||
private Boolean useJaf;
|
||||
|
||||
private String parameterName;
|
||||
private String parameterName = "format";
|
||||
|
||||
private MediaType defaultContentType;
|
||||
|
||||
@@ -126,6 +127,7 @@ public class ContentNegotiationManagerFactoryBean
|
||||
* <p>The default parameter name is {@code "format"}.
|
||||
*/
|
||||
public void setParameterName(String parameterName) {
|
||||
Assert.notNull(parameterName, "parameterName is required");
|
||||
this.parameterName = parameterName;
|
||||
}
|
||||
|
||||
|
||||
@@ -44,7 +44,6 @@ public class ParameterContentNegotiationStrategy extends AbstractMappingContentN
|
||||
*/
|
||||
public ParameterContentNegotiationStrategy(Map<String, MediaType> mediaTypes) {
|
||||
super(mediaTypes);
|
||||
Assert.notEmpty(mediaTypes, "Cannot look up media types without any mappings");
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -52,6 +51,7 @@ public class ParameterContentNegotiationStrategy extends AbstractMappingContentN
|
||||
* <p>The default parameter name is {@code format}.
|
||||
*/
|
||||
public void setParameterName(String parameterName) {
|
||||
Assert.notNull(parameterName, "parameterName is required");
|
||||
this.parameterName = parameterName;
|
||||
}
|
||||
|
||||
|
||||
@@ -88,7 +88,6 @@ public class ContentNegotiationManagerFactoryBeanTests {
|
||||
@Test
|
||||
public void favorParameter() throws Exception {
|
||||
this.factoryBean.setFavorParameter(true);
|
||||
this.factoryBean.setParameterName("f");
|
||||
|
||||
Properties mediaTypes = new Properties();
|
||||
mediaTypes.put("json", MediaType.APPLICATION_JSON_VALUE);
|
||||
@@ -98,7 +97,7 @@ public class ContentNegotiationManagerFactoryBeanTests {
|
||||
ContentNegotiationManager manager = this.factoryBean.getObject();
|
||||
|
||||
this.servletRequest.setRequestURI("/flower");
|
||||
this.servletRequest.addParameter("f", "json");
|
||||
this.servletRequest.addParameter("format", "json");
|
||||
|
||||
assertEquals(Arrays.asList(MediaType.APPLICATION_JSON), manager.resolveMediaTypes(this.webRequest));
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user