Skip to content

Commit

Permalink
Expose MethodParameter in MissingServletRequestParameterException
Browse files Browse the repository at this point in the history
  • Loading branch information
rstoyanchev committed Jun 13, 2023
1 parent 6b50b7b commit 1e3161b
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 6 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2022 the original author or authors.
* Copyright 2002-2023 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 @@ -16,6 +16,9 @@

package org.springframework.web.bind;

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

/**
* {@link ServletRequestBindingException} subclass that indicates a missing parameter.
*
Expand All @@ -29,14 +32,38 @@ public class MissingServletRequestParameterException extends MissingRequestValue

private final String parameterType;

@Nullable
private final MethodParameter parameter;


/**
* Constructor for MissingServletRequestParameterException.
* @param parameterName the name of the missing parameter
* @param parameterType the expected type of the missing parameter
*/
public MissingServletRequestParameterException(String parameterName, String parameterType) {
this(parameterName, parameterType, false);
super("", false, null, new Object[] {parameterName});
this.parameterName = parameterName;
this.parameterType = parameterType;
this.parameter = null;
getBody().setDetail(initBodyDetail(this.parameterName));
}

/**
* Constructor with a {@link MethodParameter} instead of a String parameterType.
* @param parameterName the name of the missing parameter
* @param parameter the target method parameter for the missing value
* @param missingAfterConversion whether the value became null after conversion
* @since 6.1
*/
public MissingServletRequestParameterException(
String parameterName, MethodParameter parameter, boolean missingAfterConversion) {

super("", missingAfterConversion, null, new Object[] {parameterName});
this.parameterName = parameterName;
this.parameterType = parameter.getNestedParameterType().getSimpleName();
this.parameter = parameter;
getBody().setDetail(initBodyDetail(this.parameterName));
}

/**
Expand All @@ -45,14 +72,21 @@ public MissingServletRequestParameterException(String parameterName, String para
* @param parameterType the expected type of the missing parameter
* @param missingAfterConversion whether the value became null after conversion
* @since 5.3.6
* @deprecated in favor of {@link #MissingServletRequestParameterException(String, MethodParameter, boolean)}
*/
@Deprecated(since = "6.1", forRemoval = true)
public MissingServletRequestParameterException(
String parameterName, String parameterType, boolean missingAfterConversion) {

super("", missingAfterConversion, null, new Object[] {parameterName});
this.parameterName = parameterName;
this.parameterType = parameterType;
getBody().setDetail("Required parameter '" + this.parameterName + "' is not present.");
this.parameter = null;
getBody().setDetail(initBodyDetail(this.parameterName));
}

private static String initBodyDetail(String name) {
return "Required parameter '" + name + "' is not present.";
}


Expand All @@ -77,4 +111,14 @@ public final String getParameterType() {
return this.parameterType;
}

/**
* Return the target {@link MethodParameter} if the exception was raised for
* a controller method argument.
* @since 6.1
*/
@Nullable
public MethodParameter getMethodParameter() {
return this.parameter;
}

}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2022 the original author or authors.
* Copyright 2002-2023 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 @@ -214,8 +214,7 @@ protected void handleMissingValueInternal(
}
}
else {
throw new MissingServletRequestParameterException(name,
parameter.getNestedParameterType().getSimpleName(), missingAfterConversion);
throw new MissingServletRequestParameterException(name, parameter, missingAfterConversion);
}
}

Expand Down

0 comments on commit 1e3161b

Please sign in to comment.