Skip to content

Commit 83702e9

Browse files
davsclausclaude
andauthored
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 <noreply@anthropic.com>
1 parent 8417419 commit 83702e9

3 files changed

Lines changed: 131 additions & 35 deletions

File tree

‎docs/user-manual/modules/ROOT/pages/camel-jbang-projects.adoc‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -426,10 +426,12 @@ Generate JSON Schema definitions from Java objects for use with visual developme
426426

427427
[source,bash]
428428
----
429-
camel generate schema fhir org.hl7.fhir.r4.model.Patient
430-
Schema saved to: ./org.hl7.fhir.r4.model.Patient-schema.json
429+
camel generate schema fhir org.hl7.fhir.r4.model.Patient > patient-schema.json
431430
----
432431

432+
The schema is printed on stdout; `--output=patient-schema.json` writes it to a file instead. `--verbose` tells on
433+
stderr what the command does (the dependencies it downloads, the class it loads), so stdout stays the schema.
434+
433435
== Version management
434436

435437
Show the current Camel CLI version:

‎dsl/camel-jbang/camel-jbang-plugin-generate/src/main/java/org/apache/camel/dsl/jbang/core/commands/generate/CodeSchemaGenerator.java‎

Lines changed: 37 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -47,14 +47,17 @@
4747
* This command downloads the specified Camel component dependencies and generates a JSON Schema (Draft 2020-12) for the
4848
* given fully qualified class name.
4949
*
50-
* Usage Examples: - camel generate schema fhir org.hl7.fhir.r4.model.Patient - camel generate schema http
51-
* org.apache.camel.component.http.HttpConfiguration
50+
* Usage Examples: - camel generate schema fhir org.hl7.fhir.r4.model.Patient - camel generate schema timer
51+
* org.apache.camel.component.timer.TimerComponent
5252
*
5353
* The generated schema includes: - Public and non-public fields (with getters) - Flattened enums using toString() -
5454
* Jackson annotations support
5555
*
56-
* Exit Codes: - 0: Success - 1: Component not found or dependency download failed - 2: Class not found - 3: Schema
57-
* generation failed - 4: General error
56+
* The schema goes to stdout, or to the file of --output; --verbose tells on stderr what the command does.
57+
*
58+
* Exit Codes: - 0: Success - 1: the dependencies cannot be downloaded, or the schema cannot be generated - 2: a
59+
* component or class name that is empty - 3: the class is not in the dependencies of the component - 70: an unexpected
60+
* error
5861
*/
5962
@CommandDefinition(name = "schema", description = "Create a JSON schema for a given Camel component and Java Object",
6063
generateHelp = true, sortOptions = true)
@@ -90,9 +93,9 @@ public Integer doCall() throws Exception {
9093
}
9194

9295
if (verbose) {
93-
printer().println("Generating JSON Schema for component: " + camelComponent);
94-
printer().println("Class: " + fullyQualifiedName);
95-
printer().println("Camel version: " + camelVersion);
96+
printer().printDiagnostic("Generating JSON Schema for component: " + camelComponent);
97+
printer().printDiagnostic("Class: " + fullyQualifiedName);
98+
printer().printDiagnostic("Camel version: " + camelVersion);
9699
}
97100

98101
// Validate inputs
@@ -137,7 +140,7 @@ public Integer doCall() throws Exception {
137140
outputSchema(schema);
138141

139142
if (verbose) {
140-
printer().println("Schema generation completed successfully");
143+
printer().printDiagnostic("Schema generation completed successfully");
141144
}
142145

143146
return 0;
@@ -181,12 +184,12 @@ private SchemaGeneratorConfig createSchemaGeneratorConfig(JacksonSchemaModule ja
181184
* @param verbose Whether to enable verbose logging
182185
* @return DependencyDownloaderClassLoader or null if failed
183186
*/
184-
private DependencyDownloaderClassLoader getDependencyDownloaderClassLoader(
187+
DependencyDownloaderClassLoader getDependencyDownloaderClassLoader(
185188
String camelComponent, String version, boolean verbose) {
186189

187190
try {
188191
if (verbose) {
189-
printer().println("Creating dependency downloader class loader...");
192+
printer().printDiagnostic("Creating dependency downloader class loader...");
190193
}
191194

192195
DependencyDownloaderClassLoader cl = new DependencyDownloaderClassLoader(
@@ -199,7 +202,7 @@ private DependencyDownloaderClassLoader getDependencyDownloaderClassLoader(
199202
downloader.start();
200203

201204
if (verbose) {
202-
printer().println("Downloading artifacts for camel-" + camelComponent + ":" + version);
205+
printer().printDiagnostic("Downloading artifacts for camel-" + camelComponent + ":" + version);
203206
}
204207

205208
List<MavenArtifact> artifacts = downloader.downloadArtifacts(
@@ -211,15 +214,15 @@ private DependencyDownloaderClassLoader getDependencyDownloaderClassLoader(
211214
if (artifacts == null || artifacts.isEmpty()) {
212215
printer().printErr(
213216
"Error: No artifacts found for component 'camel-" + camelComponent + "' version '" + version + "'");
214-
printer().printErr("Please verify that the component name is correct and the version exists.");
215-
printer().printErr("Available components can be found at: https://camel.apache.org/components/");
217+
printer().printDiagnostic("Please verify that the component name is correct and the version exists.");
218+
printer().printDiagnostic("Available components can be found at: https://camel.apache.org/components/");
216219
IOHelper.close(cl);
217220
return null;
218221
}
219222

220223
if (verbose) {
221-
printer().println("Downloaded " + artifacts.size() + " artifact(s)");
222-
artifacts.forEach(artifact -> printer().println(" - " + artifact.getFile().getName()));
224+
printer().printDiagnostic("Downloaded " + artifacts.size() + " artifact(s)");
225+
artifacts.forEach(artifact -> printer().printDiagnostic(" - " + artifact.getFile().getName()));
223226
}
224227

225228
artifacts.forEach(artifact -> cl.addFile(artifact.getFile()));
@@ -259,24 +262,25 @@ private DependencyDownloaderClassLoader getDependencyDownloaderClassLoader(
259262
private Class<?> loadTargetClass(ClassLoader classLoader, String className) {
260263
try {
261264
if (verbose) {
262-
printer().println("Loading class: " + className);
265+
printer().printDiagnostic("Loading class: " + className);
263266
}
264267

265268
return classLoader.loadClass(className);
266269

267270
} catch (ClassNotFoundException e) {
268271

269272
printer().printErr("Class '" + className + "' not found in component dependencies");
270-
printer().printErr(System.lineSeparator());
271-
printer().printErr("Possible causes:");
272-
printer().printErr(" - Class name is incorrect (check spelling and package)");
273-
printer().printErr(" - Class is not part of the '" + camelComponent + "' component");
274-
printer().printErr(" - Class is in a different artifact not included as dependency");
275-
printer().printErr(System.lineSeparator());
276-
printer().printErr("To find available classes, you can:");
277-
printer().printErr(" - Check the component documentation");
278-
printer().printErr(" - Browse the Maven artifact at: https://mvnrepository.com/artifact/org/apache/camel/camel-"
279-
+ camelComponent);
273+
printer().printDiagnostic("");
274+
printer().printDiagnostic("Possible causes:");
275+
printer().printDiagnostic(" - Class name is incorrect (check spelling and package)");
276+
printer().printDiagnostic(" - Class is not part of the '" + camelComponent + "' component");
277+
printer().printDiagnostic(" - Class is in a different artifact not included as dependency");
278+
printer().printDiagnostic("");
279+
printer().printDiagnostic("To find available classes, you can:");
280+
printer().printDiagnostic(" - Check the component documentation");
281+
printer().printDiagnostic(
282+
" - Browse the Maven artifact at: https://mvnrepository.com/artifact/org/apache/camel/camel-"
283+
+ camelComponent);
280284

281285
if (verbose) {
282286
e.printStackTrace();
@@ -302,19 +306,19 @@ private Class<?> loadTargetClass(ClassLoader classLoader, String className) {
302306
private JsonNode generateSchema(SchemaGenerator generator, Class<?> targetClass) {
303307
try {
304308
if (verbose) {
305-
printer().println("Generating schema for class: " + targetClass.getName());
309+
printer().printDiagnostic("Generating schema for class: " + targetClass.getName());
306310
}
307311

308312
return generator.generateSchema(targetClass);
309313

310314
} catch (Exception e) {
311315
printer().printErr("Error generating schema for class '" + targetClass.getName() + "':");
312-
printer().printErr(" " + e.getMessage());
313-
printer().printErr(System.lineSeparator());
314-
printer().printErr("This might be caused by:");
315-
printer().printErr(" - Complex class structure that cannot be serialized");
316-
printer().printErr(" - Missing dependencies for the class");
317-
printer().printErr(" - Circular references in the class hierarchy");
316+
printer().printDiagnostic(" " + e.getMessage());
317+
printer().printDiagnostic("");
318+
printer().printDiagnostic("This might be caused by:");
319+
printer().printDiagnostic(" - Complex class structure that cannot be serialized");
320+
printer().printDiagnostic(" - Missing dependencies for the class");
321+
printer().printDiagnostic(" - Circular references in the class hierarchy");
318322

319323
if (verbose) {
320324
e.printStackTrace();
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
package org.apache.camel.dsl.jbang.core.commands.generate;
18+
19+
import org.apache.camel.dsl.jbang.core.commands.CamelJBangMain;
20+
import org.apache.camel.dsl.jbang.core.common.CliExitCode;
21+
import org.apache.camel.dsl.jbang.core.common.CommandLineTestHelper;
22+
import org.apache.camel.dsl.jbang.run.common.StringPrinter;
23+
import org.apache.camel.main.download.DependencyDownloaderClassLoader;
24+
import org.apache.camel.util.json.JsonObject;
25+
import org.apache.camel.util.json.Jsoner;
26+
import org.junit.jupiter.api.Test;
27+
28+
import static org.assertj.core.api.Assertions.assertThat;
29+
30+
/**
31+
* camel generate schema prints the schema on stdout, and with --verbose what it does on stderr, so stdout can be
32+
* redirected to a schema file (CAMEL-26933). The class comes from the test class path instead of downloaded component
33+
* dependencies.
34+
*/
35+
class CodeSchemaGeneratorTest {
36+
37+
private final StringPrinter printer = new StringPrinter();
38+
39+
@Test
40+
void verboseKeepsStdoutTheSchema() throws Exception {
41+
int exit = generate("timer", Order.class.getName(), "--verbose");
42+
43+
assertThat(exit).as(printer.getOutput()).isZero();
44+
JsonObject schema = (JsonObject) Jsoner.deserialize(printer.getStdout());
45+
JsonObject properties = (JsonObject) schema.get("properties");
46+
assertThat(properties).containsKeys("id", "amount");
47+
assertThat(printer.getStderr()).contains("Generating JSON Schema for component: timer",
48+
"Schema generation completed successfully");
49+
}
50+
51+
@Test
52+
void classNotFoundSaysSoOnce() throws Exception {
53+
int exit = generate("timer", "org.acme.NoSuchClass");
54+
55+
assertThat(exit).isEqualTo(CliExitCode.NOT_FOUND);
56+
assertThat(printer.getStdout()).isEmpty();
57+
// one error line, the hints after it without an ERROR of their own
58+
assertThat(printer.getStderr()).containsOnlyOnce("ERROR")
59+
.contains("ERROR: Class 'org.acme.NoSuchClass' not found in component dependencies", "Possible causes:");
60+
}
61+
62+
private int generate(String component, String className, String... options) throws Exception {
63+
CodeSchemaGenerator command = new CodeSchemaGenerator(new CamelJBangMain().withPrinter(printer)) {
64+
@Override
65+
DependencyDownloaderClassLoader getDependencyDownloaderClassLoader(
66+
String camelComponent, String version, boolean verbose) {
67+
return new DependencyDownloaderClassLoader(CodeSchemaGeneratorTest.class.getClassLoader());
68+
}
69+
};
70+
String[] args = new String[options.length + 2];
71+
args[0] = component;
72+
args[1] = className;
73+
System.arraycopy(options, 0, args, 2, options.length);
74+
CommandLineTestHelper.parse(command, args);
75+
return command.doCall();
76+
}
77+
78+
public static class Order {
79+
private String id;
80+
private int amount;
81+
82+
public String getId() {
83+
return id;
84+
}
85+
86+
public int getAmount() {
87+
return amount;
88+
}
89+
}
90+
}

0 commit comments

Comments
 (0)