Skip to content

Configuration properties binding bug with * in property name #24548

Closed
@ztomic

Description

@ztomic

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,

Metadata

Metadata

Assignees

No one assigned

    Labels

    status: invalidAn issue that we don't feel is valid

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions