How to parse a list of custom objects from Gradle DSL inside a Java extension

Hi,

while it’s clear to me how to support a map of custom objects in Gradle DSL using a NamedDomainObjectContainer, I can’t find any way to support a list of anonymous custom objects.

Here’s what I’d like to have in my DSL:

        identifiers = [
          {
            position = 'BUILD'
            qualifier = 'branch'
            value = '123'
          },
          {
            position = 'BUILD'
            qualifier = 'commit'
            value = '456'
          },
          {
            position = 'BUILD'
            qualifier = 'timestamp'
            value = '789'
          }
        ]

and here’s the java snippet from my extension:

private final ListProperty<Identifier> identifiers = getObjectfactory().listProperty(Identifier.class);

public ListProperty<Identifier> getIdentifiers() {
  return identifiers;
}

public void identifiers(Action<? super ListProperty<Identifier>> configurationAction) {
  configurationAction.execute(identifiers); // this is invoked but does nothing
}

where Identifier is the class of the items.

I always get an exception because the closure has some unknown type of object. But if I replace the DSL block with this:

        identifiers {[
          {
            position = 'BUILD'
            qualifier = 'branch'
            value = '123'
          },
          {
            position = 'BUILD'
            qualifier = 'commit'
            value = '456'
          },
          {
            position = 'BUILD'
            qualifier = 'timestamp'
            value = '789'
          }
        ]}

I have no exception, but still the list is not parsed.

Any help? Is it even possible to parse a list of custom objects?

Hi, I was also strugeling with the same problem
In my extension I define a list property and a method that accepts a map.

{code}
abstract public ListProperty getApis();

public ApiConfiguration apiConfig(final Map<String, Object> map) {
//get arguments from the map and pass them to the ApiConfiguration constructor
return new ApiConfiguration();
}

[code]

Then in my build file I call the method with the map parameter to construct the custom object.
With that I was able to get the values of the listproperty in my plugin.

[code]
openapi {
basePackageName = ‘io.github.petstore’
apis = [
apiConfig(
openApiFile: new File(project.getProjectDir(), “petstore/petstore.yaml”).absolutePath,
basePackageName: “io.github.petstore”
)
]
}
{code}