Skip to content

Commit 0d19234

Browse files
committed
Improve null-safety of module/spring-boot-webflux
See gh-46926
1 parent 76157c7 commit 0d19234

File tree

4 files changed

+19
-15
lines changed

4 files changed

+19
-15
lines changed

module/spring-boot-webflux/src/main/java/org/springframework/boot/webflux/autoconfigure/error/AbstractErrorWebExceptionHandler.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ public void setViewResolvers(List<ViewResolver> viewResolvers) {
127127
* @param options options to control error attributes
128128
* @return the error attributes as a Map
129129
*/
130-
protected Map<String, Object> getErrorAttributes(ServerRequest request, ErrorAttributeOptions options) {
130+
protected Map<String, @Nullable Object> getErrorAttributes(ServerRequest request, ErrorAttributeOptions options) {
131131
return this.errorAttributes.getErrorAttributes(request, options);
132132
}
133133

@@ -194,7 +194,7 @@ private boolean getBooleanParameter(ServerRequest request, String parameterName)
194194
* @return a Publisher of the {@link ServerResponse}
195195
*/
196196
protected Mono<ServerResponse> renderErrorView(String viewName, ServerResponse.BodyBuilder responseBody,
197-
Map<String, Object> error) {
197+
Map<String, @Nullable Object> error) {
198198
if (isTemplateAvailable(viewName)) {
199199
return responseBody.render(viewName, error);
200200
}
@@ -234,7 +234,7 @@ private boolean isTemplateAvailable(String viewName) {
234234
* @return a Publisher of the {@link ServerResponse}
235235
*/
236236
protected Mono<ServerResponse> renderDefaultErrorView(ServerResponse.BodyBuilder responseBody,
237-
Map<String, Object> error) {
237+
Map<String, @Nullable Object> error) {
238238
StringBuilder builder = new StringBuilder();
239239
Date timestamp = (Date) error.get("timestamp");
240240
Object message = error.get("message");

module/spring-boot-webflux/src/main/java/org/springframework/boot/webflux/autoconfigure/error/DefaultErrorWebExceptionHandler.java

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import java.util.List;
2424
import java.util.Map;
2525

26+
import org.jspecify.annotations.Nullable;
2627
import reactor.core.publisher.Flux;
2728
import reactor.core.publisher.Mono;
2829

@@ -123,7 +124,7 @@ protected RouterFunction<ServerResponse> getRoutingFunction(ErrorAttributes erro
123124
* @return a {@code Publisher} of the HTTP response
124125
*/
125126
protected Mono<ServerResponse> renderErrorView(ServerRequest request) {
126-
Map<String, Object> errorAttributes = getErrorAttributes(request, MediaType.TEXT_HTML);
127+
Map<String, @Nullable Object> errorAttributes = getErrorAttributes(request, MediaType.TEXT_HTML);
127128
int status = getHttpStatus(request, errorAttributes);
128129
ServerResponse.BodyBuilder responseBody = ServerResponse.status(status).contentType(TEXT_HTML_UTF8);
129130
return Flux.just(getData(status).toArray(new String[] {}))
@@ -150,14 +151,14 @@ private List<String> getData(int errorStatus) {
150151
* @return a {@code Publisher} of the HTTP response
151152
*/
152153
protected Mono<ServerResponse> renderErrorResponse(ServerRequest request) {
153-
Map<String, Object> errorAttributes = getErrorAttributes(request, MediaType.ALL);
154+
Map<String, @Nullable Object> errorAttributes = getErrorAttributes(request, MediaType.ALL);
154155
int status = getHttpStatus(request, errorAttributes);
155156
return ServerResponse.status(status)
156157
.contentType(MediaType.APPLICATION_JSON)
157158
.body(BodyInserters.fromValue(errorAttributes));
158159
}
159160

160-
private Map<String, Object> getErrorAttributes(ServerRequest request, MediaType mediaType) {
161+
private Map<String, @Nullable Object> getErrorAttributes(ServerRequest request, MediaType mediaType) {
161162
return getErrorAttributes(request, getErrorAttributeOptions(request, mediaType));
162163
}
163164

@@ -235,7 +236,7 @@ protected boolean isIncludePath(ServerRequest request, MediaType produces) {
235236
};
236237
}
237238

238-
private int getHttpStatus(ServerRequest request, Map<String, Object> errorAttributes) {
239+
private int getHttpStatus(ServerRequest request, Map<String, @Nullable Object> errorAttributes) {
239240
return getHttpStatus(errorAttributes.containsKey("status") ? errorAttributes
240241
: defaultErrorAttributes.getErrorAttributes(request, ONLY_STATUS));
241242
}
@@ -245,7 +246,7 @@ private int getHttpStatus(ServerRequest request, Map<String, Object> errorAttrib
245246
* @param errorAttributes the current error information
246247
* @return the error HTTP status
247248
*/
248-
protected int getHttpStatus(Map<String, Object> errorAttributes) {
249+
protected int getHttpStatus(Map<String, @Nullable Object> errorAttributes) {
249250
Object status = errorAttributes.get("status");
250251
Assert.state(status instanceof Integer, "ErrorAttributes must contain a status integer");
251252
return (int) status;

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

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@
2323
import java.util.Map;
2424
import java.util.Optional;
2525

26+
import org.jspecify.annotations.Nullable;
27+
2628
import org.springframework.boot.web.error.Error;
2729
import org.springframework.boot.web.error.ErrorAttributeOptions;
2830
import org.springframework.boot.web.error.ErrorAttributeOptions.Include;
@@ -71,14 +73,15 @@ public class DefaultErrorAttributes implements ErrorAttributes {
7173
private static final String ERROR_INTERNAL_ATTRIBUTE = DefaultErrorAttributes.class.getName() + ".ERROR";
7274

7375
@Override
74-
public Map<String, Object> getErrorAttributes(ServerRequest request, ErrorAttributeOptions options) {
75-
Map<String, Object> errorAttributes = getErrorAttributes(request, options.isIncluded(Include.STACK_TRACE));
76+
public Map<String, @Nullable Object> getErrorAttributes(ServerRequest request, ErrorAttributeOptions options) {
77+
Map<String, @Nullable Object> errorAttributes = getErrorAttributes(request,
78+
options.isIncluded(Include.STACK_TRACE));
7679
options.retainIncluded(errorAttributes);
7780
return errorAttributes;
7881
}
7982

80-
private Map<String, Object> getErrorAttributes(ServerRequest request, boolean includeStackTrace) {
81-
Map<String, Object> errorAttributes = new LinkedHashMap<>();
83+
private Map<String, @Nullable Object> getErrorAttributes(ServerRequest request, boolean includeStackTrace) {
84+
Map<String, @Nullable Object> errorAttributes = new LinkedHashMap<>();
8285
errorAttributes.put("timestamp", new Date());
8386
errorAttributes.put("path", request.requestPath().value());
8487
Throwable error = getError(request);
@@ -103,14 +106,14 @@ private HttpStatus determineHttpStatus(Throwable error, MergedAnnotation<Respons
103106
return responseStatusAnnotation.getValue("code", HttpStatus.class).orElse(HttpStatus.INTERNAL_SERVER_ERROR);
104107
}
105108

106-
private void addStackTrace(Map<String, Object> errorAttributes, Throwable error) {
109+
private void addStackTrace(Map<String, @Nullable Object> errorAttributes, Throwable error) {
107110
StringWriter stackTrace = new StringWriter();
108111
error.printStackTrace(new PrintWriter(stackTrace));
109112
stackTrace.flush();
110113
errorAttributes.put("trace", stackTrace.toString());
111114
}
112115

113-
private void handleException(Map<String, Object> errorAttributes, Throwable error,
116+
private void handleException(Map<String, @Nullable Object> errorAttributes, Throwable error,
114117
MergedAnnotation<ResponseStatus> responseStatusAnnotation, boolean includeStackTrace) {
115118
Throwable exception;
116119
if (error instanceof BindingResult bindingResult) {

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

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

0 commit comments

Comments
 (0)