Skip to content

Commit 28634bd

Browse files
authored
feat: add xsd support (#2350)
1 parent 8b68cba commit 28634bd

File tree

31 files changed

+4336
-30
lines changed

31 files changed

+4336
-30
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,6 @@ java_runtime_node
1212
.netlify
1313
.metals
1414
.scala-build
15+
16+
.cursorrules
17+
.cursor

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,10 @@ The following table provides a short summary of available features for supported
204204
<td><a href="./docs/usage.md#generate-models-from-openapi-documents">OpenAPI</a></td>
205205
<td>We support the following OpenAPI versions: <em><a href="./docs/usage.md#generate-models-from-swagger-20-documents">Swagger 2.0</a>, <a href="./docs/usage.md#generate-models-from-openapi-documents">OpenAPI 3.0 and 3.1</a></em>, which generates models for all the defined request and response payloads.</td>
206206
</tr>
207+
<tr>
208+
<td><a href="./examples/xsd-to-typescript">XSD (XML Schema Definition)</a></td>
209+
<td>We support XML Schema Definition (XSD) with common elements including simple types, complex types, sequences, choices, attributes, and enumerations.</td>
210+
</tr>
207211
<tr>
208212
<td><a href="./docs/usage.md#generate-model-from-typescript-type-files">TypeScript</a></td>
209213
<td>We currently support TypeScript types as file input for model generation</td>

docs/inputs/XSD.md

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
# Interpretation of XSD to MetaModel
2+
3+
The library transforms XSD (XML Schema Definition) from XML data validation schemas to data model representations ([`MetaModel`](../internal-model.md)s).
4+
5+
The algorithm processes the XSD document structure to create models that represent the data types defined in the schema, including complex types, simple types, elements, attributes, and their relationships.
6+
7+
## Overview
8+
9+
XSD input processing follows this flow:
10+
1. **XML Parsing**: The XSD string is parsed into a JavaScript object using `fast-xml-parser`
11+
2. **XSD Schema Model**: The parsed XML is converted to a structured `XsdSchema` model
12+
3. **MetaModel Conversion**: The XSD types are mapped to Modelina's internal `MetaModel` representation
13+
4. **Model Generation**: The MetaModels are processed by generators to create output code
14+
15+
## Supported XSD Features
16+
17+
### Simple Types
18+
- **Enumerations**: `xs:restriction` with `xs:enumeration` values → enum models
19+
- **Restrictions**: Patterns, length constraints, and numeric ranges (constraint information preserved in original input)
20+
21+
### Complex Types
22+
- **Sequences**: `xs:sequence` elements → object properties in defined order
23+
- **Choices**: `xs:choice` elements → optional object properties
24+
- **Attributes**: Converted to object properties alongside elements
25+
- `use="required"` → required properties
26+
- `use="optional"` → optional properties
27+
28+
### Advanced Features
29+
- **Complex Content (Inheritance)**: `xs:complexContent` with `xs:extension`
30+
- Currently: Properties from base type are merged into extending type
31+
- Note: Full inheritance support may be added in future versions
32+
- **Simple Content**: Text content plus attributes → object with `value` property
33+
- **Arrays**: `maxOccurs="unbounded"` or `maxOccurs` > 1 → array properties
34+
- **Optional Elements**: `minOccurs="0"` → non-required properties
35+
- **Wildcards**: `xs:any` elements → mapped to `any` type
36+
- Supports `minOccurs` and `maxOccurs` for cardinality
37+
- Generated as properties with names like `additionalProperty`, `additionalProperty1`, etc.
38+
39+
## Type Mappings
40+
41+
The following table shows how XSD built-in types are mapped to Modelina's internal types:
42+
43+
| XSD Type | Mapped Type | Notes |
44+
|----------|-------------|-------|
45+
| `xs:string`, `xs:token`, `xs:normalizedString` | string | General text types |
46+
| `xs:int`, `xs:integer`, `xs:long`, `xs:short`, `xs:byte` | integer | Integer numeric types |
47+
| `xs:unsignedLong`, `xs:unsignedInt`, `xs:unsignedShort`, `xs:unsignedByte` | integer | Unsigned integer types |
48+
| `xs:positiveInteger`, `xs:negativeInteger`, `xs:nonPositiveInteger`, `xs:nonNegativeInteger` | integer | Constrained integer types |
49+
| `xs:float`, `xs:double`, `xs:decimal` | float | Floating-point numeric types |
50+
| `xs:boolean` | boolean | Boolean type |
51+
| `xs:date`, `xs:time`, `xs:dateTime`, `xs:duration` | string | Date/time types (represented as strings) |
52+
| `xs:gYear`, `xs:gMonth`, `xs:gDay` | string | Partial date types |
53+
| `xs:anyURI`, `xs:QName` | string | URI and qualified name types |
54+
| `xs:base64Binary`, `xs:hexBinary` | string | Binary data types (represented as strings) |
55+
| Complex types | object | Converted to object models with properties |
56+
| Simple types with enumerations | enum | Converted to enum models |
57+
58+
## Namespace Handling
59+
60+
XSD namespaces are handled in a simplified manner:
61+
- `targetNamespace` is extracted but not actively used
62+
- Both `xs:` and `xsd:` prefixes are supported
63+
- Custom namespace prefixes (e.g., `tns:BookType`) are stripped and resolved by name only
64+
65+
## Limitations
66+
67+
### Partial Support
68+
- **Element References**: `ref` attribute has basic support
69+
- **Union/List**: `xs:union` and `xs:list` have basic implementations
70+
- **Inheritance**: Properties are merged rather than creating true inheritance
71+
72+
### Not Supported
73+
- `xs:group` and `xs:attributeGroup` (named groups)
74+
- `xs:unique`, `xs:key`, `xs:keyref` (constraints)
75+
- `xs:notation` and `xs:redefine`
76+
- Full `xs:restriction` on complex types
77+
- `substitutionGroup` (element substitution)
78+
- Full namespace handling and validation
79+
- Constraint enforcement in generated models (patterns, lengths, ranges)
80+
81+
## Examples
82+
83+
See the [XSD to TypeScript example](../../examples/xsd-to-typescript/) for a complete working demonstration of XSD input processing.
84+
85+
For general usage information, see the [usage documentation](../usage.md#generate-models-from-xsd-documents).
86+

docs/integration.md

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,9 +53,6 @@ Do NOT enable users to write their own option callbacks. This includes but not l
5353

5454
To be on the safeside, only enable the user to chose between the internal options and presets, as you can see the [playground does](https://www.asyncapi.com/tools/modelina).
5555

56-
## Integrate Modelina in an AsyncAPI generator template
57-
TODO
58-
5956
## Integrate Modelina into Maven
6057

6158
There are at least two ways you can integrate Modelina into your build process for Maven projects, either with the AsyncAPI CLI or with a custom build script. Which one to choose all depends on your scenario, look below:

docs/languages/Rust.md

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -39,19 +39,3 @@ To generate a `new` method, use the preset `RUST_COMMON_PRESET` and provide the
3939
## Implement `Default` for enums
4040

4141
To generate `Default` implementation for enums that provide a default value, use the preset `RUST_COMMON_PRESET` and provide the option `implementDefault: true`.
42-
43-
## Implement `From<String>` (serde_json)
44-
45-
TODO
46-
47-
## Implement `Into<String>` (serde_json)
48-
49-
TODO
50-
51-
## Implement `From<FramedByteStream>` (tokio_serde)
52-
53-
TODO
54-
55-
## Implement `From<FramedByteStream>` (tokio_serde)
56-
57-
TOOD

docs/usage.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ For more specific integration options, please check out the [integration documen
2424
+ [Limitations and Compatibility](#limitations-and-compatibility-2)
2525
- [Polymorphism](#polymorphism-2)
2626
- [Generate models from TypeScript type files](#generate-models-from-typescript-type-files)
27+
- [Generate models from XSD documents](#generate-models-from-xsd-documents)
2728
- [Generate models from Meta models](#generate-models-from-meta-models)
2829
- [Generate Go models](#generate-go-models)
2930
- [Generate C# models](#generate-c%23-models)
@@ -166,6 +167,16 @@ Currently, we support generating models from a TypeScript type file.
166167

167168
The TypeScript input processor expects that the typescript file and base directory where it's present, is passed as input, in order to process the types accurately. The input processor converts the TypeScript types into JSON Schema, which are then passed on to the [JSON Schema processor](#generate-models-from-json-schema-documents).
168169

170+
## Generate models from XSD documents
171+
172+
XSD (XML Schema Definition) is fully supported as an input format. This enables Modelina to work with XML-based schemas commonly used in enterprise and legacy systems.
173+
174+
- [Generate TypeScript models from XSD](../examples/xsd-to-typescript/)
175+
176+
The XSD input processor automatically detects XSD schemas by looking for schema indicators (`xs:schema` or `xsd:schema`).
177+
178+
For a detailed explanation of how XSD is interpreted, see the [XSD input processing documentation](./inputs/XSD.md).
179+
169180
## Generate models from Meta models
170181
Sometimes, the supported inputs such as AsyncAPI and JSON Schema wont be enough for your use-case and you want to create your own data models while still utilizing the full sweep of features from the generators.
171182

examples/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ These examples show a specific input and how they can be used:
5353
- [openapi-from-object](./openapi-from-object) - A basic example where an OpenAPI v3.0 JS object is used to generate models.
5454
- [openapi-include-components](./openapi-include-components) - A basic example where an OpenAPI document without paths is used to generate models by iterating the components.
5555
- [openapi-v3_1-from-object](./openapi-v3_1-from-object) - A basic example where an OpenAPI v3.1 JS object is used to generate models.
56+
- [xsd-to-typescript](./xsd-to-typescript) - A basic example showing how to generate TypeScript models from XSD (XML Schema Definition).
5657
- [meta-model](./meta-model) - A basic example how to provide a meta model for the generator
5758

5859
## General examples

examples/csharp-generate-records/README.md

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
1-
# TODO: Your example title
2-
3-
TODO: Your example description
1+
# C# generate records
2+
Simple example showing how to generate C# records
43

54
## How to run this example
65

examples/java-from-typescript-type-with-options/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# TODO: Generate Java model from a TypeScript Types file using processor options
1+
# Generate Java model from a TypeScript Types file using processor options
22

33
This is a basic example of generating Java models using a Typescript type file as input and processor options. This can be extended to generating Go, C# and other supported language models.
44

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# Generate TypeScript models from XSD
2+
3+
This example shows you how to generate TypeScript models from an XSD (XML Schema Definition) file.
4+
5+
## How to run this example
6+
7+
Run this example using:
8+
9+
```sh
10+
npm i && npm run start
11+
```
12+
13+
If you are on Windows, use the `start:windows` script instead:
14+
15+
```sh
16+
npm i && npm run start:windows
17+
```

0 commit comments

Comments
 (0)