Skip to content

Commit 303d491

Browse files
committed
Improve null-safety of module/spring-boot-webmvc
See gh-46926
1 parent 7432045 commit 303d491

File tree

5 files changed

+23
-16
lines changed

5 files changed

+23
-16
lines changed

module/spring-boot-webmvc/src/main/java/org/springframework/boot/webmvc/autoconfigure/actuate/web/ManagementErrorEndpoint.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818

1919
import java.util.Map;
2020

21+
import org.jspecify.annotations.Nullable;
22+
2123
import org.springframework.boot.autoconfigure.web.ErrorProperties;
2224
import org.springframework.boot.web.error.ErrorAttributeOptions;
2325
import org.springframework.boot.web.error.ErrorAttributeOptions.Include;
@@ -55,7 +57,7 @@ public ManagementErrorEndpoint(ErrorAttributes errorAttributes, ErrorProperties
5557

5658
@RequestMapping("${server.error.path:${error.path:/error}}")
5759
@ResponseBody
58-
public Map<String, Object> invoke(ServletWebRequest request) {
60+
public Map<String, @Nullable Object> invoke(ServletWebRequest request) {
5961
return this.errorAttributes.getErrorAttributes(request, getErrorAttributeOptions(request));
6062
}
6163

module/spring-boot-webmvc/src/main/java/org/springframework/boot/webmvc/autoconfigure/error/AbstractErrorController.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,8 @@ private List<ErrorViewResolver> sortErrorViewResolvers(@Nullable List<ErrorViewR
7272
return sorted;
7373
}
7474

75-
protected Map<String, Object> getErrorAttributes(HttpServletRequest request, ErrorAttributeOptions options) {
75+
protected Map<String, @Nullable Object> getErrorAttributes(HttpServletRequest request,
76+
ErrorAttributeOptions options) {
7677
WebRequest webRequest = new ServletWebRequest(request);
7778
return this.errorAttributes.getErrorAttributes(webRequest, options);
7879
}

module/spring-boot-webmvc/src/main/java/org/springframework/boot/webmvc/autoconfigure/error/BasicErrorController.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222

2323
import jakarta.servlet.http.HttpServletRequest;
2424
import jakarta.servlet.http.HttpServletResponse;
25+
import org.jspecify.annotations.Nullable;
2526

2627
import org.springframework.boot.autoconfigure.web.ErrorProperties;
2728
import org.springframework.boot.web.error.ErrorAttributeOptions;
@@ -99,7 +100,8 @@ public ResponseEntity<Map<String, Object>> error(HttpServletRequest request) {
99100
if (status == HttpStatus.NO_CONTENT) {
100101
return new ResponseEntity<>(status);
101102
}
102-
Map<String, Object> body = getErrorAttributes(request, getErrorAttributeOptions(request, MediaType.ALL));
103+
Map<String, @Nullable Object> body = getErrorAttributes(request,
104+
getErrorAttributeOptions(request, MediaType.ALL));
103105
return new ResponseEntity<>(body, status);
104106
}
105107

module/spring-boot-webmvc/src/main/java/org/springframework/boot/webmvc/error/DefaultErrorAttributes.java

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -93,22 +93,23 @@ private void storeErrorAttributes(HttpServletRequest request, Exception ex) {
9393
}
9494

9595
@Override
96-
public Map<String, Object> getErrorAttributes(WebRequest webRequest, ErrorAttributeOptions options) {
97-
Map<String, Object> errorAttributes = getErrorAttributes(webRequest, options.isIncluded(Include.STACK_TRACE));
96+
public Map<String, @Nullable Object> getErrorAttributes(WebRequest webRequest, ErrorAttributeOptions options) {
97+
Map<String, @Nullable Object> errorAttributes = getErrorAttributes(webRequest,
98+
options.isIncluded(Include.STACK_TRACE));
9899
options.retainIncluded(errorAttributes);
99100
return errorAttributes;
100101
}
101102

102-
private Map<String, Object> getErrorAttributes(WebRequest webRequest, boolean includeStackTrace) {
103-
Map<String, Object> errorAttributes = new LinkedHashMap<>();
103+
private Map<String, @Nullable Object> getErrorAttributes(WebRequest webRequest, boolean includeStackTrace) {
104+
Map<String, @Nullable Object> errorAttributes = new LinkedHashMap<>();
104105
errorAttributes.put("timestamp", new Date());
105106
addStatus(errorAttributes, webRequest);
106107
addErrorDetails(errorAttributes, webRequest, includeStackTrace);
107108
addPath(errorAttributes, webRequest);
108109
return errorAttributes;
109110
}
110111

111-
private void addStatus(Map<String, Object> errorAttributes, RequestAttributes requestAttributes) {
112+
private void addStatus(Map<String, @Nullable Object> errorAttributes, RequestAttributes requestAttributes) {
112113
Integer status = getAttribute(requestAttributes, RequestDispatcher.ERROR_STATUS_CODE);
113114
if (status == null) {
114115
errorAttributes.put("status", 999);
@@ -125,7 +126,7 @@ private void addStatus(Map<String, Object> errorAttributes, RequestAttributes re
125126
}
126127
}
127128

128-
private void addErrorDetails(Map<String, Object> errorAttributes, WebRequest webRequest,
129+
private void addErrorDetails(Map<String, @Nullable Object> errorAttributes, WebRequest webRequest,
129130
boolean includeStackTrace) {
130131
Throwable error = getError(webRequest);
131132
if (error != null) {
@@ -140,7 +141,7 @@ private void addErrorDetails(Map<String, Object> errorAttributes, WebRequest web
140141
addErrorMessage(errorAttributes, webRequest, error);
141142
}
142143

143-
private void addErrorMessage(Map<String, Object> errorAttributes, WebRequest webRequest,
144+
private void addErrorMessage(Map<String, @Nullable Object> errorAttributes, WebRequest webRequest,
144145
@Nullable Throwable error) {
145146
BindingResult bindingResult = extractBindingResult(error);
146147
if (bindingResult != null) {
@@ -155,20 +156,21 @@ private void addErrorMessage(Map<String, Object> errorAttributes, WebRequest web
155156
addExceptionErrorMessage(errorAttributes, webRequest, error);
156157
}
157158

158-
private void addMessageAndErrorsFromBindingResult(Map<String, Object> errorAttributes, BindingResult result) {
159+
private void addMessageAndErrorsFromBindingResult(Map<String, @Nullable Object> errorAttributes,
160+
BindingResult result) {
159161
errorAttributes.put("message", "Validation failed for object='%s'. Error count: %s"
160162
.formatted(result.getObjectName(), result.getAllErrors().size()));
161163
errorAttributes.put("errors", Error.wrapIfNecessary(result.getAllErrors()));
162164
}
163165

164-
private void addMessageAndErrorsFromMethodValidationResult(Map<String, Object> errorAttributes,
166+
private void addMessageAndErrorsFromMethodValidationResult(Map<String, @Nullable Object> errorAttributes,
165167
MethodValidationResult result) {
166168
errorAttributes.put("message", "Validation failed for method='%s'. Error count: %s"
167169
.formatted(result.getMethod(), result.getAllErrors().size()));
168170
errorAttributes.put("errors", Error.wrapIfNecessary(result.getAllErrors()));
169171
}
170172

171-
private void addExceptionErrorMessage(Map<String, Object> errorAttributes, WebRequest webRequest,
173+
private void addExceptionErrorMessage(Map<String, @Nullable Object> errorAttributes, WebRequest webRequest,
172174
@Nullable Throwable error) {
173175
errorAttributes.put("message", getMessage(webRequest, error));
174176
}
@@ -211,14 +213,14 @@ protected String getMessage(WebRequest webRequest, @Nullable Throwable error) {
211213
return null;
212214
}
213215

214-
private void addStackTrace(Map<String, Object> errorAttributes, Throwable error) {
216+
private void addStackTrace(Map<String, @Nullable Object> errorAttributes, Throwable error) {
215217
StringWriter stackTrace = new StringWriter();
216218
error.printStackTrace(new PrintWriter(stackTrace));
217219
stackTrace.flush();
218220
errorAttributes.put("trace", stackTrace.toString());
219221
}
220222

221-
private void addPath(Map<String, Object> errorAttributes, RequestAttributes requestAttributes) {
223+
private void addPath(Map<String, @Nullable Object> errorAttributes, RequestAttributes requestAttributes) {
222224
String path = getAttribute(requestAttributes, RequestDispatcher.ERROR_REQUEST_URI);
223225
if (path != null) {
224226
errorAttributes.put("path", path);

module/spring-boot-webmvc/src/main/java/org/springframework/boot/webmvc/error/ErrorAttributes.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ public interface ErrorAttributes {
4444
* @param options options for error attribute contents
4545
* @return a map of error attributes
4646
*/
47-
default Map<String, Object> getErrorAttributes(WebRequest webRequest, ErrorAttributeOptions options) {
47+
default Map<String, @Nullable Object> getErrorAttributes(WebRequest webRequest, ErrorAttributeOptions options) {
4848
return Collections.emptyMap();
4949
}
5050

0 commit comments

Comments
 (0)