Search code examples
kotlincdiquarkus

Why does Quarkus complain about my (unused) @ApplicationScoped data class with a @ConfigProperty property?


This kotlin data class is part of an internal library used in some Quarkus microservices (Quarkus 2.0.0.Final):

@ApplicationScoped
data class FooConfiguration(
  @ConfigProperty(name = "foo.bar")
  val fooBar: String
)

The library is used in a few microservices, and most of them do use that configuration. But some are not. For those, the foo.bar property is not defined in the application.properties. I would expect this to not matter at all, as those services are never injecting a FooConfiguration, so I'd expect that to never be constructed.

However, the application refuses startup with this error message:

SRCFG00014: The config property foo.bar is required but it could not be found in any config source

I know how to workaround this issue (simply supplying a nonsense value), but I currently wonder why this is an issue in the first place. The configuration bean should never get constructed. Why is this happening?


Solution

  • This is a MicroProfile Config-related issue.

    Your foo.bar property is optional, not required, in the parlance of the MicroProfile Config specification, because a value for it is not present in any configuration source, as you have indicated. To inject a value for an optional MicroProfile Config configuration property, you need to use java.util.Optional. I don't know Kotlin, so here is what it would look like in Java:

    @Inject
    @ConfigProperty(name = "foo.bar")
    private Optional<String> fooBar;