Skip to content

Commit

Permalink
Refine null-safety in spring-webflux
Browse files Browse the repository at this point in the history
  • Loading branch information
sdeleuze committed Mar 22, 2024
1 parent cf9033a commit 87dfa49
Show file tree
Hide file tree
Showing 16 changed files with 26 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import org.springframework.context.annotation.Configuration;
import org.springframework.format.FormatterRegistry;
import org.springframework.http.codec.ServerCodecConfigurer;
import org.springframework.lang.Nullable;
import org.springframework.util.CollectionUtils;
import org.springframework.validation.MessageCodesResolver;
import org.springframework.validation.Validator;
Expand Down Expand Up @@ -63,12 +64,14 @@ protected void addFormatters(FormatterRegistry registry) {
}

@Override
@Nullable
protected Validator getValidator() {
Validator validator = this.configurers.getValidator();
return (validator != null ? validator : super.getValidator());
}

@Override
@Nullable
protected MessageCodesResolver getMessageCodesResolver() {
MessageCodesResolver messageCodesResolver = this.configurers.getMessageCodesResolver();
return (messageCodesResolver != null ? messageCodesResolver : super.getMessageCodesResolver());
Expand Down Expand Up @@ -110,6 +113,7 @@ protected void configureViewResolvers(ViewResolverRegistry registry) {
}

@Override
@Nullable
protected WebSocketService getWebSocketService() {
WebSocketService service = this.configurers.getWebSocketService();
return (service != null ? service : super.getWebSocketService());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,11 +61,13 @@ public void addFormatters(FormatterRegistry registry) {
}

@Override
@Nullable
public Validator getValidator() {
return createSingleBean(WebFluxConfigurer::getValidator, Validator.class);
}

@Override
@Nullable
public MessageCodesResolver getMessageCodesResolver() {
return createSingleBean(WebFluxConfigurer::getMessageCodesResolver, MessageCodesResolver.class);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,7 @@ public Map<String, Object> attributes() {
}

@Override
@Nullable
public Consumer<ClientHttpRequest> httpRequest() {
return this.httpRequestConsumer;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package org.springframework.web.reactive.function.client;

import org.springframework.core.NestedRuntimeException;
import org.springframework.lang.Nullable;

/**
* Abstract base class for exception published by {@link WebClient} in case of errors.
Expand All @@ -42,7 +43,7 @@ public WebClientException(String msg) {
* @param msg the message
* @param ex the exception
*/
public WebClientException(String msg, Throwable ex) {
public WebClientException(@Nullable String msg, Throwable ex) {
super(msg, ex);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
import org.springframework.http.server.PathContainer;
import org.springframework.http.server.RequestPath;
import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.lang.Nullable;
import org.springframework.util.Assert;
import org.springframework.util.MultiValueMap;
import org.springframework.web.bind.WebDataBinder;
Expand Down Expand Up @@ -283,6 +284,7 @@ public Optional<MediaType> contentType() {
}

@Override
@Nullable
public InetSocketAddress host() {
return this.headers.host();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ protected Object lookupHandler(PathContainer lookupPath, ServerWebExchange excha
* @param exchange current exchange
*/
@SuppressWarnings("UnusedParameters")
protected void validateHandler(Object handler, ServerWebExchange exchange) {
protected void validateHandler(@Nullable Object handler, ServerWebExchange exchange) {
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import org.springframework.lang.Nullable;
import org.springframework.util.StringUtils;

/**
Expand All @@ -40,6 +41,7 @@ public abstract class AbstractFileNameVersionStrategy implements VersionStrategy


@Override
@Nullable
public String extractVersion(String requestPath) {
Matcher matcher = pattern.matcher(requestPath);
if (matcher.find()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import org.springframework.lang.Nullable;
import org.springframework.util.Assert;

/**
Expand All @@ -44,6 +45,7 @@ protected AbstractPrefixVersionStrategy(String version) {


@Override
@Nullable
public String extractVersion(String requestPath) {
return (requestPath.startsWith(this.prefix) ? this.prefix : null);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -400,6 +400,7 @@ protected boolean hasCorsConfigurationSource(Object handler) {
}

@Override
@Nullable
protected CorsConfiguration getCorsConfiguration(Object handler, ServerWebExchange exchange) {
CorsConfiguration corsConfig = super.getCorsConfiguration(handler, exchange);
if (handler instanceof HandlerMethod handlerMethod) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
import org.springframework.http.server.PathContainer;
import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.http.server.reactive.observation.ServerRequestObservationContext;
import org.springframework.lang.Nullable;
import org.springframework.util.Assert;
import org.springframework.util.CollectionUtils;
import org.springframework.util.MultiValueMap;
Expand Down Expand Up @@ -87,6 +88,7 @@ protected Set<String> getDirectPaths(RequestMappingInfo info) {
* @return an info in case of a match; or {@code null} otherwise.
*/
@Override
@Nullable
protected RequestMappingInfo getMatchingMapping(RequestMappingInfo info, ServerWebExchange exchange) {
return info.getMatchingCondition(exchange);
}
Expand Down Expand Up @@ -171,6 +173,7 @@ protected void handleMatch(RequestMappingInfo info, HandlerMethod handlerMethod,
* method but not by query parameter conditions
*/
@Override
@Nullable
protected HandlerMethod handleNoMatch(Set<RequestMappingInfo> infos,
ServerWebExchange exchange) throws Exception {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ protected NamedValueInfo createNamedValueInfo(MethodParameter parameter) {
}

@Override
@Nullable
protected Object resolveNamedValue(String name, MethodParameter parameter, ServerWebExchange exchange) {
HttpCookie cookie = exchange.getRequest().getCookies().getFirst(name);
Class<?> paramType = parameter.getNestedParameterType();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ protected NamedValueInfo createNamedValueInfo(MethodParameter parameter) {
}

@Override
@Nullable
protected Object resolveNamedValue(String name, MethodParameter parameter, ServerWebExchange exchange) {
// No name to resolve
return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ protected NamedValueInfo createNamedValueInfo(MethodParameter parameter) {
}

@Override
@Nullable
protected Object resolveNamedValue(String name, MethodParameter parameter, ServerWebExchange exchange) {
String attributeName = HandlerMapping.URI_TEMPLATE_VARIABLES_ATTRIBUTE;
return exchange.getAttributeOrDefault(attributeName, Collections.emptyMap()).get(name);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ protected NamedValueInfo createNamedValueInfo(MethodParameter parameter) {
}

@Override
@Nullable
protected Object resolveNamedValue(String name, MethodParameter parameter, ServerWebExchange exchange) {
List<String> headerValues = exchange.getRequest().getHeaders().get(name);
Object result = null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -356,6 +356,7 @@ private void updateConsumesCondition(RequestMappingInfo info, Method method) {
}

@Override
@Nullable
protected CorsConfiguration initCorsConfiguration(Object handler, Method method, RequestMappingInfo mappingInfo) {
HandlerMethod handlerMethod = createHandlerMethod(handler, method);
Class<?> beanType = handlerMethod.getBeanType();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ protected NamedValueInfo createNamedValueInfo(MethodParameter parameter) {
}

@Override
@Nullable
protected Object resolveNamedValue(String name, MethodParameter parameter, ServerWebExchange exchange) {
List<String> paramValues = exchange.getRequest().getQueryParams().get(name);
Object result = null;
Expand Down

0 comments on commit 87dfa49

Please sign in to comment.