4

Here's a sample application.yml file

myapp:
  resource:
    teststring: "string"
    testlist:
      - "apple"
      - "banana"

Here's my immutables configuration class

import org.immutables.value.Value.Immutable;
import org.immutables.value.Value.Modifiable;

@Immutable
@Modifiable
public abstract class ResourceConfiguration {

  public abstract String teststring();

  public abstract List<String> testlist();

}

Here's my top-level configuration class

@ConstructorBinding
@ConfigurationProperties(prefix = "myapp")
public class MyAppConfiguration {

  private final ImmutableResourceConfiguration resource;

  public MyAppConfiguration(ImmutableResourceConfiguration resource) {
    this.resource = resource.toImmutable();
    System.out.println("Look here: "+resource);
  }

It reads the teststring value fine. But the testlist is empty.

Note that I can read the testlist fine if it's not wrapped in an Immutables For example if I changed application.yml to

myapp:
  resource:
    teststring: "string"
  testlist:
    - "apple"
    - "banana"

and I changed my top-level class to:

@ConstructorBinding
@ConfigurationProperties(prefix = "myapp")
public class MyAppConfiguration {

  private final ImmutableResourceConfiguration resource;

  public MyAppConfiguration(ImmutableResourceConfiguration resource, List<String> testlist) {
    this.resource = resource.toImmutable();
    System.out.println("Look here: "+testlist);
  }

Then testlist is correctly populated.

So I'm assuming it has to do with the way the Immutables library processes lists in spring configuration files.

kane
  • 5,465
  • 6
  • 44
  • 72
  • 1
    By the way, while you did apply a tag to your question, there are several library annotations with the simple name `@Immutable`, so it would be helpful to specify the fully-qualified name and possibly link to the library in question. Additionally, the combination of `@Immutable @Modifiable` seems... confusing. – chrylis -cautiouslyoptimistic- Feb 20 '21 at 00:49
  • I added the import statements to be clear about which `@Immutable`. The `@Immutable` tag auto-generates a `ImmutableResourceConfiguration` and the `@Modifiable` tag auto-generates a `ModifiableResourceConfiguration`. It's a little confusing but I need both – kane Feb 20 '21 at 02:21

1 Answers1

3

After some experimentation, I found out that you have to read it as an String[] rather than List<String>

@Immutable
@Modifiable
public abstract class ResourceConfiguration {

  public abstract String[] testlist();

}

I don't see this documented anywhere so if anyone can provide that, I will happily select that as the better answer.

kane
  • 5,465
  • 6
  • 44
  • 72