Skip to content

Fix #2348 - ignore non-String environment properties (ClassCastException)#2446

Merged
arnaudgiuliani merged 1 commit into
4.2.2from
fix/2348-env-properties-cast
Jun 12, 2026
Merged

Fix #2348 - ignore non-String environment properties (ClassCastException)#2446
arnaudgiuliani merged 1 commit into
4.2.2from
fix/2348-env-properties-cast

Conversation

@arnaudgiuliani

Copy link
Copy Markdown
Member

Problem

PropertyRegistry.saveProperties cast every java.util.Properties entry to Map<String, String>:

val propertiesMapValues = properties.toMap() as Map<String, String>
propertiesMapValues.forEach { (k: String, v: String) -> saveProperty(k, v) }

Properties can legally hold non-String keys/values (e.g. after System.setProperties(...) with arbitrary objects — happens with some libraries/runtimes). The destructuring cast then throws ClassCastException on startKoin / loadEnvironmentProperties. Fixes #2348.

Fix

Filter to String/String entries and ignore the rest (debug-logged), per the issue's expected behavior ("start fine, ignoring invalid properties"). Removes the unchecked cast:

properties.forEach { (k, v) ->
    if (k is String && v is String) saveProperty(k, v)
    else _koin.logger.debug("ignore non-string property '$k'")
}

Test

EnvironmentPropertiesTest (jvmTest) — a Properties containing a non-String key, a non-String value, and a both-non-String entry no longer throws; the valid string property still loads, the invalid ones are ignored. RED (ClassCastException) → GREEN.

Verification

  • :core:koin-core:jvmTest green; apiCheck pass.
  • JVM-only change (jvmMain), so no off-JVM targets involved.

🤖 Generated with Claude Code

…ption)

PropertyRegistry.saveProperties cast every java.util.Properties entry to
Map<String, String>, so a non-String key or value (legal in Properties, e.g.
after System.setProperties(...) with arbitrary objects) crashed startup with
ClassCastException.

The registry is String-keyed but stores Any values (PropertyRegistry._values is
Map<String, Any>, getProperty uses `as? T`). So the correct fix keeps any
String-keyed entry with its value as-is (Any) and only drops non-String keys
(which can't be addressed as Koin property keys) — rather than forcing values
to String. Removes the unchecked cast.

Regression test (jvmTest): EnvironmentPropertiesTest — Properties with a
non-String key, plus a String key holding an arbitrary object, no longer throws;
the object value is preserved and retrievable, valid string props still load.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
@arnaudgiuliani arnaudgiuliani force-pushed the fix/2348-env-properties-cast branch from 1486bbc to 26020e8 Compare June 12, 2026 12:54
@arnaudgiuliani arnaudgiuliani merged commit 0029f34 into 4.2.2 Jun 12, 2026
4 checks passed
@UpcraftLP

Copy link
Copy Markdown

Thank you for fixing this, now I can finally update my koin app.

Tho, is there any reason to drop all non-strings instead of allowing them as valid keys, like in #2358?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants