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 59d0c0edf444 CAMEL-26863: camel-jbang - Every command in the shell 
panel fails with a NullPointerException (#29635)
59d0c0edf444 is described below

commit 59d0c0edf4449a267e2058c661c4c07ce37ef1fb
Author: Claus Ibsen <[email protected]>
AuthorDate: Wed Nov 11 16:13:28 2026 +0100

    CAMEL-26863: camel-jbang - Every command in the shell panel fails with a 
NullPointerException (#29635)
    
    The shell panel builds the commands with CamelJBangMain.newCommandLine(), a 
root command line that is never parsed. The shell runs a sub-command directly, 
and picocli then asks each parent command whether help was requested, which 
fails on the root without a parse result. CamelShellCommandRegistry now parses 
such a command line once with no arguments; camel shell keeps the parse result 
of the command line that ran it.
    
    Co-Authored-By: Claude Opus 5.5 <[email protected]>
    Signed-off-by: Claus Ibsen <[email protected]>
---
 .../core/commands/CamelShellCommandRegistry.java   |  6 +++++
 .../commands/CamelShellCommandRegistryTest.java    | 29 ++++++++++++++++++++++
 2 files changed, 35 insertions(+)

diff --git 
a/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/CamelShellCommandRegistry.java
 
b/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/CamelShellCommandRegistry.java
index 0b71d9887dab..17dec73819ce 100644
--- 
a/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/CamelShellCommandRegistry.java
+++ 
b/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/CamelShellCommandRegistry.java
@@ -71,6 +71,12 @@ public class CamelShellCommandRegistry extends 
PicocliCommandRegistry {
     public CamelShellCommandRegistry(CommandLine commandLine) {
         // TODO: replace with new PicocliCommandRegistry(commandLine, "Camel") 
when JLine merges #1947
         super(commandLine);
+        if (commandLine.getParseResult() == null) {
+            // the shell runs a sub-command directly, and picocli then asks 
each of its parents whether help was
+            // requested: a command line that was never parsed (such as 
CamelJBangMain.newCommandLine()) fails that
+            // with a NullPointerException, so parse it once with no arguments
+            commandLine.parseArgs();
+        }
     }
 
     @Override
diff --git 
a/dsl/camel-jbang/camel-jbang-core/src/test/java/org/apache/camel/dsl/jbang/core/commands/CamelShellCommandRegistryTest.java
 
b/dsl/camel-jbang/camel-jbang-core/src/test/java/org/apache/camel/dsl/jbang/core/commands/CamelShellCommandRegistryTest.java
index 6efcd7f61bd2..6a91c6f1f8da 100644
--- 
a/dsl/camel-jbang/camel-jbang-core/src/test/java/org/apache/camel/dsl/jbang/core/commands/CamelShellCommandRegistryTest.java
+++ 
b/dsl/camel-jbang/camel-jbang-core/src/test/java/org/apache/camel/dsl/jbang/core/commands/CamelShellCommandRegistryTest.java
@@ -37,11 +37,40 @@ class CamelShellCommandRegistryTest {
 
     @CommandLine.Command(name = "greet")
     static class Greet implements Runnable {
+        static int runs;
+
         @Override
         public void run() {
+            runs++;
         }
     }
 
+    @Test
+    void aCommandRunsOnACommandLineThatWasNeverParsed() throws Exception {
+        // the shell panel of camel tui builds the commands with 
CamelJBangMain.newCommandLine(), whose root is never
+        // parsed; picocli asks each parent of the command it runs whether 
help was requested (CAMEL-26863)
+        CamelShellCommandRegistry registry = new CamelShellCommandRegistry(new 
CommandLine(new Root()));
+        ByteArrayOutputStream out = new ByteArrayOutputStream();
+        int before = Greet.runs;
+
+        registry.command("greet").execute(session(out), new String[0]);
+        registry.command("camel").execute(session(out), new String[] { "greet" 
});
+
+        assertThat(Greet.runs).isEqualTo(before + 2);
+        
assertThat(out.toString(StandardCharsets.UTF_8)).doesNotContain("NullPointerException");
+    }
+
+    @Test
+    void theParseResultOfTheCommandLineThatRanTheShellIsKept() {
+        // camel shell hands over the command line that ran it
+        CommandLine commandLine = new CommandLine(new Root());
+        commandLine.parseArgs("greet");
+
+        new CamelShellCommandRegistry(commandLine);
+
+        
assertThat(commandLine.getParseResult().originalArgs()).containsExactly("greet");
+    }
+
     @Test
     void camelIsACommandOfTheShell() {
         CamelShellCommandRegistry registry = new CamelShellCommandRegistry(new 
CommandLine(new Root()));

Reply via email to