spring-projects / spring-projects/spring-framework

YamlPropertiesFactoryBean incorrect flatten nested map to properties when map key contains escaped brackets

Open
#27,020 3 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

in: core type: bug
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

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. 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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.