I have list of objects in my properties and I want populate them by env variables. Spring boot version = 3.5.3
.
Example of my properties structure:
my:
tests:
- a: foo
b: bar
- a: hello
b: world
My kotlin code:
data class Test(val a: String, val b: String)
@ConfigurationProperties(prefix = "my")
data class MyProperties(val tests: List<Test>)
and I want fill them with environment variables:
MY_TESTS_0_A=foo
MY_TESTS_0_B=bar
MY_TESTS_1_A=hello
MY_TESTS_1_B=world
But the tests
is always empty.
I tried multiple combination with list of Strings or object with single field. Both works.
@ConfigurationProperties(prefix = "my")
data class MyProperties(val tests: List<String>)
MY_TESTS_0=foo
MY_TESTS_1=bar
data class Test(val a: String, val b: String)
@ConfigurationProperties(prefix = "my")
data class MyProperties(val tests: List<String>)
MY_TESTS_0=foo
MY_TESTS_1=bar
The mapping somehow works, but not for objects with multiple fields.
Workaround is to use env SPRING_APPLICATION_JSON
and pass values as JSON.
Comment From: philwebb
I'm not sure exactly what's going on, but I can't seem to replicate this with a pure Java application.
If you build this project then run the following:
MY_TESTS_0_A=foo MY_TESTS_0_B=bar MY_TESTS_1_A=hello MY_TESTS_1_B=world java -jar target/demobind-0.0.1-SNAPSHOT.jar
You get:
2025-07-31T22:22:02.069+01:00 INFO 82614 --- [demobind] [ main] c.example.demobind.DemobindApplication : Started DemobindApplication in 0.445 seconds (process running for 0.669)
MyProperties[tests=[Test[a=foo, b=bar], Test[a=hello, b=world]]]
@radeklos Could you please share a project that replicates the problem?