Skip to content

Binding a value from a config tree fails after a collection of the same element type has been bound from the same config tree #52553

Closed

Description

@artem1458

When properties are imported with spring.config.import=configtree:..., binding a value fails with ConverterNotFoundException if a collection with the same element type was bound from the same config tree before it. If the single value is bound first, both bind fine. The example below uses Set<Integer> and Integer; the other types I tried are listed further down.

Versions: reproduced on Spring Boot 4.2.0-RC1, 4.1.2, 4.0.9, 3.5.16, 3.4.13, 3.3.13, 3.2.12, 3.1.12, 3.0.13, 2.7.18 and 2.5.0, with JDK 21 and 25. Spring Boot 2.4.13 starts fine with the same sample.

Reproduction

configtree/collection.ids, no trailing newline:

254,169,1413

configtree/scalar.timeout:

300000

application.properties:

spring.config.import=configtree:configtree/
@ConfigurationProperties("collection")
public record CollectionProperties(Set<Integer> ids) {}

@ConfigurationProperties("scalar")
public record ScalarProperties(Integer timeout) {}

@SpringBootApplication
@EnableConfigurationProperties({ CollectionProperties.class, ScalarProperties.class })
public class ReproApplication {

	public static void main(String[] args) {
		SpringApplication.run(ReproApplication.class, args);
	}

	// CollectionProperties is bound before ScalarProperties (parameter order)
	@Bean
	ApplicationRunner printer(CollectionProperties collection, ScalarProperties scalar) {
		return (args) -> System.out.println("Bound " + collection + " and " + scalar);
	}

}

Run it from the project root with ./mvnw spring-boot:run.

Expected: the application starts and prints Bound CollectionProperties[ids=[254, 169, 1413]] and ScalarProperties[timeout=300000].

Actual:

***************************
APPLICATION FAILED TO START
***************************

Description:

Failed to bind properties under 'scalar.timeout' to java.lang.Integer:

    Property: scalar.timeout
    Value: "300000"
    Origin: file [.../configtree/scalar.timeout] - 1:1
    Reason: org.springframework.core.convert.ConverterNotFoundException: No converter found capable of converting from type [org.springframework.boot.env.ConfigTreePropertySource$PropertyFileContent] to type [java.lang.Integer]

Controls

  • Reverse order. Same config tree, but ScalarProperties is bound before CollectionProperties: the application starts and both properties bind.
  • String values. Same values as plain properties instead of a config tree (collection.ids=254,169,1413, scalar.timeout=300000), with CollectionProperties bound first: the application starts and both properties bind.

Without an application context

The same happens with a Binder alone:

Binder binder = new Binder(
		List.of(ConfigurationPropertySource.from(new ConfigTreePropertySource("tree", Path.of("configtree")))), null,
		new ApplicationConversionService());

binder.bind("collection.ids", Bindable.setOf(Integer.class)).get();   // [254, 169, 1413]
binder.bind("scalar.timeout", Integer.class).get();                   // BindException, cause: ConverterNotFoundException

It can also be observed on an ApplicationConversionService directly:

Object value = new ConfigTreePropertySource("tree", Path.of("configtree")).getProperty("scalar.timeout");
TypeDescriptor source = TypeDescriptor.forObject(value);
TypeDescriptor setOfInteger = new TypeDescriptor(ResolvableType.forClassWithGenerics(Set.class, Integer.class), null, null);
ApplicationConversionService conversionService = new ApplicationConversionService();

conversionService.canConvert(source, setOfInteger);                       // true
conversionService.canConvert(source, TypeDescriptor.valueOf(Integer.class)); // false

Called in the reverse order on a new ApplicationConversionService, both calls return true.

The same happens with List<T> and T[] instead of Set<T>, and for every other element type I tried that needs a conversion: Long, Short, Byte, Double, Float, Boolean, Character, BigDecimal, BigInteger, UUID, LocalDate, Duration, Period, DataSize, an enum, Locale, Charset and URI. String elements, primitive targets (for example int after Set<Integer>) and the reverse order are not affected.

Sample project with all of the above as tests, including every element type and collection kind listed (./mvnw test: 106 tests, 60 fail, 46 pass; -Dspring-boot.version=… to switch versions): https://github.com/artem1458/spring-boot-configtree-binding-repro


AI disclosure: the investigation, the sample project and this text were prepared with an AI agent (Claude Code) and reviewed by me before posting.

Activity

  1. added
    status: waiting-for-triage
    on Nov 2, 2026
  2. changed the title [-]Binding an Integer from a config tree fails after a Set<Integer> has been bound from the same config tree[/-] [+]Binding a value from a config tree fails after a collection of the same element type has been bound from the same config tree[/+] on Nov 2, 2026
  3. wilkinsona commented on Nov 2, 2026

    @wilkinsona
    Member

    This is caused by CharSequenceToObjectConverter polluting the converterCache with a NO_MATCH entry for PropertyFileContent -> Integer. The entry is added when ObjectToCollectionConverter is checking if it can convert the elements. It's a NO_MATCH because of the disable thread local that CharSequenceToObjectConverter sets when it checks the PropertyFileContent -> Set<Integer> conversion that then spills over into the elements conversion check.

    If PropertyFileContent -> Integer is checked first, the pollution of the cache doesn't happen and things work as expected.

  4. added
    type: bug
    and removed
    status: waiting-for-triage
    on Nov 2, 2026
  5. added this to the 4.0.x milestone on Nov 2, 2026
  6. wilkinsona commented on Nov 2, 2026

    @wilkinsona
    Member

    This test passes:

    @Test
    void convertScalarThenSet() throws IOException {
    	addProperty("scalar.timeout", "10000");
    	Object value = new ConfigTreePropertySource("test", this.directory).getProperty("scalar.timeout");
    	TypeDescriptor source = TypeDescriptor.forObject(value);
    	TypeDescriptor setOfInteger = new TypeDescriptor(ResolvableType.forClassWithGenerics(Set.class, Integer.class),
    			null, null);
    	ApplicationConversionService conversionService = new ApplicationConversionService();
    	Map<?, ?> converterCache = (Map<?, ?>) ReflectionTestUtils.getField(conversionService, "converterCache");
    	assertThat(conversionService.canConvert(source, TypeDescriptor.valueOf(Integer.class))).isTrue();
    	System.out.println("Converter cache:");
    	converterCache.forEach((k, v) -> System.out.println("  " + k + " " + v));
    	assertThat(conversionService.canConvert(source, setOfInteger)).isTrue();
    }

    After the first conversion, the cache looks like this:

    Converter cache:
      ConverterCacheKey [sourceType = org.springframework.boot.env.ConfigTreePropertySource$PropertyFileContent, targetType = java.lang.Integer] org.springframework.boot.convert.CharSequenceToObjectConverter@3fcdcf
      ConverterCacheKey [sourceType = java.lang.String, targetType = java.lang.Integer] java.lang.String -> java.lang.Number : org.springframework.core.convert.support.StringToNumberConverterFactory@7668d560
    

    This test fails:

    @Test
    void convertSetThenScalar() throws IOException {
    	addProperty("scalar.timeout", "10000");
    	Object value = new ConfigTreePropertySource("test", this.directory).getProperty("scalar.timeout");
    	TypeDescriptor source = TypeDescriptor.forObject(value);
    	TypeDescriptor setOfInteger = new TypeDescriptor(ResolvableType.forClassWithGenerics(Set.class, Integer.class),
    			null, null);
    	ApplicationConversionService conversionService = new ApplicationConversionService();
    	Map<?, ?> converterCache = (Map<?, ?>) ReflectionTestUtils.getField(conversionService, "converterCache");
    	assertThat(conversionService.canConvert(source, setOfInteger)).isTrue();
    	System.out.println("Converter cache:");
    	converterCache.forEach((k, v) -> System.out.println("  " + k + " " + v));
    	assertThat(conversionService.canConvert(source, TypeDescriptor.valueOf(Integer.class))).isTrue();
    }

    After the first conversion, the cache looks like this:

    Converter cache:
      ConverterCacheKey [sourceType = org.springframework.boot.env.ConfigTreePropertySource$PropertyFileContent, targetType = java.util.Set<java.lang.Integer>] org.springframework.boot.convert.CharSequenceToObjectConverter@126be319
      ConverterCacheKey [sourceType = org.springframework.boot.env.ConfigTreePropertySource$PropertyFileContent, targetType = java.lang.Integer] NO_MATCH
      ConverterCacheKey [sourceType = java.lang.String, targetType = java.lang.Integer] java.lang.String -> java.lang.Number : org.springframework.core.convert.support.StringToNumberConverterFactory@6c44052e
      ConverterCacheKey [sourceType = java.lang.String, targetType = java.util.Set<java.lang.Integer>] org.springframework.boot.convert.DelimitedStringToCollectionConverter@5c371e13
    

    It's the second entry in the cache that's the pollution and that causes the failure.

  7. modified the milestones: 4.0.x, 4.0.10 on Nov 2, 2026
  8. added a commit that references this issue on Nov 3, 2026
    7c1bef5
  9. challa commented on Nov 4, 2026

    @challa
    Contributor

    ConfigurationPropertiesCharSequenceToObjectConverter, the copy of CharSequenceToObjectConverter that ConversionServiceDeducer uses when there are @ConfigurationPropertiesBinding converters, still has the disable thread local that 7c1bef5 replaced. The same cache pollution still happens for types that require one of these custom converters.

    With a config tree containing custom.endpoints (a:1,b:2) and custom.primary (c:3), this fails to start on 4.0.10-SNAPSHOT, 4.1.3-SNAPSHOT and 4.2.0-SNAPSHOT, tested on November 4.

    Import the directory using spring.config.import=configtree:configtree/, enable CustomProperties with @EnableConfigurationProperties(CustomProperties.class), and declare the converter bean in the application configuration:

    public record Endpoint(String host, int port) {
    
    	static Endpoint parse(String value) {
    		String[] parts = value.split(":");
    		return new Endpoint(parts[0], Integer.parseInt(parts[1]));
    	}
    
    }
    
    @ConfigurationProperties("custom")
    public record CustomProperties(List<Endpoint> endpoints, Endpoint primary) {
    }
    
    @Bean
    @ConfigurationPropertiesBinding
    static Converter<String, Endpoint> endpointConverter() {
    	return Endpoint::parse;
    }
    Failed to bind properties under 'custom.primary' to com.example.Endpoint:
    
        Reason: org.springframework.core.convert.ConverterNotFoundException: No converter found capable of converting from type [org.springframework.boot.env.ConfigTreePropertySource$PropertyFileContent] to type [com.example.Endpoint]
    

    Declaring primary before endpoints works. Applying the same change to the copy fixes it; I have that change with a test on a branch based on 4.0.x

    I used an AI coding assistant for parts of the analysis; I reviewed and verified it myself.

  10. wilkinsona commented on Nov 4, 2026

    @wilkinsona
    Member

    Well spotted, @challa, thank you. I have made the necessary changes in 00cfa0d.

  11. added a commit that references this issue on Nov 4, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

type: bug

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions