Search in sources :

Example 11 with ConfigurationProperty

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;
}
Also used : ConfigurationProperty(org.springframework.boot.context.properties.source.ConfigurationProperty) ConfigurationPropertySource(org.springframework.boot.context.properties.source.ConfigurationPropertySource) IterableConfigurationPropertySource(org.springframework.boot.context.properties.source.IterableConfigurationPropertySource) Map(java.util.Map)

Example 12 with ConfigurationProperty

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);
}
Also used : ConfigurationProperty(org.springframework.boot.context.properties.source.ConfigurationProperty) ConverterNotFoundException(org.springframework.core.convert.ConverterNotFoundException)

Example 13 with ConfigurationProperty

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");
}
Also used : ConfigurationProperty(org.springframework.boot.context.properties.source.ConfigurationProperty) ConfigurationPropertySource(org.springframework.boot.context.properties.source.ConfigurationPropertySource) BeforeEach(org.junit.jupiter.api.BeforeEach) Errors(org.springframework.validation.Errors) MockConfigurationPropertySource(org.springframework.boot.context.properties.source.MockConfigurationPropertySource) Validator(org.springframework.validation.Validator) ConfigurationProperty(org.springframework.boot.context.properties.source.ConfigurationProperty) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) Valid(jakarta.validation.Valid) NotNull(jakarta.validation.constraints.NotNull) ArrayList(java.util.ArrayList) ConverterNotFoundException(org.springframework.core.convert.ConverterNotFoundException) LinkedHashMap(java.util.LinkedHashMap) ConfigurationPropertyName(org.springframework.boot.context.properties.source.ConfigurationPropertyName) ValidationUtils(org.springframework.validation.ValidationUtils) ObjectError(org.springframework.validation.ObjectError) Map(java.util.Map) Assertions.assertThatExceptionOfType(org.assertj.core.api.Assertions.assertThatExceptionOfType) ConfigurationPropertySources(org.springframework.boot.context.properties.source.ConfigurationPropertySources) LocalValidatorFactoryBean(org.springframework.validation.beanvalidation.LocalValidatorFactoryBean) BindContext(org.springframework.boot.context.properties.bind.BindContext) Bindable(org.springframework.boot.context.properties.bind.Bindable) Validated(org.springframework.validation.annotation.Validated) Origin(org.springframework.boot.origin.Origin) FieldError(org.springframework.validation.FieldError) Set(java.util.Set) AbstractBindHandler(org.springframework.boot.context.properties.bind.AbstractBindHandler) Test(org.junit.jupiter.api.Test) List(java.util.List) BindException(org.springframework.boot.context.properties.bind.BindException) MapPropertySource(org.springframework.core.env.MapPropertySource) Binder(org.springframework.boot.context.properties.bind.Binder) Min(jakarta.validation.constraints.Min) MockConfigurationPropertySource(org.springframework.boot.context.properties.source.MockConfigurationPropertySource) Test(org.junit.jupiter.api.Test)

Example 14 with ConfigurationProperty

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);
}
Also used : ConfigurationProperty(org.springframework.boot.context.properties.source.ConfigurationProperty) LinkedHashSet(java.util.LinkedHashSet) Origin(org.springframework.boot.origin.Origin) MockOrigin(org.springframework.boot.origin.MockOrigin) ConfigurationPropertyName(org.springframework.boot.context.properties.source.ConfigurationPropertyName) ObjectError(org.springframework.validation.ObjectError) ArrayList(java.util.ArrayList) FieldError(org.springframework.validation.FieldError) Test(org.junit.jupiter.api.Test)

Example 15 with ConfigurationProperty

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);
}
Also used : ConfigurationProperty(org.springframework.boot.context.properties.source.ConfigurationProperty) LinkedHashSet(java.util.LinkedHashSet) Test(org.junit.jupiter.api.Test)

Aggregations

ConfigurationProperty (org.springframework.boot.context.properties.source.ConfigurationProperty)18 Test (org.junit.jupiter.api.Test)9 ConfigurationPropertyName (org.springframework.boot.context.properties.source.ConfigurationPropertyName)5 ArrayList (java.util.ArrayList)4 MockConfigurationPropertySource (org.springframework.boot.context.properties.source.MockConfigurationPropertySource)4 ConfigurationPropertySource (org.springframework.boot.context.properties.source.ConfigurationPropertySource)3 Origin (org.springframework.boot.origin.Origin)3 LinkedHashSet (java.util.LinkedHashSet)2 List (java.util.List)2 Map (java.util.Map)2 SanitizableData (org.springframework.boot.actuate.endpoint.SanitizableData)2 BindContext (org.springframework.boot.context.properties.bind.BindContext)2 Bindable (org.springframework.boot.context.properties.bind.Bindable)2 Binder (org.springframework.boot.context.properties.bind.Binder)2 ConverterNotFoundException (org.springframework.core.convert.ConverterNotFoundException)2 FieldError (org.springframework.validation.FieldError)2 ObjectError (org.springframework.validation.ObjectError)2 Valid (jakarta.validation.Valid)1 Min (jakarta.validation.constraints.Min)1 NotNull (jakarta.validation.constraints.NotNull)1