Skip to content

Commit

Permalink
missingAfterConversion flag for missing request values
Browse files Browse the repository at this point in the history
  • Loading branch information
rstoyanchev authored and lxbzmy committed Mar 26, 2022
1 parent c2aa4ba commit b5d6b92
Show file tree
Hide file tree
Showing 12 changed files with 206 additions and 32 deletions.
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2018 the original author or authors.
* Copyright 2002-2021 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -28,7 +28,7 @@
* @see MissingPathVariableException
*/
@SuppressWarnings("serial")
public class MissingMatrixVariableException extends ServletRequestBindingException {
public class MissingMatrixVariableException extends MissingRequestValueException {

private final String variableName;

Expand All @@ -41,16 +41,30 @@ public class MissingMatrixVariableException extends ServletRequestBindingExcepti
* @param parameter the method parameter
*/
public MissingMatrixVariableException(String variableName, MethodParameter parameter) {
super("");
this(variableName, parameter, false);
}

/**
* Constructor for use when a value was present but converted to {@code null}.
* @param variableName the name of the missing matrix variable
* @param parameter the method parameter
* @param missingAfterConversion whether the value became null after conversion
* @since 5.3.6
*/
public MissingMatrixVariableException(
String variableName, MethodParameter parameter, boolean missingAfterConversion) {

super("", missingAfterConversion);
this.variableName = variableName;
this.parameter = parameter;
}


@Override
public String getMessage() {
return "Missing matrix variable '" + this.variableName +
"' for method parameter of type " + this.parameter.getNestedParameterType().getSimpleName();
return "Required matrix variable '" + this.variableName + "' for method parameter type " +
this.parameter.getNestedParameterType().getSimpleName() + " is " +
(isMissingAfterConversion() ? "present but converted to null" : "not present");
}

/**
Expand Down
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2018 the original author or authors.
* Copyright 2002-2021 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -30,7 +30,7 @@
* @see MissingMatrixVariableException
*/
@SuppressWarnings("serial")
public class MissingPathVariableException extends ServletRequestBindingException {
public class MissingPathVariableException extends MissingRequestValueException {

private final String variableName;

Expand All @@ -43,16 +43,30 @@ public class MissingPathVariableException extends ServletRequestBindingException
* @param parameter the method parameter
*/
public MissingPathVariableException(String variableName, MethodParameter parameter) {
super("");
this(variableName, parameter, false);
}

/**
* Constructor for use when a value was present but converted to {@code null}.
* @param variableName the name of the missing path variable
* @param parameter the method parameter
* @param missingAfterConversion whether the value became null after conversion
* @since 5.3.6
*/
public MissingPathVariableException(
String variableName, MethodParameter parameter, boolean missingAfterConversion) {

super("", missingAfterConversion);
this.variableName = variableName;
this.parameter = parameter;
}


@Override
public String getMessage() {
return "Missing URI template variable '" + this.variableName +
"' for method parameter of type " + this.parameter.getNestedParameterType().getSimpleName();
return "Required URI template variable '" + this.variableName + "' for method parameter type " +
this.parameter.getNestedParameterType().getSimpleName() + " is " +
(isMissingAfterConversion() ? "present but converted to null" : "not present");
}

/**
Expand Down
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2018 the original author or authors.
* Copyright 2002-2021 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -28,7 +28,7 @@
* @see MissingRequestHeaderException
*/
@SuppressWarnings("serial")
public class MissingRequestCookieException extends ServletRequestBindingException {
public class MissingRequestCookieException extends MissingRequestValueException {

private final String cookieName;

Expand All @@ -41,16 +41,30 @@ public class MissingRequestCookieException extends ServletRequestBindingExceptio
* @param parameter the method parameter
*/
public MissingRequestCookieException(String cookieName, MethodParameter parameter) {
super("");
this(cookieName, parameter, false);
}

/**
* Constructor for use when a value was present but converted to {@code null}.
* @param cookieName the name of the missing request cookie
* @param parameter the method parameter
* @param missingAfterConversion whether the value became null after conversion
* @since 5.3.6
*/
public MissingRequestCookieException(
String cookieName, MethodParameter parameter, boolean missingAfterConversion) {

super("", missingAfterConversion);
this.cookieName = cookieName;
this.parameter = parameter;
}


@Override
public String getMessage() {
return "Missing cookie '" + this.cookieName +
"' for method parameter of type " + this.parameter.getNestedParameterType().getSimpleName();
return "Required cookie '" + this.cookieName + "' for method parameter type " +
this.parameter.getNestedParameterType().getSimpleName() + " is " +
(isMissingAfterConversion() ? "present but converted to null" : "not present");
}

/**
Expand Down
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2018 the original author or authors.
* Copyright 2002-2021 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -28,7 +28,7 @@
* @see MissingRequestCookieException
*/
@SuppressWarnings("serial")
public class MissingRequestHeaderException extends ServletRequestBindingException {
public class MissingRequestHeaderException extends MissingRequestValueException {

private final String headerName;

Expand All @@ -41,16 +41,30 @@ public class MissingRequestHeaderException extends ServletRequestBindingExceptio
* @param parameter the method parameter
*/
public MissingRequestHeaderException(String headerName, MethodParameter parameter) {
super("");
this(headerName, parameter, false);
}

/**
* Constructor for use when a value was present but converted to {@code null}.
* @param headerName the name of the missing request header
* @param parameter the method parameter
* @param missingAfterConversion whether the value became null after conversion
* @since 5.3.6
*/
public MissingRequestHeaderException(
String headerName, MethodParameter parameter, boolean missingAfterConversion) {

super("", missingAfterConversion);
this.headerName = headerName;
this.parameter = parameter;
}


@Override
public String getMessage() {
return "Missing request header '" + this.headerName +
"' for method parameter of type " + this.parameter.getNestedParameterType().getSimpleName();
String typeName = this.parameter.getNestedParameterType().getSimpleName();
return "Required request header '" + this.headerName + "' for method parameter type " + typeName + " is " +
(isMissingAfterConversion() ? "present but converted to null" : "not present");
}

/**
Expand Down
@@ -0,0 +1,50 @@
/*
* Copyright 2002-2021 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.web.bind;

/**
* Base class for {@link ServletRequestBindingException} exceptions that could
* not bind because the request value is required but is either missing or
* otherwise resolves to {@code null} after conversion.
*
* @author Rossen Stoyanchev
* @since 5.3.6
*/
@SuppressWarnings("serial")
public class MissingRequestValueException extends ServletRequestBindingException {

private final boolean missingAfterConversion;


public MissingRequestValueException(String msg) {
this(msg, false);
}

public MissingRequestValueException(String msg, boolean missingAfterConversion) {
super(msg);
this.missingAfterConversion = missingAfterConversion;
}


/**
* Whether the request value was present but converted to {@code null}, e.g. via
* {@code org.springframework.core.convert.support.IdToEntityConverter}.
*/
public boolean isMissingAfterConversion() {
return this.missingAfterConversion;
}

}
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2012 the original author or authors.
* Copyright 2002-2021 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -23,7 +23,7 @@
* @since 2.0.2
*/
@SuppressWarnings("serial")
public class MissingServletRequestParameterException extends ServletRequestBindingException {
public class MissingServletRequestParameterException extends MissingRequestValueException {

private final String parameterName;

Expand All @@ -36,15 +36,30 @@ public class MissingServletRequestParameterException extends ServletRequestBindi
* @param parameterType the expected type of the missing parameter
*/
public MissingServletRequestParameterException(String parameterName, String parameterType) {
super("");
this(parameterName, parameterType, false);
}

/**
* Constructor for use when a value was present but converted to {@code null}.
* @param parameterName the name of the missing parameter
* @param parameterType the expected type of the missing parameter
* @param missingAfterConversion whether the value became null after conversion
* @since 5.3.6
*/
public MissingServletRequestParameterException(
String parameterName, String parameterType, boolean missingAfterConversion) {

super("", missingAfterConversion);
this.parameterName = parameterName;
this.parameterType = parameterType;
}


@Override
public String getMessage() {
return "Required " + this.parameterType + " parameter '" + this.parameterName + "' is not present";
return "Required request parameter '" + this.parameterName + "' for method parameter type " +
this.parameterType + " is " +
(isMissingAfterConversion() ? "present but converted to null" : "not present");
}

/**
Expand Down
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2018 the original author or authors.
* Copyright 2002-2021 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -24,6 +24,7 @@
import org.springframework.web.bind.ServletRequestBindingException;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.CookieValue;
import org.springframework.web.context.request.NativeWebRequest;

/**
* A base abstract class to resolve method arguments annotated with
Expand Down Expand Up @@ -70,6 +71,12 @@ protected void handleMissingValue(String name, MethodParameter parameter) throws
throw new MissingRequestCookieException(name, parameter);
}

@Override
protected void handleMissingValueAfterConversion(
String name, MethodParameter parameter, NativeWebRequest request) throws Exception {

throw new MissingRequestCookieException(name, parameter, true);
}

private static final class CookieValueNamedValueInfo extends NamedValueInfo {

Expand Down
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2020 the original author or authors.
* Copyright 2002-2021 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -135,7 +135,7 @@ else if ("".equals(arg) && namedValueInfo.defaultValue != null) {
// Check for null value after conversion of incoming argument value
if (arg == null && namedValueInfo.defaultValue == null &&
namedValueInfo.required && !nestedParameter.isOptional()) {
handleMissingValue(namedValueInfo.name, nestedParameter, webRequest);
handleMissingValueAfterConversion(namedValueInfo.name, nestedParameter, webRequest);
}
}

Expand Down Expand Up @@ -237,6 +237,19 @@ protected void handleMissingValue(String name, MethodParameter parameter) throws
"' for method parameter of type " + parameter.getNestedParameterType().getSimpleName());
}

/**
* Invoked when a named value is present but becomes {@code null} after conversion.
* @param name the name for the value
* @param parameter the method parameter
* @param request the current request
* @since 5.3.6
*/
protected void handleMissingValueAfterConversion(String name, MethodParameter parameter, NativeWebRequest request)
throws Exception {

handleMissingValue(name, parameter, request);
}

/**
* A {@code null} results in a {@code false} value for {@code boolean}s or an exception for other primitives.
*/
Expand Down
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2018 the original author or authors.
* Copyright 2002-2021 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -87,6 +87,12 @@ protected void handleMissingValue(String name, MethodParameter parameter) throws
throw new MissingRequestHeaderException(name, parameter);
}

@Override
protected void handleMissingValueAfterConversion(
String name, MethodParameter parameter, NativeWebRequest request) throws Exception {

throw new MissingRequestHeaderException(name, parameter, true);
}

private static final class RequestHeaderNamedValueInfo extends NamedValueInfo {

Expand Down

0 comments on commit b5d6b92

Please sign in to comment.