spring-projects / spring-projects/spring-framework
YamlPropertiesFactoryBean incorrect flatten nested map to properties when map key contains escaped brackets
Nobody has claimed this yet.
- Dominant language
- Java
- Stars
- 60.2k
- Forks
- 38.8k
- Avg merge
- 5d 2h
- Merged PRs (30d)
- 27
Description
Affects: since 4.1
Yaml file like bellow:
root:
webservices:
"[domain.test:8080]":
- username: me
password: mypassword
Yaml's map to json like
{
"webservices": {
"[domain.test:8080]": [
{
"username": "me",
"password": "mypassword"
}
]
}
}
should equals properties file like:
root.webservices.[[domain.test\:8080]][0].username=me
root.webservices.[[domain.test\:8080]][0].password=mypassword
or:
root.webservices[[domain.test\:8080]][0].username=me
root.webservices[[domain.test\:8080]][0].password=mypassword
Both properties file is acceptable for Spring Boot Binding Maps
Reproduce Test Case is
@Test
void loadNestedEscapedProperties() throws Exception {
String yaml = "root:\n" +
" webservices:\n" +
" \"[domain.test:8080]\":\n" +
" - username: me\n" +
" password: mypassword\n";
String propsStr = "root.webservices[[domain.test\\:8080]][0].username=me\n" +
"root.webservices[[domain.test\\:8080]][0].password=mypassword";
final PropertiesFactoryBean factory = new PropertiesFactoryBean();
factory.setLocations(new ByteArrayResource(propsStr.getBytes(StandardCharsets.UTF_8)));
factory.setSingleton(false);
final Properties properties = factory.getObject();
final YamlPropertiesFactoryBean yamlFactory = new YamlPropertiesFactoryBean();
yamlFactory.setResources(new ByteArrayResource(yaml.getBytes(StandardCharsets.UTF_8)));
assertThat(yamlFactory.getObject()).isEqualTo(properties);
}
Current it is failed like bellow
expected: {"root.webservices[[domain.test:8080]][0].password"="mypassword", "root.webservices[[domain.test:8080]][0].username"="me"}
but was : {"root.webservices[domain.test:8080][0].password"="mypassword", "root.webservices[domain.test:8080][0].username"="me"}
org.opentest4j.AssertionFailedError:
expected: {"root.webservices[[domain.test:8080]][0].password"="mypassword", "root.webservices[[domain.test:8080]][0].username"="me"}
but was : {"root.webservices[domain.test:8080][0].password"="mypassword", "root.webservices[domain.test:8080][0].username"="me"}
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at org.springframework.beans.factory.config.YamlPropertiesFactoryBeanTests.loadNestedEscapedProperties(YamlPropertiesFactoryBeanTests.java:270)
It seems there is a bug cause yaml's nested map is flatten incorrectly like
root.webservices[domain.test:8080][0].username=me
root.webservices[domain.test:8080][0].password=mypassword
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start with YamlPropertiesFactoryBean and the loadNestedEscapedProperties reproduction in YamlPropertiesFactoryBeanTests.java. Run that test, then trace how the nested YAML map key is flattened; done means the resulting properties match the expected escaped-bracket and colon form shown in the issue.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- java, spring
- Domain
- backend
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Clearly specified
- Newbie friendliness
- 52/100