How to redefiine maxAliasesForCollections in LoaderOptions?

Viewed 305

i am using @ConfigurationProperties annotation to bind my yaml file, which is defined in spring.profile.active, to the class. The problem is that this file is using a lot of aliases in it. By default LoaderOptions() class has maxAliasesForCollections defined as 50.

Due to it next exception is throw ->

org.yaml.snakeyaml.error.YAMLException: Number of aliases for non-scalar nodes exceeds the specified max=50

Tried using

class UnlimitedYamlPropertiesFactoryBean : YamlPropertiesFactoryBean() {
    @Override
    override fun createYaml(): Yaml {
        val loaderOptions = LoaderOptions()
        loaderOptions.isAllowDuplicateKeys = false
        loaderOptions.maxAliasesForCollections = Int.MAX_VALUE
        loaderOptions.allowRecursiveKeys = true
        return Yaml(loaderOptions)
    }
}

class YamlPropertySourceFactory : PropertySourceFactory {
    override fun createPropertySource(name: String?, encodedResource: EncodedResource): PropertySource<*> {
        val factory: YamlPropertiesFactoryBean = UnlimitedYamlPropertiesFactoryBean()
        factory.setResources(encodedResource.resource)
        val properties: Properties? = factory.getObject()
        return PropertiesPropertySource(encodedResource.resource.filename!!, properties!!)
    }
}

@Configuration
@ConfigurationProperties
@PropertySource(value = ["classpath:foo.yml"], factory = YamlPropertySourceFactory::class)
class YamlFooProperties {
    val mtProxy: List<FooConfigs>? = null
}

If i don't have this YML file defined in spring.profile.active, it works perfectly ok

i tried debugging and it does not even call UnlimitedYamlPropertiesFactoryBean and throws exception before that

Is There any workaround for this ?

0 Answers
Related