Skip to content

Commit 582e7b3

Browse files
Copilotclemensv
andauthored
Add structuretoxsd: JSON Structure to XSD conversion (#96)
* Initial plan * Implement structuretoxsd conversion and tests Co-authored-by: clemensv <542030+clemensv@users.noreply.github.com> * Add XSD reference files and README documentation for s2x Co-authored-by: clemensv <542030+clemensv@users.noreply.github.com> * Enable XSD validation and fix schema validity issues Co-authored-by: clemensv <542030+clemensv@users.noreply.github.com> * Add enum, const, and custom properties support to increase feature coverage Co-authored-by: clemensv <542030+clemensv@users.noreply.github.com> --------- Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: clemensv <542030+clemensv@users.noreply.github.com>
1 parent 2a09818 commit 582e7b3

14 files changed

Lines changed: 1695 additions & 0 deletions

README.md

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,10 @@ Generate code from JSON Structure:
7676
- [`avrotize s2py`](#convert-json-structure-to-python-classes) - Generate Python code from JSON Structure schema.
7777
- [`avrotize s2ts`](#convert-json-structure-to-typescript-classes) - Generate TypeScript code from JSON Structure schema.
7878

79+
Direct JSON Structure conversions:
80+
81+
- [`avrotize s2x`](#convert-json-structure-to-xml-schema-xsd) - Convert JSON Structure to XML Schema (XSD).
82+
7983
Other commands:
8084

8185
- [`avrotize pcf`](#create-the-parsing-canonical-form-pcf-of-an-avrotize-schema) - Create the Parsing Canonical Form (PCF) of an Avrotize Schema.
@@ -249,6 +253,45 @@ Conversion notes:
249253

250254
then joined with a choice.
251255

256+
### Convert JSON Structure to XML Schema (XSD)
257+
258+
```bash
259+
avrotize s2x <path_to_structure_file> [--out <path_to_xsd_schema_file>] [--namespace <target_namespace>]
260+
```
261+
262+
Parameters:
263+
264+
- `<path_to_structure_file>`: The path to the JSON Structure schema file to be converted. If omitted, the file is read from stdin.
265+
- `--out`: The path to the XML schema file to write the conversion result to. If omitted, the output is directed to stdout.
266+
- `--namespace`: (optional) Target namespace for the XSD schema.
267+
268+
Conversion notes:
269+
270+
- JSON Structure object types are mapped to XML Schema complex types with elements.
271+
- JSON Structure primitive types (string, int8-128, uint8-128, float/double, boolean, etc.) are mapped to appropriate XSD simple types.
272+
- Extended primitive types are mapped as follows:
273+
- `binary`/`bytes``xs:base64Binary`
274+
- `date``xs:date`
275+
- `time``xs:time`
276+
- `datetime`/`timestamp``xs:dateTime`
277+
- `duration``xs:duration`
278+
- `uuid``xs:string`
279+
- `uri``xs:anyURI`
280+
- `decimal``xs:decimal`
281+
- Collection types:
282+
- `array` and `set` → complex types with sequences of items
283+
- `map` → complex type with entry elements containing key and value
284+
- `tuple` → complex type with fixed sequence of typed items
285+
- Union types (`choice` or type arrays like `["string", "null"]`):
286+
- Tagged unions (with discriminator) → `xs:choice` elements
287+
- Inline unions → abstract base types with concrete extensions
288+
- Nullable types → elements with `minOccurs="0"`
289+
- Type references (`$ref`) are resolved to named XSD types
290+
- Type extensions (`$extends`) are mapped to XSD complex type extensions with `xs:complexContent`
291+
- Abstract types are marked with `abstract="true"` in XSD
292+
- Validation constraints (minLength, maxLength, pattern, minimum, maximum) are converted to XSD restrictions/facets
293+
- Required properties become elements with `minOccurs="1"`, optional properties have `minOccurs="0"`
294+
252295
### Convert ASN.1 schema to Avrotize Schema
253296

254297
```bash

avrotize/commands.json

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2658,5 +2658,48 @@
26582658
],
26592659
"suggested_output_file_path": "{input_file_name}.struct.json",
26602660
"prompts": []
2661+
},
2662+
{
2663+
"command": "s2x",
2664+
"description": "Convert JSON Structure to XSD schema",
2665+
"group": "1_Schemas",
2666+
"function": {
2667+
"name": "avrotize.structuretoxsd.convert_structure_to_xsd",
2668+
"args": {
2669+
"structure_schema_path": "input_file_path",
2670+
"xml_file_path": "output_file_path",
2671+
"target_namespace": "args.namespace"
2672+
}
2673+
},
2674+
"extensions": [".struct.json", ".json"],
2675+
"args": [
2676+
{
2677+
"name": "input",
2678+
"type": "str",
2679+
"nargs": "?",
2680+
"help": "Path to the JSON Structure file (or read from stdin if omitted)",
2681+
"required": false
2682+
},
2683+
{
2684+
"name": "--out",
2685+
"type": "str",
2686+
"help": "Path to the XSD schema file",
2687+
"required": false
2688+
},
2689+
{
2690+
"name": "--struct",
2691+
"type": "str",
2692+
"help": "Deprecated: Path to the JSON Structure file (for backcompat)",
2693+
"required": false
2694+
},
2695+
{
2696+
"name": "--namespace",
2697+
"type": "str",
2698+
"help": "Target namespace for the XSD schema",
2699+
"required": false
2700+
}
2701+
],
2702+
"suggested_output_file_path": "{input_file_name}.xsd",
2703+
"prompts": []
26612704
}
26622705
]

0 commit comments

Comments
 (0)