pixel
pixel

Reputation: 26473

How to convert map entry from yaml into property in Spring?

I have the following property list in my application.yml:

foo: 
  bar: 
    - 
      id: baz
      item: value
    // ...

Then I want to overwrite item value in tests using @DynamicPropertySource:

    @DynamicPropertySource
    @JvmStatic
    @Suppress("unused")
    fun setupProperties(registry: DynamicPropertyRegistry) {
        registry.add("foo.bar[0].item") { "new value" }
    }

But during the tests, I got all other properties set to nulls, with one element in bar array.

I guess that I'm not referring correctly to map entry in yaml file. I wonder how I can do that?

Upvotes: 1

Views: 699

Answers (1)

pixel
pixel

Reputation: 26473

It turns out that Spring Boot documentation states clearly:

When lists are configured in more than one place, overriding works by replacing the entire list.

https://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#boot-features-external-config-complex-type-merge

This effectively means that I need to provide whole list item:

@DynamicPropertySource
@JvmStatic
@Suppress("unused")
fun setupProperties(registry: DynamicPropertyRegistry) {
    registry.add("foo.bar[0].id") { "new baz" }
    registry.add("foo.bar[0].item") { "new value" }
    // ...
}

Upvotes: 1

Related Questions