Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
ian4hu opened this issue Jun 4, 2021 · 3 comments
Labels
in: core Issues in core modules (aop, beans, core, context, expression) type: bug A general bug

Comments

@ian4hu
Copy link

ian4hu commented Jun 4, 2021

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
@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged or decided on label Jun 4, 2021
@snicoll snicoll changed the title [Bug] YamlPropertiesFactoryBean incorrect flatten nested map to properties when map key contains escaped brackets YamlPropertiesFactoryBean incorrect flatten nested map to properties when map key contains escaped brackets Jun 4, 2021
@ian4hu
Copy link
Author

ian4hu commented Jun 4, 2021

@ian4hu
Copy link
Author

ian4hu commented Jun 4, 2021

related #27022

@rstoyanchev rstoyanchev added the in: core Issues in core modules (aop, beans, core, context, expression) label Nov 10, 2021
simonbasle added a commit to simonbasle/spring-framework that referenced this issue Mar 2, 2023
@snicoll snicoll added type: bug A general bug and removed status: waiting-for-triage An issue we've not yet triaged or decided on labels Sep 27, 2023
@snicoll snicoll added this to the 6.x Backlog milestone Sep 27, 2023
@snicoll
Copy link
Member

snicoll commented Sep 27, 2023

@ian4hu you said you had some code to attempt to fix this issue. Would you mind sharing that?

@jhoeller jhoeller modified the milestones: 6.x Backlog, General Backlog Oct 1, 2024
remeio added a commit to remeio/spring-framework that referenced this issue Jan 19, 2025
…when map key contains escaped brackets.

Close spring-projectsgh-27020.

Signed-off-by: Mengqi Xu <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
in: core Issues in core modules (aop, beans, core, context, expression) type: bug A general bug
Projects
None yet
Development

No branches or pull requests

5 participants