Skip to content

Fall back to the library class loader for classpath resources - #1265

Merged
stevehu merged 1 commit into
networknt:masterfrom
joaodinissf:classpath-resource-loader-fallback
Jul 7, 2026
Merged

stevehu merged 1 commit into
networknt:masterfrom
joaodinissf:classpath-resource-loader-fallback

Conversation

@joaodinissf

Copy link
Copy Markdown
Contributor

Closes #1264

Retries with the library's own class loader after the thread context class loader misses. The TCCL still wins on a hit; a custom class loader source is never overridden. The test covers the fallback, TCCL precedence, and custom-source passthrough with stub class loaders — no new test dependencies. (Also verified in an embedded Felix framework; that harness is not committed.)

In OSGi runtimes the thread context class loader cannot see
this library's bundled meta-schemas, so resolution fails with
FileNotFoundException: classpath:draft/2020-12/schema. The 1.x
ClasspathURLStreamHandler chained class loaders; the chain was not
carried over in the schema retrieval refactor (#931).

The fallback runs only after the TCCL misses, so flat classpath
behavior is unchanged, and it is skipped when a custom class loader
source is supplied.
@stevehu

stevehu commented Jul 7, 2026

Copy link
Copy Markdown
Contributor

No blocking findings.

Risk: Medium-low. The code change is tiny, but ClasspathResourceLoader#getResource sits under schema loading and $ref/meta-schema resolution. GitNexus shows direct callers in
SchemaLoader.getSchemaResource, SchemaLoader.getClasspathResource, and Schema.getInputResource, with the affected flow centered on schema loading.

Coverage looks good for the intended behavior:

  • fallback when TCCL cannot see bundled resources: json-schema-validator/src/test/java/com/networknt/schema/resource/ClasspathResourceLoaderTcclTest.java:66
  • TCCL still wins when it can resolve the resource: json-schema-validator/src/test/java/com/networknt/schema/resource/ClasspathResourceLoaderTcclTest.java:74
  • explicit custom classloader source is not overridden: json-schema-validator/src/test/java/com/networknt/schema/resource/ClasspathResourceLoaderTcclTest.java:95

Areas to improve, non-blocking:

  • There is no committed real OSGi/Felix integration test. The stub classloader regression test is reasonable and cheap, but the actual bundle classloader behavior remains manually
    verified.

  • The fallback uses ResourceLoader.class.getClassLoader() at json-schema-validator/src/main/java/com/networknt/schema/resource/ClasspathResourceLoader.java:85. That should be equivalent
    in this package, but ClasspathResourceLoader.class.getClassLoader() would make the intent slightly more local and explicit.

@stevehu

stevehu commented Jul 7, 2026

Copy link
Copy Markdown
Contributor

It appears that PR #1265 contains the exact same code changes as PR #1266 (both modifying ClasspathResourceLoader.java and adding ClasspathResourceLoaderTcclTest.java with 123 insertions). They likely represent duplicate PRs opened by the author (perhaps accidentally or against different branches).

Since the codebase changes are identical, my previous review applies in its entirety to this PR as well.

To summarize:

  • The fix is correct: It safely falls back to the library's own class loader when the Thread Context Class Loader (TCCL) fails to find bundled resources (which is the root cause of the OSGi issue).
  • It preserves backward compatibility: Custom class loader suppliers are respected and do not trigger the fallback unexpectedly.
  • The tests are solid: They cover both the blind TCCL case and cases where TCCL and the library class loader compete.

Status: Approved / LGTM (Looks Good To Me).

You can safely merge whichever of the two PRs (#1265 or #1266) targets the branch you intend to update, and close the other as a duplicate!

@stevehu
stevehu merged commit 8bf1423 into networknt:master Jul 7, 2026
@joaodinissf
joaodinissf deleted the classpath-resource-loader-fallback branch July 7, 2026 23:34
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.

Bundled meta-schemas fail to load under OSGi (TCCL cannot see the library's resources)

2 participants