Hello,

after migrated from Spring Boot 1.5.3.RELEASE to 2.1.3.RELEASE we've encountered a bug(?) with property binding.

The example code can be found here. It's a simple application with one property class that receives values from the application.yml via the @ConfigurationProperties annotation.

Starting the DemoApplication works fine.

But starting the simple DemoApplicationTests does not work, as the demo uses "test" profile and therefore the property class receives it's values from "application-test.yml".

The excepton does NOT occur if "ignoreUnknownFields" is NOT present (i.e., set to true).

Caused by: org.springframework.boot.context.properties.bind.BindException: Failed to bind properties under 'demo-properties' to com.example.demo.DemoApplication$DemoProperties
    at org.springframework.boot.context.properties.bind.Binder.handleBindError(Binder.java:249) ~[spring-boot-2.1.3.RELEASE.jar:2.1.3.RELEASE]
    at org.springframework.boot.context.properties.bind.Binder.bind(Binder.java:225) ~[spring-boot-2.1.3.RELEASE.jar:2.1.3.RELEASE]
    at org.springframework.boot.context.properties.bind.Binder.bind(Binder.java:208) ~[spring-boot-2.1.3.RELEASE.jar:2.1.3.RELEASE]
    at org.springframework.boot.context.properties.bind.Binder.bind(Binder.java:190) ~[spring-boot-2.1.3.RELEASE.jar:2.1.3.RELEASE]
    at org.springframework.boot.context.properties.ConfigurationPropertiesBinder.bind(ConfigurationPropertiesBinder.java:83) ~[spring-boot-2.1.3.RELEASE.jar:2.1.3.RELEASE]
    at org.springframework.boot.context.properties.ConfigurationPropertiesBindingPostProcessor.bind(ConfigurationPropertiesBindingPostProcessor.java:107) ~[spring-boot-2.1.3.RELEASE.jar:2.1.3.RELEASE]
    ... 43 common frames omitted
Caused by: org.springframework.boot.context.properties.bind.UnboundConfigurationPropertiesException: The elements [demo-properties.data[2]] were left unbound.
    at org.springframework.boot.context.properties.bind.handler.NoUnboundElementsBindHandler.checkNoUnboundElements(NoUnboundElementsBindHandler.java:88) ~[spring-boot-2.1.3.RELEASE.jar:2.1.3.RELEASE]
    at org.springframework.boot.context.properties.bind.handler.NoUnboundElementsBindHandler.onFinish(NoUnboundElementsBindHandler.java:73) ~[spring-boot-2.1.3.RELEASE.jar:2.1.3.RELEASE]
    at org.springframework.boot.context.properties.bind.Binder.handleBindResult(Binder.java:235) ~[spring-boot-2.1.3.RELEASE.jar:2.1.3.RELEASE]
    at org.springframework.boot.context.properties.bind.Binder.bind(Binder.java:222) ~[spring-boot-2.1.3.RELEASE.jar:2.1.3.RELEASE]
    ... 47 common frames omitted

The bug does only appear when the list in application-test.yml is SMALLER than the list in application.yml.

Regards, Manuel

Comment From: snicoll

Thanks for the sample, that is very useful.

We've tighten the mechanism to merge complex types, see the documentation.

I am not sure if we intended to throw an exception if you combine that with ignoreUnknownFields. If I add a string6 in the test profile, the exception goes away as the full list has been replaced. This shouldn't be necessary IMO.

Comment From: mbhave

I think it's a bug and we shouldn't throw an exception since the list is completely overridden.

Comment From: rhamedy

I would be happy to work on this issue should the team decide that it should be fixed 🙂

Comment From: mbhave

Thanks for the offer @rhamedy but I'm already looking into this.

Comment From: anatoliy-balakirev

We're getting exactly the same issue with spring boot 2.2.4. Based on tags here, it looks like it was fixed in 2.1.4 and 2.3.0. Is that correct? Is it planned to be fixed in some 2.2.x as well?

Comment From: snicoll

@anatoliy-balakirev all fixes are merged forward. This is fixed in 2.1, 2.2 and master. If you believe you've found an issue with Spring Boot, please build a small sample (a zip or a link to a github repo) with a test that shows the problem and create a new issue.

Comment From: anatoliy-balakirev

@snicoll thanks for info! IMO, an issue I mentioned is not some new one, but rather just a bit different flavor of the current. The only difference is that in my case properties are not plain strings, but rather some object with a field. I've forked an original sample repo and adjusted it to use the latest spring boot / config with a nested property: https://github.com/anatoliy-balakirev/spring-boot-configurationproperties-bug just run mvn clean test and you'll get:

[ERROR] contextLoads  Time elapsed: 0.001 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[0].data,demo-properties.nested-data[1].data,demo-properties.nested-data[2].data] were left unbound.

Comment From: snicoll

@anatoliy-balakirev this issue is closed in an already released version so we're not going to reopen it. Can you please move that comment to a new issue?

Comment From: snicoll

@anatoliy-balakirev before you do, please review the comments I've added to your sample. I believe it is wrong and the exception is legit and doesn't reproduce this issue.

Comment From: anatoliy-balakirev

@snicoll sorry, I might be missing something, but which comments you mean?

Comment From: mbhave

@anatoliy-balakirev I think Stephane is talking about these comments he added to your sample.

Comment From: anatoliy-balakirev

@mbhave thanks! not using github that often these days.

@snicoll you are right, I was trying to create something really quick yesterday after work and generated some rubbish, sorry (we have this same error with our real project, so I didn't test that DemoApplication properly). Adjusted it now. Still, problem is there: DemoApplication can be started, while DemoApplicationTests fails with:

Caused by: org.springframework.boot.context.properties.bind.UnboundConfigurationPropertiesException: The elements [demo-properties.nested-data[2].data] were left unbound.
    at org.springframework.boot.context.properties.bind.handler.NoUnboundElementsBindHandler.checkNoUnboundElements(NoUnboundElementsBindHandler.java:83)
    at org.springframework.boot.context.properties.bind.handler.NoUnboundElementsBindHandler.onFinish(NoUnboundElementsBindHandler.java:71)
    at org.springframework.boot.context.properties.bind.Binder.handleBindResult(Binder.java:340)
    at org.springframework.boot.context.properties.bind.Binder.bind(Binder.java:321)
    ... 47 more

Comment From: anatoliy-balakirev

As discussed, created a dedicated issue for that: https://github.com/spring-projects/spring-boot/issues/20134