How do I use @ConstructorBinding with child objects that only exist in lists?

Viewed 450

I am trying to set up my application's Spring Cloud config objects using a Kotlin data class. I've been successful with using @ConstructorBinding when defining classes that exist once at a specific node in the config but have run into a roadblock with objects that only appear in a child list.

Sample config:

toolbox:
  tools:
    - id: 1
      description: hammer
    - id: 2
      description: screwdriver

What I would like to be able to do is this:

@ConstructorBinding
@ConfigurationProperties("toolbox")
data class ToolBox(val tools: List<Tool>) {

    @ConstructorBinding
    data class Tool (val id: String, val description: String)
}

But when I do this I receive errors like this:

Property: toolbox.tools[0].description
Value: hammer
Origin: "toolbox.tools[0].description" from property source "bootstrapProperties-https:/some-config.yml"
Reason: The elements [toolbox.tools[0].description,toolbox.tools[0].id,toolbox.tools[1].description,toolbox.tools[1].id] were left unbound.

My application is configured with @ConfigurationPropertiesScan and other uses of @ConstructorBinding work for me. For example, the following alternative configuration works:

@ConstructorBinding
@ConfigurationProperties("toolbox")
data class ToolBox(val tools: List<Tool>) {

    class Tool {
        lateinit var id: String
        lateinit var description: String
    }
}

I've tried adding @ConfigurationProperties to the Tool class but that annotation requires a prefix and I don't know of a way to define a prefix that addresses into a list like I am trying to use here.

Is there a way to use @ConstructorBinding with a configuration class that only appears within a list?

0 Answers
Related