Skip to content

Return false from PropertiesConfigReader.canLoad for missing resources - #770

Open
abhiishek340 wants to merge 1 commit into
Netflix:2.xfrom
abhiishek340:fix-properties-reader-canload
Open

abhiishek340 wants to merge 1 commit into
Netflix:2.xfrom
abhiishek340:fix-properties-reader-canload

Conversation

@abhiishek340

Copy link
Copy Markdown

Fixes #511.

PropertiesConfigReader.canLoad(ClassLoader, String) returns getResources(loader, name) != null. But getResources always returns a (possibly empty) LinkedHashSet, so canLoad is true for every resource name.

DefaultConfigLoader's Loader.load(String) uses the first reader whose canLoad is true and then breaks. When PropertiesConfigReader is tried first, a missing foo.properties stops any other reader from being tried. For example, TypesafeConfigReader (whose canLoad is always true) never gets a chance at foo.conf. Because the builder keeps readers in a HashSet, whether this happens depends on hash order, so the symptom can come and go between runs.

The fix checks !getResources(loader, name).isEmpty() instead. If no reader can load a name, load(String) already skips it without an error, so a missing resource still loads nothing; the only change is that later readers get their turn.

Testing:

  • New PropertyConfigReaderTest.canLoadOnlyExistingResources: canLoad is true for application / application.properties (test resources) and false for a missing name, with or without the suffix. It fails before the fix and passes after.
  • ./gradlew :archaius2-core:test :archaius2-typesafe:test -DtestJdk=21: 201 tests, 0 failures (Java 8 toolchain for compilation, as the build configures).

canLoad(ClassLoader, String) returned `getResources(...) != null`, but
getResources always returns a set, so the reader claimed every resource
name. DefaultConfigLoader stops at the first reader whose canLoad is
true, so when it was tried first, a missing `foo.properties` kept other
readers (such as TypesafeConfigReader for `foo.conf`) from being tried.

Check that the set is non-empty instead.

Fixes Netflix#511

This branch has not been deployed

No deployments
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.

PropertiesConfigReader.canLoad never returns false

1 participant