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 83702e9fb1da CAMEL-26933: camel-jbang - camel generate schema 
--verbose tells what it does on stderr, so stdout stays the schema (#30022)
83702e9fb1da is described below

commit 83702e9fb1da30fbb67eaddb4c756b2569c348cc
Author: Claus Ibsen <[email protected]>
AuthorDate: Sun Nov 15 16:05:23 2026 +0100

    CAMEL-26933: camel-jbang - camel generate schema --verbose tells what it 
does on stderr, so stdout stays the schema (#30022)
    
    … it does on stderr, so stdout stays the schema
    
    camel generate schema --verbose printed its progress (the component, the 
class, the downloaded JARs) on stdout before
    the schema, so camel generate schema ... --verbose > schema.json was not 
JSON. The progress now goes to stderr, and the
    hints after an error line no longer start with ERROR (a class that is not 
found printed an empty ERROR line and nine
    more). The class Javadoc lists the real exit codes, its example class 
exists, and camel-jbang-projects.adoc says the
    schema goes to stdout unless --output is given.
    
    Co-Authored-By: Claude Opus 5.5 <[email protected]>
---
 .../modules/ROOT/pages/camel-jbang-projects.adoc   |  6 +-
 .../commands/generate/CodeSchemaGenerator.java     | 70 +++++++++--------
 .../commands/generate/CodeSchemaGeneratorTest.java | 90 ++++++++++++++++++++++
 3 files changed, 131 insertions(+), 35 deletions(-)

diff --git a/docs/user-manual/modules/ROOT/pages/camel-jbang-projects.adoc 
b/docs/user-manual/modules/ROOT/pages/camel-jbang-projects.adoc
index e99189cddf57..48b387e9dde2 100644
--- a/docs/user-manual/modules/ROOT/pages/camel-jbang-projects.adoc
+++ b/docs/user-manual/modules/ROOT/pages/camel-jbang-projects.adoc
@@ -426,10 +426,12 @@ Generate JSON Schema definitions from Java objects for 
use with visual developme
 
 [source,bash]
 ----
-camel generate schema fhir org.hl7.fhir.r4.model.Patient
-Schema saved to: ./org.hl7.fhir.r4.model.Patient-schema.json
+camel generate schema fhir org.hl7.fhir.r4.model.Patient > patient-schema.json
 ----
 
+The schema is printed on stdout; `--output=patient-schema.json` writes it to a 
file instead. `--verbose` tells on
+stderr what the command does (the dependencies it downloads, the class it 
loads), so stdout stays the schema.
+
 == Version management
 
 Show the current Camel CLI version:
diff --git 
a/dsl/camel-jbang/camel-jbang-plugin-generate/src/main/java/org/apache/camel/dsl/jbang/core/commands/generate/CodeSchemaGenerator.java
 
b/dsl/camel-jbang/camel-jbang-plugin-generate/src/main/java/org/apache/camel/dsl/jbang/core/commands/generate/CodeSchemaGenerator.java
index fc855bbd9217..c3279503347e 100644
--- 
a/dsl/camel-jbang/camel-jbang-plugin-generate/src/main/java/org/apache/camel/dsl/jbang/core/commands/generate/CodeSchemaGenerator.java
+++ 
b/dsl/camel-jbang/camel-jbang-plugin-generate/src/main/java/org/apache/camel/dsl/jbang/core/commands/generate/CodeSchemaGenerator.java
@@ -47,14 +47,17 @@ import tools.jackson.databind.json.JsonMapper;
  * This command downloads the specified Camel component dependencies and 
generates a JSON Schema (Draft 2020-12) for the
  * given fully qualified class name.
  *
- * Usage Examples: - camel generate schema fhir org.hl7.fhir.r4.model.Patient 
- camel generate schema http
- * org.apache.camel.component.http.HttpConfiguration
+ * Usage Examples: - camel generate schema fhir org.hl7.fhir.r4.model.Patient 
- camel generate schema timer
+ * org.apache.camel.component.timer.TimerComponent
  *
  * The generated schema includes: - Public and non-public fields (with 
getters) - Flattened enums using toString() -
  * Jackson annotations support
  *
- * Exit Codes: - 0: Success - 1: Component not found or dependency download 
failed - 2: Class not found - 3: Schema
- * generation failed - 4: General error
+ * The schema goes to stdout, or to the file of --output; --verbose tells on 
stderr what the command does.
+ *
+ * Exit Codes: - 0: Success - 1: the dependencies cannot be downloaded, or the 
schema cannot be generated - 2: a
+ * component or class name that is empty - 3: the class is not in the 
dependencies of the component - 70: an unexpected
+ * error
  */
 @CommandDefinition(name = "schema", description = "Create a JSON schema for a 
given Camel component and Java Object",
                    generateHelp = true, sortOptions = true)
@@ -90,9 +93,9 @@ public class CodeSchemaGenerator extends CamelCommand {
             }
 
             if (verbose) {
-                printer().println("Generating JSON Schema for component: " + 
camelComponent);
-                printer().println("Class: " + fullyQualifiedName);
-                printer().println("Camel version: " + camelVersion);
+                printer().printDiagnostic("Generating JSON Schema for 
component: " + camelComponent);
+                printer().printDiagnostic("Class: " + fullyQualifiedName);
+                printer().printDiagnostic("Camel version: " + camelVersion);
             }
 
             // Validate inputs
@@ -137,7 +140,7 @@ public class CodeSchemaGenerator extends CamelCommand {
                 outputSchema(schema);
 
                 if (verbose) {
-                    printer().println("Schema generation completed 
successfully");
+                    printer().printDiagnostic("Schema generation completed 
successfully");
                 }
 
                 return 0;
@@ -181,12 +184,12 @@ public class CodeSchemaGenerator extends CamelCommand {
      * @param  verbose        Whether to enable verbose logging
      * @return                DependencyDownloaderClassLoader or null if failed
      */
-    private DependencyDownloaderClassLoader getDependencyDownloaderClassLoader(
+    DependencyDownloaderClassLoader getDependencyDownloaderClassLoader(
             String camelComponent, String version, boolean verbose) {
 
         try {
             if (verbose) {
-                printer().println("Creating dependency downloader class 
loader...");
+                printer().printDiagnostic("Creating dependency downloader 
class loader...");
             }
 
             DependencyDownloaderClassLoader cl = new 
DependencyDownloaderClassLoader(
@@ -199,7 +202,7 @@ public class CodeSchemaGenerator extends CamelCommand {
                 downloader.start();
 
                 if (verbose) {
-                    printer().println("Downloading artifacts for camel-" + 
camelComponent + ":" + version);
+                    printer().printDiagnostic("Downloading artifacts for 
camel-" + camelComponent + ":" + version);
                 }
 
                 List<MavenArtifact> artifacts = downloader.downloadArtifacts(
@@ -211,15 +214,15 @@ public class CodeSchemaGenerator extends CamelCommand {
                 if (artifacts == null || artifacts.isEmpty()) {
                     printer().printErr(
                             "Error: No artifacts found for component 'camel-" 
+ camelComponent + "' version '" + version + "'");
-                    printer().printErr("Please verify that the component name 
is correct and the version exists.");
-                    printer().printErr("Available components can be found at: 
https://camel.apache.org/components/";);
+                    printer().printDiagnostic("Please verify that the 
component name is correct and the version exists.");
+                    printer().printDiagnostic("Available components can be 
found at: https://camel.apache.org/components/";);
                     IOHelper.close(cl);
                     return null;
                 }
 
                 if (verbose) {
-                    printer().println("Downloaded " + artifacts.size() + " 
artifact(s)");
-                    artifacts.forEach(artifact -> printer().println("  - " + 
artifact.getFile().getName()));
+                    printer().printDiagnostic("Downloaded " + artifacts.size() 
+ " artifact(s)");
+                    artifacts.forEach(artifact -> printer().printDiagnostic("  
- " + artifact.getFile().getName()));
                 }
 
                 artifacts.forEach(artifact -> cl.addFile(artifact.getFile()));
@@ -259,7 +262,7 @@ public class CodeSchemaGenerator extends CamelCommand {
     private Class<?> loadTargetClass(ClassLoader classLoader, String 
className) {
         try {
             if (verbose) {
-                printer().println("Loading class: " + className);
+                printer().printDiagnostic("Loading class: " + className);
             }
 
             return classLoader.loadClass(className);
@@ -267,16 +270,17 @@ public class CodeSchemaGenerator extends CamelCommand {
         } catch (ClassNotFoundException e) {
 
             printer().printErr("Class '" + className + "' not found in 
component dependencies");
-            printer().printErr(System.lineSeparator());
-            printer().printErr("Possible causes:");
-            printer().printErr("  - Class name is incorrect (check spelling 
and package)");
-            printer().printErr("  - Class is not part of the '" + 
camelComponent + "' component");
-            printer().printErr("  - Class is in a different artifact not 
included as dependency");
-            printer().printErr(System.lineSeparator());
-            printer().printErr("To find available classes, you can:");
-            printer().printErr("  - Check the component documentation");
-            printer().printErr("  - Browse the Maven artifact at: 
https://mvnrepository.com/artifact/org/apache/camel/camel-";
-                               + camelComponent);
+            printer().printDiagnostic("");
+            printer().printDiagnostic("Possible causes:");
+            printer().printDiagnostic("  - Class name is incorrect (check 
spelling and package)");
+            printer().printDiagnostic("  - Class is not part of the '" + 
camelComponent + "' component");
+            printer().printDiagnostic("  - Class is in a different artifact 
not included as dependency");
+            printer().printDiagnostic("");
+            printer().printDiagnostic("To find available classes, you can:");
+            printer().printDiagnostic("  - Check the component documentation");
+            printer().printDiagnostic(
+                    "  - Browse the Maven artifact at: 
https://mvnrepository.com/artifact/org/apache/camel/camel-";
+                                      + camelComponent);
 
             if (verbose) {
                 e.printStackTrace();
@@ -302,19 +306,19 @@ public class CodeSchemaGenerator extends CamelCommand {
     private JsonNode generateSchema(SchemaGenerator generator, Class<?> 
targetClass) {
         try {
             if (verbose) {
-                printer().println("Generating schema for class: " + 
targetClass.getName());
+                printer().printDiagnostic("Generating schema for class: " + 
targetClass.getName());
             }
 
             return generator.generateSchema(targetClass);
 
         } catch (Exception e) {
             printer().printErr("Error generating schema for class '" + 
targetClass.getName() + "':");
-            printer().printErr("  " + e.getMessage());
-            printer().printErr(System.lineSeparator());
-            printer().printErr("This might be caused by:");
-            printer().printErr("  - Complex class structure that cannot be 
serialized");
-            printer().printErr("  - Missing dependencies for the class");
-            printer().printErr("  - Circular references in the class 
hierarchy");
+            printer().printDiagnostic("  " + e.getMessage());
+            printer().printDiagnostic("");
+            printer().printDiagnostic("This might be caused by:");
+            printer().printDiagnostic("  - Complex class structure that cannot 
be serialized");
+            printer().printDiagnostic("  - Missing dependencies for the 
class");
+            printer().printDiagnostic("  - Circular references in the class 
hierarchy");
 
             if (verbose) {
                 e.printStackTrace();
diff --git 
a/dsl/camel-jbang/camel-jbang-plugin-generate/src/test/java/org/apache/camel/dsl/jbang/core/commands/generate/CodeSchemaGeneratorTest.java
 
b/dsl/camel-jbang/camel-jbang-plugin-generate/src/test/java/org/apache/camel/dsl/jbang/core/commands/generate/CodeSchemaGeneratorTest.java
new file mode 100644
index 000000000000..ec7b8d755724
--- /dev/null
+++ 
b/dsl/camel-jbang/camel-jbang-plugin-generate/src/test/java/org/apache/camel/dsl/jbang/core/commands/generate/CodeSchemaGeneratorTest.java
@@ -0,0 +1,90 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.camel.dsl.jbang.core.commands.generate;
+
+import org.apache.camel.dsl.jbang.core.commands.CamelJBangMain;
+import org.apache.camel.dsl.jbang.core.common.CliExitCode;
+import org.apache.camel.dsl.jbang.core.common.CommandLineTestHelper;
+import org.apache.camel.dsl.jbang.run.common.StringPrinter;
+import org.apache.camel.main.download.DependencyDownloaderClassLoader;
+import org.apache.camel.util.json.JsonObject;
+import org.apache.camel.util.json.Jsoner;
+import org.junit.jupiter.api.Test;
+
+import static org.assertj.core.api.Assertions.assertThat;
+
+/**
+ * camel generate schema prints the schema on stdout, and with --verbose what 
it does on stderr, so stdout can be
+ * redirected to a schema file (CAMEL-26933). The class comes from the test 
class path instead of downloaded component
+ * dependencies.
+ */
+class CodeSchemaGeneratorTest {
+
+    private final StringPrinter printer = new StringPrinter();
+
+    @Test
+    void verboseKeepsStdoutTheSchema() throws Exception {
+        int exit = generate("timer", Order.class.getName(), "--verbose");
+
+        assertThat(exit).as(printer.getOutput()).isZero();
+        JsonObject schema = (JsonObject) 
Jsoner.deserialize(printer.getStdout());
+        JsonObject properties = (JsonObject) schema.get("properties");
+        assertThat(properties).containsKeys("id", "amount");
+        assertThat(printer.getStderr()).contains("Generating JSON Schema for 
component: timer",
+                "Schema generation completed successfully");
+    }
+
+    @Test
+    void classNotFoundSaysSoOnce() throws Exception {
+        int exit = generate("timer", "org.acme.NoSuchClass");
+
+        assertThat(exit).isEqualTo(CliExitCode.NOT_FOUND);
+        assertThat(printer.getStdout()).isEmpty();
+        // one error line, the hints after it without an ERROR of their own
+        assertThat(printer.getStderr()).containsOnlyOnce("ERROR")
+                .contains("ERROR: Class 'org.acme.NoSuchClass' not found in 
component dependencies", "Possible causes:");
+    }
+
+    private int generate(String component, String className, String... 
options) throws Exception {
+        CodeSchemaGenerator command = new CodeSchemaGenerator(new 
CamelJBangMain().withPrinter(printer)) {
+            @Override
+            DependencyDownloaderClassLoader getDependencyDownloaderClassLoader(
+                    String camelComponent, String version, boolean verbose) {
+                return new 
DependencyDownloaderClassLoader(CodeSchemaGeneratorTest.class.getClassLoader());
+            }
+        };
+        String[] args = new String[options.length + 2];
+        args[0] = component;
+        args[1] = className;
+        System.arraycopy(options, 0, args, 2, options.length);
+        CommandLineTestHelper.parse(command, args);
+        return command.doCall();
+    }
+
+    public static class Order {
+        private String id;
+        private int amount;
+
+        public String getId() {
+            return id;
+        }
+
+        public int getAmount() {
+            return amount;
+        }
+    }
+}

Reply via email to