Provide empty string to Quarkus as the default value for an environment variable

(quarkus 2.5.4.Final)

With the following .env file:

MY_VAR=

and the following application.properties files:

myapp.foo.bar=${MY_VAR}

and the following constructor

public MyClass(@ConfigProperty(name = "myapp.foo.bar") String bar)

I would like bar to be an empty string but I have the following error:

Failed to start Quarkus ... Caused by: io.quarkus.runtime.configuration.ConfigurationException: Failed to load config value of type class java.lang.String for: myapp.foo.bar

So far:

  • I tried to add the defaultValue = "" property to @ConfigProperty but it does not change anything.
  • I tried to add a colon: myapp.foo.bar=${MY_VAR:} or :null coupled with defaultValue.
  • I tried to add interpolation (though I could not find docs for that so I'm pasting random stuff): myapp.foo.bar=${MY_VAR:#{}}. The app starts but the variable is "}" and not an empty string.

How can I default to an empty string using an environment variable that is empty or not defined?


Solution 1:

I tested now with myapp.foo.bar=${MY_VAR:} and looks like that one empty String is not supported by the SmallRye OpenApi:

Caused by: javax.enterprise.inject.spi.DeploymentException: io.quarkus.runtime.configuration.ConfigurationException: Failed to load config value of type class java.lang.String for: myapp.foo.bar
    at io.quarkus.arc.runtime.ConfigRecorder.validateConfigProperties(ConfigRecorder.java:70)
    at io.quarkus.deployment.steps.ConfigBuildStep$validateConfigValues1665125174.deploy_0(Unknown Source)
    at io.quarkus.deployment.steps.ConfigBuildStep$validateConfigValues1665125174.deploy(Unknown Source)
    ... 13 more
    Suppressed: java.util.NoSuchElementException: SRCFG00040: The config property myapp.foo.bar is defined as the empty String ("") which the following Converter considered to be null: io.smallrye.config.Converters$BuiltInConverter
        at io.smallrye.config.SmallRyeConfig.convertValue(SmallRyeConfig.java:282)
        at io.smallrye.config.inject.ConfigProducerUtil.getValue(ConfigProducerUtil.java:81)
        at io.quarkus.arc.runtime.ConfigRecorder.validateConfigProperties(ConfigRecorder.java:60)
        ... 15 more
Caused by: io.quarkus.runtime.configuration.ConfigurationException: Failed to load config value of type class java.lang.String for: myapp.foo.bar

The framework is creating one null object instead one empty String.

I don't know if it would work for you, but you could create one String with one blank space, putting one blank space after the :, like this:

myapp.foo.bar=${MY_VAR: }

But, in this case your variable would be " " and not one empty String.