Skip to content

Binding to collection fails with unbound elements error if collection overridden in another property source with smaller number of elements #20134

@anatoliy-balakirev

Description

@anatoliy-balakirev

As was suggested in #16290, creating this follow up issue for a bit different flavor of the same problem. In the original issue, a list of properties contained strings and that case seems to be fixed now, but when we use some custom object instead of string - the problem is still reproducible.

Here is a sample application to reproduce an issue.

Running mvn clean test fails with:

[ERROR] Tests run: 1, Failures: 0, Errors: 1, Skipped: 0, Time elapsed: 2.156 s <<< FAILURE! - in com.example.demo.DemoApplicationTests
[ERROR] contextLoads  Time elapsed: 0.016 s  <<< ERROR!
java.lang.IllegalStateException: Failed to load ApplicationContext
Caused by: org.springframework.boot.context.properties.ConfigurationPropertiesBindException: Error creating bean with name 'demoApplication.DemoProperties': Could not bind properties to 'DemoApplication.DemoProperties' : prefix=demo-properties, ignoreInvalidFields=false, ignoreUnknownFields=false; nested exception is org.springframework.boot.context.properties.bind.BindException: Failed to bind properties under 'demo-properties' to com.example.demo.DemoApplication$DemoProperties
Caused by: org.springframework.boot.context.properties.bind.BindException: Failed to bind properties under 'demo-properties' to com.example.demo.DemoApplication$DemoProperties
Caused by: org.springframework.boot.context.properties.bind.UnboundConfigurationPropertiesException: The elements [demo-properties.nested-data[2].data] were left unbound.

While starting the application itself works fine.

This is because in the application-test.yml I have a list of properties (nested-data) overridden with a list, smaller than the original one in the application.yml

According to the documentation I would expect the list from the application-test.yml to just override one from the application.yml. Now it looks like it defines a number of elements based on the application.yml then tries to fill those three elements and fails because there is no data for the third one (because in the application-test.yml we have only two). Adding the third element to the application-test.yml fixes the problem.

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions