Fix local cache path fallback for Spring Boot 3 executable JARs#136
Fix local cache path fallback for Spring Boot 3 executable JARs#136nobodyiam wants to merge 1 commit intoapolloconfig:mainfrom
Conversation
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: defaults Review profile: CHILL Plan: Pro Run ID: 📒 Files selected for processing (3)
📝 WalkthroughWalkthroughThis PR fixes Apollo client's local cache file creation for Spring Boot 3 by refactoring Changes
Estimated code review effort🎯 2 (Simple) | ⏱️ ~12 minutes Suggested reviewers
Poem
🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
3bbe114 to
1055f6b
Compare
There was a problem hiding this comment.
Pull request overview
This PR fixes Apollo Java’s local cache directory resolution when running from Spring Boot 3 executable (nested) JARs by avoiding treating nested-jar classpath URLs as filesystem paths, and instead falling back to a safe default (user.dir).
Changes:
- Refactors
ClassLoaderUtilclasspath resolution to only treatfile:URLs as filesystem locations and otherwise fall back to a provided default. - Adds regression tests covering both
file:URL resolution and Spring Boot 3 nested-jar URL fallback behavior.
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
| apollo-core/src/main/java/com/ctrip/framework/apollo/core/utils/ClassLoaderUtil.java | Introduces resolveClassPath(...) using URL protocol checks and Paths.get(url.toURI()) to prevent nested-jar URLs from being used as filesystem paths. |
| apollo-core/src/test/java/com/ctrip/framework/apollo/core/utils/ClassLoaderUtilTest.java | Adds tests validating correct handling for file: URLs and fallback behavior for nested-jar URLs. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| static String resolveClassPath(ClassLoader classLoader, String defaultClassPath) throws Exception { | ||
| URL url = classLoader.getResource(""); | ||
| if (url == null || !"file".equalsIgnoreCase(url.getProtocol())) { | ||
| return defaultClassPath; | ||
| } | ||
|
|
||
| String resolvedClassPath = Paths.get(url.toURI()).toString(); | ||
| if (Strings.isNullOrEmpty(resolvedClassPath)) { | ||
| return defaultClassPath; | ||
| } |
There was a problem hiding this comment.
resolveClassPath now derives the classpath via Paths.get(url.toURI()).toString(), which changes the returned format on Windows (it will no longer include the leading / that URL#getPath() produced). There is existing code in the repo that compensates for the old behavior (e.g. apollo-client/src/test/java/com/ctrip/framework/apollo/BaseIntegrationTest.java strips the leading /), which will become incorrect with this change and can break Windows builds/tests. Consider either preserving the previous Windows formatting (e.g. keep using the decoded url.getPath() for file: URLs) or updating the affected call sites/tests to stop assuming a leading / from getClassPath().
What's the purpose of this PR
Fix Apollo Java local cache directory resolution when running from a Spring Boot 3 executable JAR.
ClassLoaderUtilpreviously assumed that jar-internal classpath locations would contain.jar!, but Spring Boot 3 executable JARs expose nested URLs such asjar:nested:/.../!BOOT-INF/classes/!/. When Apollo falls back toClassLoaderUtil.getClassPath()for local cache persistence, that nested URL was treated as a filesystem path andconfig-cachedirectory creation failed.Which issue(s) this PR fixes:
Fixes apolloconfig/apollo#5592
Brief changelog
file:resources as filesystem classpath locations and fall back touser.dirfor nested-jar URLs.mvn -pl apollo-core -Dtest=ClassLoaderUtilTest test -Dmaven.gitcommitid.skip=truemvn -pl apollo-client -am -Dtest=ClassLoaderUtilTest,LocalFileConfigRepositoryTest,DefaultConfigTest -Dsurefire.failIfNoSpecifiedTests=false test -Dmaven.gitcommitid.skip=trueFollow this checklist to help us incorporate your contribution quickly and easily:
mvn clean testto make sure this pull request doesn't break anything.CHANGESlog.Summary by CodeRabbit