davsclaus opened a new pull request, #30597:
URL: https://github.com/apache/camel/pull/30597

   [CAMEL-27375](https://issues.apache.org/jira/browse/CAMEL-27375): XML to 
JSON and back, driven by an XML Schema (an XSD, or the types of a WSDL), for 
SOAP payloads in low-code routes that have no generated classes.
   
   ## Problem
   
   camel-xj maps XML to JSON without a schema, so information is lost. Every 
value becomes a string. One `<item>` gives an object, and two give an array. 
Going back to XML, the JSON has to list its fields in schema order and the 
route has to know the namespace. The soap-source/soap-action Kamelets prototype 
worked around this with a 2.5 KB XSLT and jq fixes, and those only work for one 
namespace with `elementFormDefault="qualified"`.
   
   ## Design (phase 1, as approved)
   
   - A new optional module `components/camel-xsd-json` with the data format 
`xsdJson`. It is not an option on camel-xj, which needs Saxon, or on 
camel-jacksonxml, which has no XSD model and would tie this to Jackson 2. Its 
only third-party dependency is Apache XmlSchema `xmlschema-core` 2.3.3, about 
190 KB with no dependencies. CXF 4.2.4 uses the same version, set by the new 
parent property `xmlschema-core-version`.
   - The XML is the wire format, as with jacksonXml and protobuf. `unmarshal` 
reads XML into JSON text. `marshal` writes XML from JSON: text in a String, 
bytes, a stream or a file, or a `Map`. JSON is parsed with `camel-util-json` 
(no Jackson), and XML is read and written with JDK StAX. The schema is compiled 
once at start into a model of elements and types.
   - Options:
     - `schema` (required): an `.xsd` or a `.wsdl`. For a WSDL, every 
`wsdl:types/xs:schema` is read, with the namespace declarations it inherits 
from `wsdl:definitions`.
     - `element`: the root element, as `{ns}name` or as a unique local name.
     - `includeRoot`, `attributePrefix`, `textName` (default `value`), 
`ignoreUnknown` and `prettyPrint`.
   - The header `CamelXsdJsonElement` overrides `element` per message, and may 
only name a global element of the schema.
   - Mapping:
     - XSD numeric types become JSON numbers, and `xs:decimal` keeps its exact 
value and scale. `xs:boolean` becomes true/false, and `1`/`0` are accepted.
     - `xs:list` becomes an array. Every other simple type is a string. A 
restriction maps as its base type.
     - `maxOccurs > 1`, or an element in a repeating group, is always an array, 
even with one item.
     - `marshal` writes children in `xs:sequence` order whatever the JSON 
order. `xs:all` keeps the JSON order. `xs:choice` writes the branch that is 
present, and fails when the JSON has more than one.
     - Namespaces follow `elementFormDefault`/`attributeFormDefault`/`form` 
across imports. Attributes become fields. Simple content with attributes 
becomes `{"currency":"EUR","value":9.95}`.
     - `xsi:nil` becomes `null`. On marshal, `null` writes `xsi:nil` when the 
element is nillable, is left out when the element is optional, and fails 
otherwise.
   - Not in phase 1: `xs:any` content, anyType, mixed content, substitution 
groups, abstract types, and `xsi:type` naming another type. Each fails with a 
message that names the construct.
   
   Answers to the open questions, as decided: the module is `camel-xsd-json` 
and the data format is `xsdJson`. There is no data type transformer 
(follow-up). The per-message element is a header. There is no `validate` option 
and no WSDL-operation selection.
   
   One refinement of the design: an unsupported construct, or a naming conflict 
(two children with one local name, or an attribute and an element with the same 
JSON name), does not fail the whole schema load. It is recorded on the type and 
fails when a message reaches it, so a large WSDL stays usable for its other 
elements. When the `element` option is set, a naming conflict in that element's 
types still fails at start. `xs:any` makes a type fail only when wildcard 
content actually occurs, so extensible types with an optional `xs:any` still 
work.
   
   ## Security
   
   Following the data format guidance of the security model:
   - The schema, the WSDL and every document they import or include are parsed 
with DOCTYPE refused, external entities and XInclude off, and secure processing 
on.
   - `xs:import`/`xs:include` resolve relative to the document that has them, 
and are only followed to resources with the same scheme as the `schema` option. 
A `classpath:` or `https:` schema cannot pull in `file:`.
   - Message XML is read with StAX with DTD support and external entities off, 
and a document with a DOCTYPE is refused.
   - Nothing from the message is evaluated. Nesting is limited to 512 levels 
(the JDK parser may have a lower limit). The header can only pick a global 
element of the schema.
   
   ## Wiring
   
   - Model `XsdJsonDataFormat` with registration in `DataFormatsDefinition`, 
marshal/unmarshal and `DataFormatTransformerDefinition`.
   - `DataFormatClause.xsdJson(...)` and `DataFormatBuilderFactory.xsdJson()`, 
and the reifier.
   - Regenerated xml-io/yaml-io/java-io readers and writers, the YAML DSL 
deserializers and schemas, and the Spring XML / xml-io XSDs.
   - The catalog (dataformats and models JSON, the lists, the doc), 
`ArtifactUtils`, camel-main `dataformats.properties`, parent/bom/allcomponents 
poms, the docs nav and symlinks.
   - The doc `xsdJson-dataformat.adoc` has the mapping table, its limits and 
the security notes.
   
   ## Tests
   
   `mvn -o test` in `components/camel-xsd-json` (with the changed core modules 
and camel-yaml-dsl installed into a private scratch repository): **37 tests, 0 
failures**.
   - `XsdJsonDataFormatTest` (19):
     - the types, a one-item array, attributes, simple content, `xs:list`, an 
exact decimal, `xsi:nil`, and the round trip
     - schema order from shuffled JSON, the namespaces of an imported schema 
with unqualified children, extension
     - choice (one branch, and two failing), `xs:all` order, a repeating 
sequence, a recursive type
     - `attributePrefix`/`textName`, `includeRoot`, `prettyPrint`, the header 
element (marshal, the unmarshal restriction, an unknown name)
     - the errors for unknown fields, elements and attributes, and 
`ignoreUnknown`
     - a wrong namespace, invalid numbers, an array for a single element, a 
body that is not JSON
   - `XsdJsonWsdlTest` (4): the prototype `orders.wsdl` with 1 and 2 items, the 
request in schema order with the WSDL's `tns` prefix, the fault element through 
the header, and two inline schemas importing each other by namespace only.
   - `XsdJsonUnsupportedTest` (6): mixed content, `xs:any` (fine until wildcard 
content occurs, then a clear error), anyType, a substitution group and an 
abstract element, an abstract type and `xsi:type`, and naming conflicts 
(failing at start, solved by `attributePrefix`).
   - `XsdJsonSecurityTest` (6): an XXE message, a DOCTYPE message, a schema 
with DOCTYPE, an import with another scheme, relative import resolution with 
`..`, and the depth limit on unmarshal and marshal.
   - `XsdJsonRouteTest` (2): Java DSL and YAML DSL routes.
   
   Not done: the camel-cxf PAYLOAD test mentioned in the design, because it 
would bring the CXF and Jetty stack into this module's tests (the WSDL test 
covers the payloads themselves). The camel-kamelets follow-up, which moves 
soap-source/soap-action JSON mode to `xsdJson` and drops the XSLT, is separate.
   
   _Claude Code on behalf of davsclaus_
   
   🤖 Generated with [Claude Code](https://claude.com/claude-code)
   


-- 
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