Skip to content

Configuration properties binding bug with * in property name #24548

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

Closed
ztomic opened this issue Dec 17, 2020 · 3 comments
Closed

Configuration properties binding bug with * in property name #24548

ztomic opened this issue Dec 17, 2020 · 3 comments
Labels
status: invalid An issue that we don't feel is valid

Comments

@ztomic
Copy link
Contributor

ztomic commented Dec 17, 2020

Hi,

I have some configuration properties class which has Map<String, Integer> field. Key in map can contain * for some ant path matching. If property source contains that keys, binder will bind them without * characters and with unexpected values also.

Here is minimal sample to reproduce this:

static class DemoConfigurationProperties {

	private Map<String, Integer> properties = Map.of("foo-*", 1, "*-foo", 2, "*-foo-*", 3);

	public Map<String, Integer> getProperties() {
		return properties;
	}

	public void setProperties(Map<String, Integer> properties) {
		this.properties = properties;
	}
}

/**
 * Tis test fails with:
 * AssertionFailedError:
 * Expected :{foo-*=4, *-foo-*=6, *-foo=5}
 * Actual   :{foo-*=1, *-foo-*=3, *-foo=2, -foo-=6, foo-=6, -foo=6}
 */
@Test
public void testWithSameProperties() {
	DemoConfigurationProperties demoConfigurationProperties = new DemoConfigurationProperties();
	Assertions.assertEquals(Map.of("foo-*", 1, "*-foo", 2, "*-foo-*", 3), demoConfigurationProperties.getProperties());

	// now bind same properties
	StandardEnvironment environment = new StandardEnvironment();
	Map<String, Object> properties = Map.of("demo.properties.foo-*", 4, "demo.properties.*-foo", 5, "demo.properties.*-foo-*", 6);
	environment.getPropertySources().addFirst(new MapPropertySource("demo-map-source", properties));
	Binder.get(environment).bind("demo", Bindable.ofInstance(demoConfigurationProperties));

	Assertions.assertEquals(Map.of("foo-*", 4, "*-foo", 5, "*-foo-*", 6), demoConfigurationProperties.getProperties());
}

Note above that all properties which are bound from environment have same value 6 but in environment only one property has value 6.

NOTE: I'm using Spring Boot 2.4.1

Regards,

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged label Dec 17, 2020
@ztomic
Copy link
Contributor Author

ztomic commented Dec 17, 2020

On Spring Boot 2.2.10.RELEASE output is not same on each run. (I know it is an unsupported version, just wanted to make sure that my problem is not tied exclusively to the new version)

E.g.

org.opentest4j.AssertionFailedError: 
Expected :{*-foo-*=6, *-foo=5, foo-*=4}
Actual   :{*-foo-*=3, *-foo=2, foo-*=1, -foo-=6, foo-=6, -foo=5}
org.opentest4j.AssertionFailedError: 
Expected :{*-foo-*=6, foo-*=4, *-foo=5}
Actual   :{*-foo-*=3, foo-*=1, *-foo=2, -foo=5, foo-=5, -foo-=5}
org.opentest4j.AssertionFailedError: 
Expected :{foo-*=4, *-foo-*=6, *-foo=5}
Actual   :{foo-*=1, *-foo-*=3, *-foo=2, -foo-=6, foo-=6, -foo=5}

@wilkinsona
Copy link
Member

This is working as documented. Properties with special characters in their name must by wrapped in [].

@wilkinsona wilkinsona added status: invalid An issue that we don't feel is valid and removed status: waiting-for-triage An issue we've not yet triaged labels Dec 17, 2020
@ztomic
Copy link
Contributor Author

ztomic commented Dec 17, 2020

Thanks, sorry for opened issue, missed that part in documentation.

quaff added a commit to quaff/spring-boot that referenced this issue Oct 28, 2024
Given:
```yaml
my:
  map:
    "/key": "value"
```
---
Before this commit:
It's equivalent to
```yaml
my:
  map:
    "[key]": "value" # "[/key]" is expected
```
Such counter-intuitive behavior will confuse developers, there are several reported issues, incomplete list: spring-projectsGH-41099 spring-projectsGH-29582 spring-projectsGH-24548

---
After this commit:
```
***************************
APPLICATION FAILED TO START
***************************

Description:

Failed to bind properties under 'my.map' to java.util.Map<java.lang.String, java.lang.String>:

    Reason: java.lang.IllegalArgumentException: Please rewrite key '/key' to '[/key]' and surround it with quotes if YAML is using

Action:

Update your application's configuration

```
---

See spring-projectsGH-42802
quaff added a commit to quaff/spring-boot that referenced this issue Oct 28, 2024
Given:
```yaml
my:
  map:
    "/key": "value"
```
---
Before this commit:
It's equivalent to
```yaml
my:
  map:
    "[key]": "value" # "[/key]" is expected
```
Such counter-intuitive behavior will confuse developers, there are several reported issues, incomplete list: spring-projectsGH-41099 spring-projectsGH-29582 spring-projectsGH-24548

---
After this commit:
```
***************************
APPLICATION FAILED TO START
***************************

Description:

Failed to bind properties under 'my.map' to java.util.Map<java.lang.String, java.lang.String>:

    Reason: java.lang.IllegalArgumentException: Please rewrite key '/key' to '[/key]' and surround it with quotes if YAML is using

Action:

Update your application's configuration

```
---

See spring-projectsGH-42802
quaff added a commit to quaff/spring-boot that referenced this issue Oct 28, 2024
Given:
```yaml
my:
  map:
    "/key": "value"
```
---
Before this commit:
It's equivalent to
```yaml
my:
  map:
    "[key]": "value" # "[/key]" is expected
```
Such counter-intuitive behavior will confuse developers, there are several reported issues, incomplete list: spring-projectsGH-41099 spring-projectsGH-29582 spring-projectsGH-24548

---
After this commit:
```
***************************
APPLICATION FAILED TO START
***************************

Description:

Failed to bind properties under 'my.map' to java.util.Map<java.lang.String, java.lang.String>:

    Reason: java.lang.IllegalArgumentException: Please rewrite key '/key' to '[/key]' and surround it with quotes if YAML is using

Action:

Update your application's configuration

```
---

See spring-projectsGH-42802
quaff added a commit to quaff/spring-boot that referenced this issue Oct 28, 2024
Given:
```yaml
my:
  map:
    "/key": "value"
```
---
Before this commit:
It's equivalent to
```yaml
my:
  map:
    "[key]": "value" # "[/key]" is expected
```
Such counter-intuitive behavior will confuse developers, there are several reported issues, incomplete list: spring-projectsGH-41099 spring-projectsGH-29582 spring-projectsGH-24548

---
After this commit:
```
***************************
APPLICATION FAILED TO START
***************************

Description:

Failed to bind properties under 'my.map' to java.util.Map<java.lang.String, java.lang.String>:

    Reason: java.lang.IllegalArgumentException: Please rewrite key '/key' to '[/key]' and surround it with quotes if YAML is using

Action:

Update your application's configuration

```
---

See spring-projectsGH-42802
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
status: invalid An issue that we don't feel is valid
Projects
None yet
Development

No branches or pull requests

3 participants