gstackoverflow
gstackoverflow

Reputation: 37106

Is there way to read the property from application.yml in polymorphic way?

I want to have following configuration in my application.yaml file:

root:
  config:
    role1:
      filter:
        type:
          or:          
            - type1
            - type2
    role2:
      filter: any

Technically it means that role2 should be added to any user but role1 should be added only in case if user contains field type = type1 or type = type2

But at this case filter should be polymorphic. So I tried to do:

@ConfigurationProperties(prefix = "root")
data class RootProperties(
    var config: Map<String, UserProperty>?,
) {

    data class UserProperty(
        var filter: Filter,
    )

    interface Filter

    data class SimpleFilter(
        var value: String,
    ) : Filter

    data class ComplexFilter(
        var type: TypeFilter,
    ) : Filter

    data class TypeFilter(
        var or: List<String>,
    )
}

@Configuration
@EnableConfigurationProperties(RootProperties::class)
class RootConfigTest(
    private val rootProperties: RootProperties,
) {

    @Bean
    fun test2(): String {
        println(rootProperties)
        return "1"
    }
}

The error is:

Failed to bind properties under 'root.config.role2.filter' to ***.config.RootProperties$Filter:

    Property: root.config.role2.filter
    Value: "any"
    Origin: class path resource [application.yml] - 66:15
    Reason: org.springframework.core.convert.ConverterNotFoundException: No converter found capable of converting from type [java.lang.String] to type [***.config.RootProperties$Filter]

Upvotes: 1

Views: 57

Answers (0)

Related Questions