Search in sources :

Example 6 with ConfigurationProperty

use of org.springframework.boot.context.properties.source.ConfigurationProperty in project spring-boot by spring-projects.

the class InactiveConfigDataAccessException method throwIfPropertyFound.

/**
 * Throw an {@link InactiveConfigDataAccessException} if the given
 * {@link ConfigDataEnvironmentContributor} contains the property.
 * @param contributor the contributor to check
 * @param name the name to check
 */
static void throwIfPropertyFound(ConfigDataEnvironmentContributor contributor, ConfigurationPropertyName name) {
    ConfigurationPropertySource source = contributor.getConfigurationPropertySource();
    ConfigurationProperty property = (source != null) ? source.getConfigurationProperty(name) : null;
    if (property != null) {
        PropertySource<?> propertySource = contributor.getPropertySource();
        ConfigDataResource location = contributor.getResource();
        throw new InactiveConfigDataAccessException(propertySource, location, name.toString(), property.getOrigin());
    }
}
Also used : ConfigurationProperty(org.springframework.boot.context.properties.source.ConfigurationProperty) ConfigurationPropertySource(org.springframework.boot.context.properties.source.ConfigurationPropertySource)

Example 7 with ConfigurationProperty

use of org.springframework.boot.context.properties.source.ConfigurationProperty in project spring-boot by spring-projects.

the class UnboundConfigurationPropertyFailureAnalyzer method analyzeUnboundConfigurationPropertiesException.

private FailureAnalysis analyzeUnboundConfigurationPropertiesException(BindException cause, UnboundConfigurationPropertiesException exception) {
    StringBuilder description = new StringBuilder(String.format("Binding to target %s failed:%n", cause.getTarget()));
    for (ConfigurationProperty property : exception.getUnboundProperties()) {
        buildDescription(description, property);
        description.append(String.format("%n    Reason: %s", exception.getMessage()));
    }
    return getFailureAnalysis(description, cause);
}
Also used : ConfigurationProperty(org.springframework.boot.context.properties.source.ConfigurationProperty)

Example 8 with ConfigurationProperty

use of org.springframework.boot.context.properties.source.ConfigurationProperty in project spring-boot by spring-projects.

the class ArrayBinderTests method bindToArrayWhenNonSequentialShouldThrowException.

@Test
void bindToArrayWhenNonSequentialShouldThrowException() {
    MockConfigurationPropertySource source = new MockConfigurationPropertySource();
    source.put("foo[0]", "2");
    source.put("foo[1]", "1");
    source.put("foo[3]", "3");
    this.sources.add(source);
    assertThatExceptionOfType(BindException.class).isThrownBy(() -> this.binder.bind("foo", INTEGER_ARRAY)).satisfies((ex) -> {
        Set<ConfigurationProperty> unbound = ((UnboundConfigurationPropertiesException) ex.getCause()).getUnboundProperties();
        assertThat(unbound.size()).isEqualTo(1);
        ConfigurationProperty property = unbound.iterator().next();
        assertThat(property.getName().toString()).isEqualTo("foo[3]");
        assertThat(property.getValue()).isEqualTo("3");
    });
}
Also used : ConfigurationProperty(org.springframework.boot.context.properties.source.ConfigurationProperty) MockConfigurationPropertySource(org.springframework.boot.context.properties.source.MockConfigurationPropertySource) Test(org.junit.jupiter.api.Test)

Example 9 with ConfigurationProperty

use of org.springframework.boot.context.properties.source.ConfigurationProperty in project spring-boot by spring-projects.

the class CollectionBinderTests method bindToNonScalarCollectionWhenNonSequentialShouldThrowException.

@Test
void bindToNonScalarCollectionWhenNonSequentialShouldThrowException() {
    MockConfigurationPropertySource source = new MockConfigurationPropertySource();
    source.put("foo[0].value", "1");
    source.put("foo[1].value", "2");
    source.put("foo[4].value", "4");
    this.sources.add(source);
    Bindable<List<JavaBean>> target = Bindable.listOf(JavaBean.class);
    assertThatExceptionOfType(BindException.class).isThrownBy(() -> this.binder.bind("foo", target)).satisfies((ex) -> {
        Set<ConfigurationProperty> unbound = ((UnboundConfigurationPropertiesException) ex.getCause()).getUnboundProperties();
        assertThat(unbound).hasSize(1);
        ConfigurationProperty property = unbound.iterator().next();
        assertThat(property.getName().toString()).isEqualTo("foo[4].value");
        assertThat(property.getValue()).isEqualTo("4");
    });
}
Also used : ConfigurationProperty(org.springframework.boot.context.properties.source.ConfigurationProperty) MockConfigurationPropertySource(org.springframework.boot.context.properties.source.MockConfigurationPropertySource) ArrayList(java.util.ArrayList) List(java.util.List) LinkedList(java.util.LinkedList) Test(org.junit.jupiter.api.Test)

Example 10 with ConfigurationProperty

use of org.springframework.boot.context.properties.source.ConfigurationProperty in project spring-boot by spring-projects.

the class IndexedElementsBinder method bindIndexed.

private void bindIndexed(ConfigurationPropertySource source, ConfigurationPropertyName root, Bindable<?> target, AggregateElementBinder elementBinder, IndexedCollectionSupplier collection, ResolvableType aggregateType, ResolvableType elementType) {
    ConfigurationProperty property = source.getConfigurationProperty(root);
    if (property != null) {
        getContext().setConfigurationProperty(property);
        bindValue(target, collection.get(), aggregateType, elementType, property.getValue());
    } else {
        bindIndexed(source, root, elementBinder, collection, elementType);
    }
}
Also used : ConfigurationProperty(org.springframework.boot.context.properties.source.ConfigurationProperty)

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