Skip to content

Configuration Processor should use the constructor as a source only with @ConstructorBinding #17035

@bdemers

Description

@bdemers

Starting with 2.2.0.M3 spring-boot-configuration-processor generates an incorrect spring-configuration-metadata.json when configuration property class contains another autowired ConfigurationProperties.

For example:

    @Autowired
    public BrokenConfigProperties(@Autowired(required = false) NestedConfigProperties nested) {
        this.nested = nested;
    }

This works in 2.1.5.RELEASE and 2.2.0.M1.
Full example: https://github.com/bdemers/spring-config-problem

Running this example with M2 or M3 creates metadata:

{
  "groups": [
    {
      "name": "another.namespace",
      "type": "com.example.spring.configproblem.NestedConfigProperties",
      "sourceType": "com.example.spring.configproblem.NestedConfigProperties"
    },
    {
      "name": "example.broken",
      "type": "com.example.spring.configproblem.BrokenConfigProperties",
      "sourceType": "com.example.spring.configproblem.BrokenConfigProperties"
    }
  ],
  "properties": [
    {
      "name": "another.namespace.other-property",
      "type": "java.lang.String",
      "sourceType": "com.example.spring.configproblem.NestedConfigProperties"
    },
    {
      "name": "example.broken.nested",
      "type": "com.example.spring.configproblem.NestedConfigProperties",
      "sourceType": "com.example.spring.configproblem.BrokenConfigProperties"
    }
  ],
  "hints": []
}

expected (and 2.1.5.RELEASE output):

{
  "groups": [
    {
      "name": "another.namespace",
      "type": "com.example.spring.configproblem.NestedConfigProperties",
      "sourceType": "com.example.spring.configproblem.NestedConfigProperties"
    },
    {
      "name": "example.broken",
      "type": "com.example.spring.configproblem.BrokenConfigProperties",
      "sourceType": "com.example.spring.configproblem.BrokenConfigProperties"
    }
  ],
  "properties": [
    {
      "name": "another.namespace.other-property",
      "type": "java.lang.String",
      "sourceType": "com.example.spring.configproblem.NestedConfigProperties"
    },
    {
      "name": "example.broken.a-string-value",
      "type": "java.lang.String",
      "sourceType": "com.example.spring.configproblem.BrokenConfigProperties"
    }
  ],
  "hints": []
}

In this case the property a-string-value is missing from the M3 version.

A work around (not fully tested), is to add a default constructor the the config properties object:

    private BrokenConfigProperties() {
        this(null);
    }

    @Autowired
    public BrokenConfigProperties(@Autowired(required = false) NestedConfigProperties nested) {
        this.nested = nested;
    }

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions