-
Notifications
You must be signed in to change notification settings - Fork 41.7k
Closed
Description
Given the following:
package com.example.demo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import java.util.EnumMap;
import java.util.List;
@EnableConfigurationProperties(DemoApplication.ExampleProperties.class)
@SpringBootApplication
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
public enum Example {
ONE, TWO, THREE;
}
@ConfigurationProperties(prefix = "example")
record ExampleProperties(List<ExampleEntry> entries) {}
record ExampleEntry(String name, EnumMap<Example, String> map) {}
}and configuration:
example:
entries:
- name: one
map:
ONE: one
TWO: two
THREE: threeWill result in the following exception:
***************************
APPLICATION FAILED TO START
***************************
Description:
Failed to bind properties under 'example.entries[0].map' to java.util.EnumMap<com.example.demo.DemoApplication$Example, java.lang.String>:
Property: example.entries[0].name
Value: "one"
Origin: class path resource [application.yml] - 3:13
Reason: java.lang.IllegalArgumentException: Cannot create EnumMap for unknown key type
Action:
Update your application's configuration
It works if I switch to Map instead of EnumMap:
- public record ExampleEntry(String name, EnumMap<Example, String> map) {}
+ public record ExampleEntry(String name, Map<Example, String> map) {}But the keys (for my use case) are enums and it makes sense to use the specialized map implementation.
After some debugging, it looks to be an issue on Boot's side in ValueObjectBinder:
Line 160 in 3648f5f
| return (T) CollectionFactory.createMap(resolved, 0); |
The key portion is lost which then fails Framework's check:
Additionally, maybe related: #19156
Metadata
Metadata
Assignees
Labels
type: bugA general bugA general bug