Refine ContentNegotiationStrategy contract

Consistently return "*/*" if no media types were requested rather than
an empty list. Existing code has to check for both in any case to see
if nothing was requested.

Issue: SPR-16624
This commit is contained in:
Rossen Stoyanchev
2018-03-27 16:32:18 -04:00
parent 9a27bc9b3e
commit f3994467c4
11 changed files with 44 additions and 40 deletions

View File

@@ -259,8 +259,7 @@ public final class ProducesRequestCondition extends AbstractRequestCondition<Pro
}
private List<MediaType> getAcceptedMediaTypes(HttpServletRequest request) throws HttpMediaTypeNotAcceptableException {
List<MediaType> mediaTypes = this.contentNegotiationManager.resolveMediaTypes(new ServletWebRequest(request));
return mediaTypes.isEmpty() ? Collections.singletonList(MediaType.ALL) : mediaTypes;
return this.contentNegotiationManager.resolveMediaTypes(new ServletWebRequest(request));
}
private int indexOfEqualMediaType(MediaType mediaType) {

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2017 the original author or authors.
* Copyright 2002-2018 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.
@@ -365,8 +365,7 @@ public abstract class AbstractMessageConverterMethodProcessor extends AbstractMe
private List<MediaType> getAcceptableMediaTypes(HttpServletRequest request)
throws HttpMediaTypeNotAcceptableException {
List<MediaType> types = this.contentNegotiationManager.resolveMediaTypes(new ServletWebRequest(request));
return (types.isEmpty() ? Collections.singletonList(MediaType.ALL) : types);
return this.contentNegotiationManager.resolveMediaTypes(new ServletWebRequest(request));
}
/**

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2017 the original author or authors.
* Copyright 2002-2018 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,11 +253,7 @@ public class ContentNegotiatingViewResolver extends WebApplicationObjectSupport
Assert.state(this.contentNegotiationManager != null, "No ContentNegotiationManager set");
try {
ServletWebRequest webRequest = new ServletWebRequest(request);
List<MediaType> acceptableMediaTypes = this.contentNegotiationManager.resolveMediaTypes(webRequest);
acceptableMediaTypes = (!acceptableMediaTypes.isEmpty() ? acceptableMediaTypes :
Collections.singletonList(MediaType.ALL));
List<MediaType> producibleMediaTypes = getProducibleMediaTypes(request);
Set<MediaType> compatibleMediaTypes = new LinkedHashSet<>();
for (MediaType acceptable : acceptableMediaTypes) {

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2017 the original author or authors.
* Copyright 2002-2018 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.
@@ -25,6 +25,7 @@ import org.junit.Test;
import org.springframework.http.MediaType;
import org.springframework.mock.web.test.MockHttpServletRequest;
import org.springframework.web.accept.ContentNegotiationManager;
import org.springframework.web.accept.ContentNegotiationStrategy;
import org.springframework.web.accept.FixedContentNegotiationStrategy;
import org.springframework.web.context.request.NativeWebRequest;
import org.springframework.web.context.request.ServletWebRequest;
@@ -65,7 +66,7 @@ public class ContentNegotiationConfigurerTests {
this.servletRequest.addParameter("format", "gif");
assertEquals("Should not resolve request parameters by default",
Collections.emptyList(), manager.resolveMediaTypes(this.webRequest));
ContentNegotiationStrategy.MEDIA_TYPE_ALL_LIST, manager.resolveMediaTypes(this.webRequest));
this.servletRequest.setRequestURI("/flower");
this.servletRequest.addHeader("Accept", MediaType.IMAGE_GIF_VALUE);
@@ -104,7 +105,7 @@ public class ContentNegotiationConfigurerTests {
this.servletRequest.setRequestURI("/flower");
this.servletRequest.addHeader("Accept", MediaType.IMAGE_GIF_VALUE);
assertEquals(Collections.emptyList(), manager.resolveMediaTypes(this.webRequest));
assertEquals(ContentNegotiationStrategy.MEDIA_TYPE_ALL_LIST, manager.resolveMediaTypes(this.webRequest));
}
@Test