Configuring additional spring configuration metadata for list-nested properties

Viewed 248

I have the following model:

@ConfigurationProperties("example.foo")
class Foo {
 
  private String name;
  private List<Bar> bars;

  public static class Bar {

    private String url;
    private String type;

  }

}

I have configured properties for top-level class like this (in additional-spring-configuration-metadata.json):

{
  "properties": [
    {
      "name": "example.foo.name",
      "type": "java.lang.String",
      "description": "The name of foo"
    },
    {
      "name": "example.foo.bars",
      "type": "java.util.List<com.example.Foo.Bar>",
      "description": "These are bars of foo"
    }
  ]
}

How can I add metadata for Bar.url and Bar.type? I tried using something like example.foo.bars[].url but it didn't work.

1 Answers

Make sure you have getters and setters in both Foo and Bar classes. This configuration worked for me (I am using Lombok to generate getters/setters):

@ConfigurationProperties("example.foo")
@Getter
@Setter
class Foo {

  private String name;
  private List<Bar> bars;

  @Getter
  @Setter
  public static class Bar {

    private String url;
    private String type;

  }

}

Metadata generated by spring-boot-configuration-processor:

{
  "properties": [
    {
      "name": "example.foo.name",
      "type": "java.lang.String",
      "sourceType": "com.demo.Foo"
    },
    {
      "name": "example.foo.bars",
      "type": "java.util.List<com.example.Foo.Bar>",
      "sourceType": "com.demo.Foo"
    }
  ]
}
Related