Skip to content

Commit 73b54f4

Browse files
committed
SPR-6466 - ContentNegotiatingViewResolver can not handle View implementations returning null as content type
1 parent c8d6360 commit 73b54f4

File tree

2 files changed

+34
-6
lines changed

2 files changed

+34
-6
lines changed

org.springframework.web.servlet/src/main/java/org/springframework/web/servlet/view/ContentNegotiatingViewResolver.java

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -350,12 +350,15 @@ public View resolveViewName(String viewName, Locale locale) throws Exception {
350350
}
351351

352352
for (View candidateView : candidateViews) {
353-
MediaType viewMediaType = MediaType.parseMediaType(candidateView.getContentType());
354-
for (MediaType requestedMediaType : requestedMediaTypes) {
355-
if (requestedMediaType.includes(viewMediaType)) {
356-
if (!views.containsKey(requestedMediaType)) {
357-
views.put(requestedMediaType, candidateView);
358-
break;
353+
String contentType = candidateView.getContentType();
354+
if (StringUtils.hasText(contentType)) {
355+
MediaType viewMediaType = MediaType.parseMediaType(contentType);
356+
for (MediaType requestedMediaType : requestedMediaTypes) {
357+
if (requestedMediaType.includes(viewMediaType)) {
358+
if (!views.containsKey(requestedMediaType)) {
359+
views.put(requestedMediaType, candidateView);
360+
break;
361+
}
359362
}
360363
}
361364
}

org.springframework.web.servlet/src/test/java/org/springframework/web/servlet/view/ContentNegotiatingViewResolverTests.java

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -280,4 +280,29 @@ public void resolveViewNameFilenameDefaultView() throws Exception {
280280
verify(viewResolverMock1, viewResolverMock2, viewMock1, viewMock2, viewMock3);
281281
}
282282

283+
@Test
284+
public void resolveViewContentTypeNull() throws Exception {
285+
MockHttpServletRequest request = new MockHttpServletRequest("GET", "/test");
286+
request.addHeader("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");
287+
RequestContextHolder.setRequestAttributes(new ServletRequestAttributes(request));
288+
289+
ViewResolver viewResolverMock = createMock(ViewResolver.class);
290+
viewResolver.setViewResolvers(Collections.singletonList(viewResolverMock));
291+
292+
View viewMock = createMock("application_xml", View.class);
293+
294+
String viewName = "view";
295+
Locale locale = Locale.ENGLISH;
296+
297+
expect(viewResolverMock.resolveViewName(viewName, locale)).andReturn(viewMock);
298+
expect(viewMock.getContentType()).andReturn(null);
299+
300+
replay(viewResolverMock, viewMock);
301+
302+
View result = viewResolver.resolveViewName(viewName, locale);
303+
assertNull("Invalid view", result);
304+
305+
verify(viewResolverMock, viewMock);
306+
}
307+
283308
}

0 commit comments

Comments
 (0)