Skip to content

Recursive ConfigurationProperties class doesn't bind recursive properties #16444

@engineal

Description

@engineal

A ConfigurationProperties class with a recursive subclass will not bind the recursive properties. I have tested this with Spring Boot 2.1.3.

This issue can be reproduced using the following application.properties file:

test.x.value: "Test1"
test.x.test.value: "Test2"
test.x.test.test.value: "Test3"

I would expect this application.properties to be bound to the TestProperties bean as TestProperties{x=Test{value='Test1', test=Test{value='Test2', test=Test{value='Test3', test=null}}}}, but Spring is only creating this: TestProperties{x=Test{value='Test1', test=null}}

Here is the ConfigurationProperties class (enabled with@EnableConfigurationProperties({TestProperties.class})):

@ConfigurationProperties(prefix = "test")
public class TestProperties {

    static class Test {

        private String value;
        private Test test;

        public String getValue() {
            return value;
        }

        public void setValue(String value) {
            this.value = value;
        }

        public Test getTest() {
            return test;
        }

        public void setTest(Test test) {
            this.test = test;
        }

        @Override
        public String toString() {
            return "Test{" +
                    "value='" + value + '\'' +
                    ", test=" + test +
                    '}';
        }
    }

    private Test x;

    public Test getX() {
        return x;
    }

    public void setX(Test x) {
        this.x = x;
    }

    @Override
    public String toString() {
        return "TestProperties{" +
                "x=" + x +
                '}';
    }
}

In addition, the spring boot configuration processor does not support this class, it will throw a StackOverflowException.

See https://stackoverflow.com/questions/52906556/spring-configuration-properties-recursive-type

Metadata

Metadata

Assignees

No one assigned

    Labels

    status: declinedA suggestion or change that we don't feel we should currently apply

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions