Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use configuration maps for custom Quarkus ConfigProperties

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?

like image 207
mika Avatar asked Jan 22 '26 23:01

mika


1 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
like image 184
gaetanc Avatar answered Jan 24 '26 12:01

gaetanc