Skip to content

Commit

Permalink
Support BindParam annotation
Browse files Browse the repository at this point in the history
Allows customizing the name of the request parameter to bind a
constructor parameter to.

Closes gh-30947
  • Loading branch information
rstoyanchev committed Jul 25, 2023
1 parent ccaccda commit 37eaded
Show file tree
Hide file tree
Showing 7 changed files with 329 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,9 @@ public class DataBinder implements PropertyEditorRegistry, TypeConverter {
@Nullable
private String[] requiredFields;

@Nullable
private NameResolver nameResolver;

@Nullable
private ConversionService conversionService;

Expand Down Expand Up @@ -225,7 +228,7 @@ public String getObjectName() {

/**
* Set the type for the target object. When the target is {@code null},
* setting the targetType allows using {@link #construct(ValueResolver)} to
* setting the targetType allows using {@link #construct} to
* create the target.
* @param targetType the type of the target object
* @since 6.1
Expand All @@ -252,7 +255,7 @@ public ResolvableType getTargetType() {
* <p>Default is "true" on a standard DataBinder. Note that since Spring 4.1 this feature is supported
* for bean property access (DataBinder's default mode) and field access.
* <p>Used for setter/field injection via {@link #bind(PropertyValues)}, and not
* applicable to constructor initialization via {@link #construct(ValueResolver)}.
* applicable to constructor binding via {@link #construct}.
* @see #initBeanPropertyAccess()
* @see org.springframework.beans.BeanWrapper#setAutoGrowNestedPaths
*/
Expand All @@ -274,7 +277,7 @@ public boolean isAutoGrowNestedPaths() {
* <p>Default is 256, preventing OutOfMemoryErrors in case of large indexes.
* Raise this limit if your auto-growing needs are unusually high.
* <p>Used for setter/field injection via {@link #bind(PropertyValues)}, and not
* applicable to constructor initialization via {@link #construct(ValueResolver)}.
* applicable to constructor binding via {@link #construct}.
* @see #initBeanPropertyAccess()
* @see org.springframework.beans.BeanWrapper#setAutoGrowCollectionLimit
*/
Expand Down Expand Up @@ -431,8 +434,8 @@ public BindingResult getBindingResult() {
* <p>Note that this setting only applies to <i>binding</i> operations
* on this DataBinder, not to <i>retrieving</i> values via its
* {@link #getBindingResult() BindingResult}.
* <p>Used for setter/field inject via {@link #bind(PropertyValues)}, and not
* applicable to constructor initialization via {@link #construct(ValueResolver)},
* <p>Used for binding to fields with {@link #bind(PropertyValues)}, and not
* applicable to constructor binding via {@link #construct},
* which uses only the values it needs.
* @see #bind
*/
Expand All @@ -456,8 +459,8 @@ public boolean isIgnoreUnknownFields() {
* <p>Note that this setting only applies to <i>binding</i> operations
* on this DataBinder, not to <i>retrieving</i> values via its
* {@link #getBindingResult() BindingResult}.
* <p>Used for setter/field inject via {@link #bind(PropertyValues)}, and not
* applicable to constructor initialization via {@link #construct(ValueResolver)},
* <p>Used for binding to fields with {@link #bind(PropertyValues)}, and not
* applicable to constructor binding via {@link #construct},
* which uses only the values it needs.
* @see #bind
*/
Expand Down Expand Up @@ -487,8 +490,8 @@ public boolean isIgnoreInvalidFields() {
* <p>More sophisticated matching can be implemented by overriding the
* {@link #isAllowed} method.
* <p>Alternatively, specify a list of <i>disallowed</i> field patterns.
* <p>Used for setter/field inject via {@link #bind(PropertyValues)}, and not
* applicable to constructor initialization via {@link #construct(ValueResolver)},
* <p>Used for binding to fields with {@link #bind(PropertyValues)}, and not
* applicable to constructor binding via {@link #construct},
* which uses only the values it needs.
* @param allowedFields array of allowed field patterns
* @see #setDisallowedFields
Expand Down Expand Up @@ -526,8 +529,8 @@ public String[] getAllowedFields() {
* <p>More sophisticated matching can be implemented by overriding the
* {@link #isAllowed} method.
* <p>Alternatively, specify a list of <i>allowed</i> field patterns.
* <p>Used for setter/field inject via {@link #bind(PropertyValues)}, and not
* applicable to constructor initialization via {@link #construct(ValueResolver)},
* <p>Used for binding to fields with {@link #bind(PropertyValues)}, and not
* applicable to constructor binding via {@link #construct},
* which uses only the values it needs.
* @param disallowedFields array of disallowed field patterns
* @see #setAllowedFields
Expand Down Expand Up @@ -562,8 +565,8 @@ public String[] getDisallowedFields() {
* incoming property values, a corresponding "missing field" error
* will be created, with error code "required" (by the default
* binding error processor).
* <p>Used for setter/field inject via {@link #bind(PropertyValues)}, and not
* applicable to constructor initialization via {@link #construct(ValueResolver)},
* <p>Used for binding to fields with {@link #bind(PropertyValues)}, and not
* applicable to constructor binding via {@link #construct},
* which uses only the values it needs.
* @param requiredFields array of field names
* @see #setBindingErrorProcessor
Expand All @@ -586,6 +589,28 @@ public String[] getRequiredFields() {
return this.requiredFields;
}

/**
* Configure a resolver to determine the name of the value to bind to a
* constructor parameter in {@link #construct}.
* <p>If not configured, or if the name cannot be resolved, by default
* {@link org.springframework.core.DefaultParameterNameDiscoverer} is used.
* @param nameResolver the resolver to use
* @since 6.1
*/
public void setNameResolver(NameResolver nameResolver) {
this.nameResolver = nameResolver;
}

/**
* Return the {@link #setNameResolver configured} name resolver for
* constructor parameters.
* @since 6.1
*/
@Nullable
public NameResolver getNameResolver() {
return this.nameResolver;
}

/**
* Set the strategy to use for resolving errors into message codes.
* Applies the given strategy to the underlying errors holder.
Expand Down Expand Up @@ -885,11 +910,19 @@ private Object createObject(ResolvableType objectType, String nestedPath, ValueR
Set<String> failedParamNames = new HashSet<>(4);

for (int i = 0; i < paramNames.length; i++) {
String paramPath = nestedPath + paramNames[i];
MethodParameter param = MethodParameter.forFieldAwareConstructor(ctor, i, paramNames[i]);
String lookupName = null;
if (this.nameResolver != null) {
lookupName = this.nameResolver.resolveName(param);
}
if (lookupName == null) {
lookupName = paramNames[i];
}

String paramPath = nestedPath + lookupName;
Class<?> paramType = paramTypes[i];
Object value = valueResolver.resolveValue(paramPath, paramType);

MethodParameter param = MethodParameter.forFieldAwareConstructor(ctor, i, paramNames[i]);
if (value == null && !BeanUtils.isSimpleValueType(param.nestedIfOptional().getNestedParameterType())) {
ResolvableType type = ResolvableType.forMethodParameter(param);
args[i] = createObject(type, paramPath + ".", valueResolver);
Expand Down Expand Up @@ -1188,16 +1221,36 @@ else if (validator != null) {


/**
* Contract to resolve a value in {@link #construct(ValueResolver)}.
* Strategy to determine the name of the value to bind to a method parameter.
* Supported on constructor parameters with {@link #construct constructor
* binding} which performs lookups via {@link ValueResolver#resolveValue}.
*/
public interface NameResolver {

/**
* Return the name to use for the given method parameter, or {@code null}
* if unresolved. For constructor parameters, the name is determined via
* {@link org.springframework.core.DefaultParameterNameDiscoverer} if
* unresolved.
*/
@Nullable
String resolveName(MethodParameter parameter);

}

/**
* Strategy for {@link #construct constructor binding} to look up the values
* to bind to a given constructor parameter.
*/
@FunctionalInterface
public interface ValueResolver {

/**
* Look up the value for a constructor argument.
* @param name the argument name
* @param type the argument type
* @return the resolved value, possibly {@code null}
* Resolve the value for the given name and target parameter type.
* @param name the name to use for the lookup, possibly a nested path
* for constructor parameters on nested objects
* @param type the target type, based on the constructor parameter type
* @return the resolved value, possibly {@code null} if none found
*/
@Nullable
Object resolveValue(String name, Class<?> type);
Expand All @@ -1217,5 +1270,4 @@ public void registerCustomEditors(PropertyEditorRegistry registry) {
}
}


}
Original file line number Diff line number Diff line change
@@ -0,0 +1,154 @@
/*
* 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.
* 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.validation;

import java.beans.ConstructorProperties;
import java.util.Map;
import java.util.Optional;

import jakarta.validation.constraints.NotNull;
import org.junit.jupiter.api.Test;

import org.springframework.core.ResolvableType;
import org.springframework.format.support.DefaultFormattingConversionService;
import org.springframework.util.Assert;

import static org.assertj.core.api.Assertions.assertThat;

/**
* Unit tests for {@link DataBinder} with constructor binding.
*
* @author Rossen Stoyanchev
*/
public class DataBinderConstructTests {


@Test
void dataClassBinding() {
MapValueResolver valueResolver = new MapValueResolver(Map.of("param1", "value1", "param2", "true"));
DataBinder binder = initDataBinder(DataClass.class);
binder.construct(valueResolver);

DataClass dataClass = getTarget(binder);
assertThat(dataClass.param1()).isEqualTo("value1");
assertThat(dataClass.param2()).isEqualTo(true);
assertThat(dataClass.param3()).isEqualTo(0);
}

@Test
void dataClassBindingWithOptionalParameter() {
MapValueResolver valueResolver =
new MapValueResolver(Map.of("param1", "value1", "param2", "true", "optionalParam", "8"));

DataBinder binder = initDataBinder(DataClass.class);
binder.construct(valueResolver);

DataClass dataClass = getTarget(binder);
assertThat(dataClass.param1()).isEqualTo("value1");
assertThat(dataClass.param2()).isEqualTo(true);
assertThat(dataClass.param3()).isEqualTo(8);
}

@Test
void dataClassBindingWithMissingParameter() {
MapValueResolver valueResolver = new MapValueResolver(Map.of("param1", "value1"));
DataBinder binder = initDataBinder(DataClass.class);
binder.construct(valueResolver);

BindingResult bindingResult = binder.getBindingResult();
assertThat(bindingResult.getAllErrors()).hasSize(1);
assertThat(bindingResult.getFieldValue("param1")).isEqualTo("value1");
assertThat(bindingResult.getFieldValue("param2")).isNull();
assertThat(bindingResult.getFieldValue("param3")).isNull();
}

@Test
void dataClassBindingWithConversionError() {
MapValueResolver valueResolver = new MapValueResolver(Map.of("param1", "value1", "param2", "x"));
DataBinder binder = initDataBinder(DataClass.class);
binder.construct(valueResolver);

BindingResult bindingResult = binder.getBindingResult();
assertThat(bindingResult.getAllErrors()).hasSize(1);
assertThat(bindingResult.getFieldValue("param1")).isEqualTo("value1");
assertThat(bindingResult.getFieldValue("param2")).isEqualTo("x");
assertThat(bindingResult.getFieldValue("param3")).isNull();
}

@SuppressWarnings("SameParameterValue")
private static DataBinder initDataBinder(Class<DataClass> targetType) {
DataBinder binder = new DataBinder(null);
binder.setTargetType(ResolvableType.forClass(targetType));
binder.setConversionService(new DefaultFormattingConversionService());
return binder;
}

@SuppressWarnings("unchecked")
private static <T> T getTarget(DataBinder dataBinder) {
assertThat(dataBinder.getBindingResult().getAllErrors()).isEmpty();
Object target = dataBinder.getTarget();
assertThat(target).isNotNull();
return (T) target;
}


private static class DataClass {

@NotNull
private final String param1;

private final boolean param2;

private int param3;

@ConstructorProperties({"param1", "param2", "optionalParam"})
DataClass(String param1, boolean p2, Optional<Integer> optionalParam) {
this.param1 = param1;
this.param2 = p2;
Assert.notNull(optionalParam, "Optional must not be null");
optionalParam.ifPresent(integer -> this.param3 = integer);
}

public String param1() {
return this.param1;
}

public boolean param2() {
return this.param2;
}

public int param3() {
return this.param3;
}
}


private static class MapValueResolver implements DataBinder.ValueResolver {

private final Map<String, Object> values;

private MapValueResolver(Map<String, Object> values) {
this.values = values;
}

@Override
public Object resolveValue(String name, Class<?> type) {
return values.get(name);
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/*
* Copyright 2002-2018 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.annotation;

import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

/**
* Annotation to bind values from a web request such as request parameters or
* path variables to fields of a Java object. Supported on constructor parameters
* of {@link ModelAttribute @ModelAttribute} controller method arguments
*
* @author Rossen Stoyanchev
* @since 6.1
* @see org.springframework.web.bind.WebDataBinder#construct
*/
@Target(ElementType.PARAMETER)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface BindParam {

/**
* The lookup name to use for the bind value.
*/
String value() default "";

}
Loading

0 comments on commit 37eaded

Please sign in to comment.