use of org.springframework.boot.context.properties.source.ConfigurationProperty in project spring-boot by spring-projects.
the class MapBinder method bindAggregate.
@Override
protected Object bindAggregate(ConfigurationPropertyName name, Bindable<?> target, AggregateElementBinder elementBinder) {
Map<Object, Object> map = CollectionFactory.createMap((target.getValue() != null) ? Map.class : target.getType().resolve(Object.class), 0);
Bindable<?> resolvedTarget = resolveTarget(target);
boolean hasDescendants = hasDescendants(name);
for (ConfigurationPropertySource source : getContext().getSources()) {
if (!ConfigurationPropertyName.EMPTY.equals(name)) {
ConfigurationProperty property = source.getConfigurationProperty(name);
if (property != null && !hasDescendants) {
return getContext().getConverter().convert(property.getValue(), target);
}
source = source.filter(name::isAncestorOf);
}
new EntryBinder(name, resolvedTarget, elementBinder).bindEntries(source, map);
}
return map.isEmpty() ? null : map;
}
use of org.springframework.boot.context.properties.source.ConfigurationProperty in project spring-boot by spring-projects.
the class Binder method bindObject.
private <T> Object bindObject(ConfigurationPropertyName name, Bindable<T> target, BindHandler handler, Context context, boolean allowRecursiveBinding) {
ConfigurationProperty property = findProperty(name, target, context);
if (property == null && context.depth != 0 && containsNoDescendantOf(context.getSources(), name)) {
return null;
}
AggregateBinder<?> aggregateBinder = getAggregateBinder(target, context);
if (aggregateBinder != null) {
return bindAggregate(name, target, handler, context, aggregateBinder);
}
if (property != null) {
try {
return bindProperty(target, context, property);
} catch (ConverterNotFoundException ex) {
// We might still be able to bind it using the recursive binders
Object instance = bindDataObject(name, target, handler, context, allowRecursiveBinding);
if (instance != null) {
return instance;
}
throw ex;
}
}
return bindDataObject(name, target, handler, context, allowRecursiveBinding);
}
use of org.springframework.boot.context.properties.source.ConfigurationProperty in project spring-boot by spring-projects.
the class ValidationBindHandlerTests method bindShouldFailWithAccessToBoundProperties.
@Test
void bindShouldFailWithAccessToBoundProperties() {
MockConfigurationPropertySource source = new MockConfigurationPropertySource();
source.put("foo.nested.name", "baz");
source.put("foo.nested.age", "4");
source.put("faf.bar", "baz");
this.sources.add(source);
BindValidationException cause = bindAndExpectValidationError(() -> this.binder.bind(ConfigurationPropertyName.of("foo"), Bindable.of(ExampleValidatedWithNestedBean.class), this.handler));
Set<ConfigurationProperty> boundProperties = cause.getValidationErrors().getBoundProperties();
assertThat(boundProperties).extracting((p) -> p.getName().toString()).contains("foo.nested.age", "foo.nested.name");
}
use of org.springframework.boot.context.properties.source.ConfigurationProperty in project spring-boot by spring-projects.
the class ValidationErrorsTests method getErrorsShouldAdaptFieldErrorsToBeOriginProviders.
@Test
void getErrorsShouldAdaptFieldErrorsToBeOriginProviders() {
Set<ConfigurationProperty> boundProperties = new LinkedHashSet<>();
ConfigurationPropertyName name1 = ConfigurationPropertyName.of("foo.bar");
Origin origin1 = MockOrigin.of("line1");
boundProperties.add(new ConfigurationProperty(name1, "boot", origin1));
ConfigurationPropertyName name2 = ConfigurationPropertyName.of("foo.baz.bar");
Origin origin2 = MockOrigin.of("line2");
boundProperties.add(new ConfigurationProperty(name2, "boot", origin2));
List<ObjectError> allErrors = new ArrayList<>();
allErrors.add(new FieldError("objectname", "bar", "message"));
ValidationErrors errors = new ValidationErrors(ConfigurationPropertyName.of("foo.baz"), boundProperties, allErrors);
assertThat(Origin.from(errors.getAllErrors().get(0))).isEqualTo(origin2);
}
use of org.springframework.boot.context.properties.source.ConfigurationProperty in project spring-boot by spring-projects.
the class ValidationErrorsTests method getBoundPropertiesShouldReturnBoundProperties.
@Test
void getBoundPropertiesShouldReturnBoundProperties() {
Set<ConfigurationProperty> boundProperties = new LinkedHashSet<>();
boundProperties.add(new ConfigurationProperty(NAME, "foo", null));
ValidationErrors errors = new ValidationErrors(NAME, boundProperties, Collections.emptyList());
assertThat(errors.getBoundProperties()).isEqualTo(boundProperties);
}
Aggregations