How to use configuration maps for custom Quarkus ConfigProperties

Viewed 4343

For my Quarkus application I'm looking for a way to define a configuration map from within a custom ConfigProperties class. I tried the following:

import io.quarkus.arc.config.ConfigProperties;
import io.quarkus.runtime.annotations.ConfigItem;

@ConfigProperties(prefix = "my-properties")
public class MyPropertiesConfiguration { 

    @ConfigItem
    public Map<String, FooConfiguration> foo;

    // ...
}
import io.quarkus.runtime.annotations.ConfigGroup;
import io.quarkus.runtime.annotations.ConfigItem;

@ConfigGroup
public class FooConfiguration {

  @ConfigItem
  public String myProperty;
}

Given those two classes and the following application.properties file...

my-properties.foo.anystring.my-property=bar

on startup the application fails with error message: javax.enterprise.inject.spi.DeploymentException: No config value of type [java.util.Map] exists for: my-properties.foo

As far as I understand https://quarkus.io/guides/writing-extensions#configuration-maps the sample should work. What am I doing wrong? Could it happen that this functionality is just limited to Quarkus extensions only?

2 Answers

As written in this Quarkus github issue, this is currently not supported.

My dirty workaround was to use the ConfigProvider directly. Use with care.

  public static Map<String, String> getMapFromConfig(String prefix) {
    final Config config = ConfigProvider.getConfig();
    final Iterable<String> propertyNames = config.getPropertyNames();
    return StreamSupport.stream(propertyNames.spliterator(), false)
        .filter(name -> name.startsWith(prefix) && !name.equalsIgnoreCase(prefix))
        .collect(
            Collectors.toMap(
                propertyName -> cleanupPropertyName(propertyName.substring(prefix.length() + 1)),
                propertyName -> config.getOptionalValue(propertyName, String.class).orElse("")));
  }

  /** Remove start and end double quotes */
  public static String cleanupPropertyName(String name) {
    if (name.startsWith("\"") && name.endsWith("\"")) {
      return name.substring(1, name.length() - 1);
    }
    return name;
  }

My config looks like this:

property-templates:
  "my.key": value 1
  "my.second.key": value 2

Declare the configuration like this

import io.quarkus.arc.config.ConfigProperties;

@ConfigProperties(prefix = "myapp")
public class AppSpecificConfig {

    public String property;

}

The application.properties file will contain

myapp.property=foo

And then you can @Inject an instance of this class anywhere within your application.

For more details, see https://quarkus.io/guides/config#using-configproperties

Related