This is an automated email from the ASF dual-hosted git repository.
davsclaus pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/camel.git
The following commit(s) were added to refs/heads/main by this push:
new 902a9fbaa3b4 CAMEL-24865: camel-jbang - source-dir must not replace a
missing classpath resource with a missing file
902a9fbaa3b4 is described below
commit 902a9fbaa3b41b77627ccbb8b4876a71d327d794
Author: Claus Ibsen <[email protected]>
AuthorDate: Mon Sep 21 11:39:43 2026 +0200
CAMEL-24865: camel-jbang - source-dir must not replace a missing classpath
resource with a missing file
Fixes https://issues.apache.org/jira/browse/CAMEL-24865
Under `camel run --source-dir` the resource loader replaced every
`classpath:` or `file:` resource that was not found with
`file:<source-dir>/<path>`, whether or not that file existed. A resource that
was optional (`?optional=true`) then stopped being optional: `JavaLanguage`
could not initialise (`classpath:camel-joor.properties?optional=true` became a
file that did not exist), so the `spring.datasource.url` auto-configure recipe
never ran and any sql route failed with `Property 'dat [...]
The same app started with `camel run sql.camel.yaml application.properties`
worked. With this change the source-dir branch only replaces the answer when
the file exists, like the fallback-dirs branch already did; a resource that
exists nowhere keeps its original answer so errors name the location as written.
Test:
`DependencyDownloaderResourceLoaderTest.aMissingResourceUnderSourceDirIsStillMissing`.
Verified by hand with an H2 datasource app under `--source-dir`: the
datasource is configured and the table is created.
---
.../download/DependencyDownloaderResourceLoader.java | 11 ++++++++---
.../DependencyDownloaderResourceLoaderTest.java | 20 ++++++++++++++++++++
2 files changed, 28 insertions(+), 3 deletions(-)
diff --git
a/dsl/camel-kamelet-main/src/main/java/org/apache/camel/main/download/DependencyDownloaderResourceLoader.java
b/dsl/camel-kamelet-main/src/main/java/org/apache/camel/main/download/DependencyDownloaderResourceLoader.java
index ee29cb7dfe06..29ccb8645a50 100644
---
a/dsl/camel-kamelet-main/src/main/java/org/apache/camel/main/download/DependencyDownloaderResourceLoader.java
+++
b/dsl/camel-kamelet-main/src/main/java/org/apache/camel/main/download/DependencyDownloaderResourceLoader.java
@@ -71,9 +71,14 @@ public class DependencyDownloaderResourceLoader extends
DefaultResourceLoader {
path = path.substring(2);
}
if (sourceDir != null) {
- // if not found then we need to look again inside the
source-dir which we can do
- // for file and classpath resources: force to load from file
system when using source-dir
- answer = super.resolveResource("file:" + sourceDir +
File.separator + path);
+ // if not found then we need to look again inside the
source-dir which we can do for file and
+ // classpath resources; only when the file is there: a
resource that exists nowhere keeps the
+ // original answer, so ?optional=true still means optional and
an error names it as written
+ // (CAMEL-24865: classpath:camel-joor.properties?optional=true
became a file that did not exist)
+ Resource candidate = super.resolveResource("file:" + sourceDir
+ File.separator + path);
+ if (candidate != null && candidate.exists()) {
+ answer = candidate;
+ }
} else {
// the files next to the routes: the first directory that has
it wins, else the original answer
// (so the error names the resource as written)
diff --git
a/dsl/camel-kamelet-main/src/test/java/org/apache/camel/main/download/DependencyDownloaderResourceLoaderTest.java
b/dsl/camel-kamelet-main/src/test/java/org/apache/camel/main/download/DependencyDownloaderResourceLoaderTest.java
index 810dd3d32dab..23d4b3f074e4 100644
---
a/dsl/camel-kamelet-main/src/test/java/org/apache/camel/main/download/DependencyDownloaderResourceLoaderTest.java
+++
b/dsl/camel-kamelet-main/src/test/java/org/apache/camel/main/download/DependencyDownloaderResourceLoaderTest.java
@@ -61,6 +61,26 @@ public class DependencyDownloaderResourceLoaderTest {
assertEquals("classpath:missing.groovy", resource.getLocation(), "the
error names the resource as written");
}
+ /** CAMEL-24865: under --source-dir a resource that exists nowhere keeps
its original (not found) answer. */
+ @Test
+ void aMissingResourceUnderSourceDirIsStillMissing() throws Exception {
+ Path sourceDir = Files.createDirectory(routes.resolve("src"));
+ SimpleCamelContext context = new SimpleCamelContext();
+ DependencyDownloaderResourceLoader loader
+ = new DependencyDownloaderResourceLoader(context,
sourceDir.toString(), List.of());
+
+ Resource resource =
loader.resolveResource("classpath:camel-joor.properties");
+ assertFalse(resource.exists());
+ assertEquals("classpath:camel-joor.properties", resource.getLocation(),
+ "not replaced by a file in the source dir that does not
exist");
+
+ // the form of the bug: with ?optional=true it is still optional
(JavaLanguage failed on it)
+ Resource optional =
loader.resolveResource("classpath:camel-joor.properties?optional=true");
+ assertFalse(optional.exists());
+ assertEquals("classpath:camel-joor.properties?optional=true",
optional.getLocation(),
+ "?optional=true resource must not be replaced by a
non-existent file");
+ }
+
@Test
void theSourceDirWinsWhenSet() throws Exception {
Path sourceDir = Files.createDirectory(routes.resolve("src"));