gnodet-bot commented on code in PR #30614:
URL: https://github.com/apache/camel/pull/30614#discussion_r4659690372


##########
components/camel-xsd-json/src/main/java/org/apache/camel/dataformat/xsdjson/XsdJsonSamples.java:
##########
@@ -0,0 +1,252 @@
+/*
+ * 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.dataformat.xsdjson;
+
+import java.io.ByteArrayOutputStream;
+import java.io.IOException;
+import java.math.BigDecimal;
+import java.nio.charset.StandardCharsets;
+import java.util.Collections;
+import java.util.IdentityHashMap;
+import java.util.LinkedHashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+
+import org.apache.camel.CamelContext;
+import org.apache.camel.Exchange;
+import org.apache.camel.dataformat.xsdjson.XsdModel.Attribute;
+import org.apache.camel.dataformat.xsdjson.XsdModel.Element;
+import org.apache.camel.dataformat.xsdjson.XsdModel.Group;
+import org.apache.camel.dataformat.xsdjson.XsdModel.SimpleType;
+import org.apache.camel.dataformat.xsdjson.XsdModel.Type;
+import org.apache.camel.support.DefaultExchange;
+import org.apache.camel.util.ObjectHelper;
+
+/**
+ * Sample documents for the global elements of an XML Schema (an .xsd file, or 
the types of a .wsdl file), such as the
+ * request and response of a SOAP operation. Every element and attribute is 
filled in with a value of its type: the
+ * first value of an enumeration, a date for xs:date, a number for xs:int, and 
so on, and a repeating element has one
+ * item. Of an xs:choice, only the first member is filled in, and an element 
whose type contains itself is filled in
+ * once. A value is not made to match the other facets of its type, such as a 
pattern or a length.
+ * <p>
+ * The JSON is the form that the xsdJson data format reads and writes, without 
the root element (the default of
+ * includeRoot), and the XML is what the data format writes from it, so a 
sample can be used as it is in a route.
+ *
+ * @since 4.24
+ */
+public final class XsdJsonSamples {

Review Comment:
   💡 `XsdJsonSamples` starts an `XsdJsonDataFormat` in its constructor but 
provides no `close()` method, leaving callers with no cleanup path. Even if 
`doStop()` is currently a no-op, the public API should follow the 
resource-cleanup contract:
   
   ```suggestion
   public final class XsdJsonSamples implements AutoCloseable {
   ```
   
   And add a `close()` method:
   ```java
   @Override
   public void close() {
       dataFormat.stop();
   }
   ```



##########
components/camel-xsd-json/src/main/java/org/apache/camel/dataformat/xsdjson/XsdJsonSamples.java:
##########
@@ -0,0 +1,252 @@
+/*
+ * 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.dataformat.xsdjson;
+
+import java.io.ByteArrayOutputStream;
+import java.io.IOException;
+import java.math.BigDecimal;
+import java.nio.charset.StandardCharsets;
+import java.util.Collections;
+import java.util.IdentityHashMap;
+import java.util.LinkedHashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+
+import org.apache.camel.CamelContext;
+import org.apache.camel.Exchange;
+import org.apache.camel.dataformat.xsdjson.XsdModel.Attribute;
+import org.apache.camel.dataformat.xsdjson.XsdModel.Element;
+import org.apache.camel.dataformat.xsdjson.XsdModel.Group;
+import org.apache.camel.dataformat.xsdjson.XsdModel.SimpleType;
+import org.apache.camel.dataformat.xsdjson.XsdModel.Type;
+import org.apache.camel.support.DefaultExchange;
+import org.apache.camel.util.ObjectHelper;
+
+/**
+ * Sample documents for the global elements of an XML Schema (an .xsd file, or 
the types of a .wsdl file), such as the
+ * request and response of a SOAP operation. Every element and attribute is 
filled in with a value of its type: the
+ * first value of an enumeration, a date for xs:date, a number for xs:int, and 
so on, and a repeating element has one
+ * item. Of an xs:choice, only the first member is filled in, and an element 
whose type contains itself is filled in
+ * once. A value is not made to match the other facets of its type, such as a 
pattern or a length.
+ * <p>
+ * The JSON is the form that the xsdJson data format reads and writes, without 
the root element (the default of
+ * includeRoot), and the XML is what the data format writes from it, so a 
sample can be used as it is in a route.
+ *
+ * @since 4.24
+ */
+public final class XsdJsonSamples {
+
+    private static final int MAX_DEPTH = 32;
+
+    /**
+     * Reads a document: the schema, or a schema or WSDL that it imports or 
includes.
+     */
+    @FunctionalInterface
+    public interface DocumentReader {
+
+        /**
+         * @param  uri         the uri of the document, such as 
file:orders.wsdl or https://example.com/orders.xsd
+         * @return             the content of the document
+         * @throws IOException if the document cannot be read, or must not be
+         */
+        byte[] read(String uri) throws IOException;
+    }
+
+    private final XsdJsonDataFormat dataFormat;
+    private final CamelContext camelContext;
+
+    /**
+     * Loads the schema as the xsdJson data format does.
+     *
+     * @param camelContext the CamelContext, which loads the schema
+     * @param schema       the schema, such as file:orders.wsdl or 
classpath:orders.xsd
+     */
+    public XsdJsonSamples(CamelContext camelContext, String schema) {
+        this(camelContext, schema, null);
+    }
+
+    /**
+     * Loads the schema as the xsdJson data format does, with the documents 
read by the reader, such as to only read
+     * from the host of a WSDL. The imports and includes still keep to the 
scheme of the schema.
+     *
+     * @param camelContext the CamelContext
+     * @param schema       the schema, such as file:orders.wsdl or 
https://example.com/orders?wsdl
+     * @param reader       reads the schema and the documents it imports or 
includes (null to use the CamelContext)
+     */
+    public XsdJsonSamples(CamelContext camelContext, String schema, 
DocumentReader reader) {
+        this.camelContext = camelContext;
+        this.dataFormat = new XsdJsonDataFormat();
+        dataFormat.setCamelContext(camelContext);
+        dataFormat.setSchema(schema);
+        dataFormat.setDocumentReader(reader);
+        dataFormat.setPrettyPrint(true);
+        dataFormat.start();

Review Comment:
   If the schema cannot be loaded (bad classpath reference, parse error, 
unresolvable import), `dataFormat.start()` throws a `RuntimeCamelException`. 
This failure mode is not documented anywhere in the constructor Javadoc. 
Consider adding:
   
   ```
   @throws RuntimeCamelException if the schema cannot be loaded or parsed
   ```



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to