Skip to content

Commit 6b59814

Browse files
committed
Switch error views to use SimpleEvaluationContext
Update `ErrorMvcAutoConfiguration` to use `SimpleEvaluationContext` rather than `StandardEvaluationContext`. Fixes gh-12507
1 parent 624a5f8 commit 6b59814

File tree

2 files changed

+84
-5
lines changed

2 files changed

+84
-5
lines changed

spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/ErrorMvcAutoConfiguration.java

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@
5959
import org.springframework.expression.EvaluationContext;
6060
import org.springframework.expression.Expression;
6161
import org.springframework.expression.spel.standard.SpelExpressionParser;
62-
import org.springframework.expression.spel.support.StandardEvaluationContext;
62+
import org.springframework.expression.spel.support.SimpleEvaluationContext;
6363
import org.springframework.util.PropertyPlaceholderHelper.PlaceholderResolver;
6464
import org.springframework.web.servlet.DispatcherServlet;
6565
import org.springframework.web.servlet.View;
@@ -276,10 +276,8 @@ private static class ExpressionResolver implements PlaceholderResolver {
276276
}
277277

278278
private EvaluationContext getContext(Map<String, ?> map) {
279-
StandardEvaluationContext context = new StandardEvaluationContext();
280-
context.addPropertyAccessor(new MapAccessor());
281-
context.setRootObject(map);
282-
return context;
279+
return SimpleEvaluationContext.forPropertyAccessors(new MapAccessor())
280+
.withRootObject(map).build();
283281
}
284282

285283
@Override
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
/*
2+
* Copyright 2012-2017 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.boot.autoconfigure.web;
18+
19+
import org.junit.Rule;
20+
import org.junit.Test;
21+
22+
import org.springframework.boot.test.rule.OutputCapture;
23+
import org.springframework.mock.web.MockHttpServletRequest;
24+
import org.springframework.mock.web.MockHttpServletResponse;
25+
import org.springframework.mock.web.MockServletContext;
26+
import org.springframework.web.context.request.RequestAttributes;
27+
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
28+
import org.springframework.web.servlet.View;
29+
import org.springframework.web.servlet.handler.DispatcherServletWebRequest;
30+
31+
import static org.assertj.core.api.Assertions.assertThat;
32+
33+
/**
34+
* Tests for {@link ErrorMvcAutoConfiguration}.
35+
*
36+
* @author Brian Clozel
37+
* @author Phillip Webb
38+
*/
39+
public class ErrorMvcAutoConfigurationTests {
40+
41+
@Rule
42+
public OutputCapture outputCapture = new OutputCapture();
43+
44+
@Test
45+
public void renderContainsViewWithExceptionDetails() throws Exception {
46+
AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext();
47+
try {
48+
context.setServletContext(new MockServletContext());
49+
context.register(ServerProperties.class, ErrorMvcAutoConfiguration.class);
50+
context.refresh();
51+
View errorView = context.getBean("error", View.class);
52+
ErrorAttributes errorAttributes = context.getBean(ErrorAttributes.class);
53+
DispatcherServletWebRequest webRequest = createWebRequest(
54+
new IllegalStateException("Exception message"), false);
55+
errorView.render(errorAttributes.getErrorAttributes(webRequest, true),
56+
webRequest.getRequest(), webRequest.getResponse());
57+
assertThat(((MockHttpServletResponse) webRequest.getResponse())
58+
.getContentAsString()).contains("<div>Exception message</div>");
59+
}
60+
finally {
61+
context.close();
62+
}
63+
}
64+
65+
private DispatcherServletWebRequest createWebRequest(Exception ex,
66+
boolean committed) {
67+
MockHttpServletRequest request = new MockHttpServletRequest("GET", "/path");
68+
MockHttpServletResponse response = new MockHttpServletResponse();
69+
DispatcherServletWebRequest webRequest = new DispatcherServletWebRequest(request,
70+
response);
71+
webRequest.setAttribute("javax.servlet.error.exception", ex,
72+
RequestAttributes.SCOPE_REQUEST);
73+
webRequest.setAttribute("javax.servlet.error.request_uri", "/path",
74+
RequestAttributes.SCOPE_REQUEST);
75+
response.setCommitted(committed);
76+
response.setOutputStreamAccessAllowed(!committed);
77+
response.setWriterAccessAllowed(!committed);
78+
return webRequest;
79+
}
80+
81+
}

0 commit comments

Comments
 (0)