hoplite: Using strict() throws an error loading config listing unused Environment Variables.

Using the following example:

application.yaml

foo:
   enabled: true

and this code

import com.sksamuel.hoplite.ConfigLoader
import com.sksamuel.hoplite.PropertySource

object Application {

    @JvmStatic
    fun main(args: Array<String>) {
        ConfigLoader.Builder()
            .addSource(PropertySource.resource("/application.yaml"))
            .strict()
            .build()
            .loadConfigOrThrow<Config>()
    }
}

data class Config(val foo: Foo)
data class Foo(val enabled: Boolean)

fails, with an error similar to:

Exception in thread "main" com.sksamuel.hoplite.ConfigException: Error loading config because:

    Config values were not used: PATH, ...snip...
	at com.sksamuel.hoplite.ConfigLoader$returnOrThrow$1.invoke(ConfigLoader.kt:335)
	at com.sksamuel.hoplite.ConfigLoader$returnOrThrow$1.invoke(ConfigLoader.kt:20)
	at com.sksamuel.hoplite.fp.ValidatedKt.getOrElse(Validated.kt:93)
	at com.sksamuel.hoplite.ConfigLoader.returnOrThrow(ConfigLoader.kt:332)
...

Can the Environment, System and UserSettings property sources be optional for strict?

Perhaps change strict() to accept override flags (with defaults matching current usage):

strict(
  includeEnvironmentVariables = true, 
  includeSystemProperties = true,
  includeUserSettings = true
)

About this issue

  • Original URL
  • State: closed
  • Created 3 years ago
  • Reactions: 5
  • Comments: 21 (11 by maintainers)

Most upvoted comments

Yes i have a lot done locally and want to release with kotlin 1.6 as durations will be no longer experimental.

On Thu, 30 Sep 2021, 18:06 Chris Ryan, @.***> wrote:

Still need this. Any progress on the 2.0 version?

— You are receiving this because you modified the open/close state. Reply to this email directly, view it on GitHub https://github.com/sksamuel/hoplite/issues/201#issuecomment-931763135, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAFVSGWR63BZH6IJLIAUUE3UETUOHANCNFSM4YYXETNA . Triage notifications on the go with GitHub Mobile for iOS https://apps.apple.com/app/apple-store/id1477376905?ct=notification-email&mt=8&pt=524675 or Android https://play.google.com/store/apps/details?id=com.github.android&referrer=utm_campaign%3Dnotification-email%26utm_medium%3Demail%26utm_source%3Dgithub.

I think strict on env’s makes no sense actually, as there’s always likely to be env’s no used. I mean hoplite doesn’t control the entire env right 😃 Same for system properties. I’m surprised the tests didn’t catch this. It should only apply to files imo.

I am using env vars, and I want to be able to use strict() at the same time.

My config loader builder looks like this

private val configLoader = ConfigLoader {
    strict()
    addEnvironmentSource()
}

Edit: I guess there’s no good way to check unused for env vars. My use case was usage of a file config that can be overridden by env vars. The example above is the simplified version that had the issue reproducible.

I think here the solution is don’t use the env property source. The built in env param mapper will do the ${name} replacements.

Still need this. Any progress on the 2.0 version?

Yes, I was waiting for Kotlin 1.5 to come out to make these changes, and release a 2.0 version (since it will require breaking some internals, which may affect some people who write custom decoders). Since Kotlin 1.5 came out yesterday I’ll do this work at the weekend and release then.

On Wed, 5 May 2021 at 07:54, John Newman @.***> wrote:

Hi there, I am having the same issue with env variables, I really like the use of strict mode however due to it picking up the env variables I am unable to use it. Any idea if this is in the horizon to fix?

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/sksamuel/hoplite/issues/201#issuecomment-832663498, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAFVSGTPNLMGSWBBSRZ3KP3TME5ZXANCNFSM4YYXETNA .