Search code examples
liquid-xml

Missing some required field in JSON Schema from XSD to JSON Schema Converter


When an XSD element is set to ‘minOccurs >= 1’ or the default value, the XSD to JSON Schema Converter incorrectly converts the element to an optional field in the output JSON schema.

Below example shows TestDocument.Name and TestDocument.Address.Line are missing in required property.

XML Schema:

<?xml version="1.0" encoding="utf-8" ?>
<!--Created with Liquid Studio (https://www.liquid-technologies.com)-->
<xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">
    <xs:element id="TD0001" name="TestDocument">
        <xs:complexType>
            <xs:sequence>
                <xs:element id="TE0003" name="Address">
                    <xs:complexType>
                        <xs:sequence>
                            <xs:element name="CityName" type="xs:string" minOccurs="0" />
                            <xs:element name="Line" type="xs:string" maxOccurs="2" />
                        </xs:sequence>
                    </xs:complexType>
                </xs:element>
                <xs:element id="TE0002" name="Name" type="xs:string" maxOccurs="2" />
            </xs:sequence>
        </xs:complexType>
    </xs:element>
</xs:schema>

The output JSON Schema from Liquid Studio:

{
    "$schema": "https://json-schema.org/draft/2020-12/schema",
    "type": "object",
    "properties": {
        "TestDocument": {
            "$ref": "#/$defs/TestDocument"
        }
    },
    "additionalProperties": false,
    "required": [
        "TestDocument"
    ],
    "$defs": {
        "TestDocument": {
            "type": "object",
            "properties": {
                "Address": {
                    "type": "object",
                    "properties": {
                        "CityName": {
                            "type": "string"
                        },
                        "Line": {
                            "type": "array",
                            "maxItems": 2,
                            "minItems": 1,
                            "items": {
                                "type": "string"
                            }
                        }
                    },
                    "additionalProperties": false
                },
                "Name": {
                    "type": "array",
                    "maxItems": 2,
                    "minItems": 1,
                    "items": {
                        "type": "string"
                    }
                }
            },
            "additionalProperties": false,
            "required": [
                "Address"
            ]
        }
    }
}

Solution

  • This issue has now been fixed and is included in Liquid Studio v20.7.14.

    The JSON Schema generated from your XSD is now:

    {
        "$schema": "https://json-schema.org/draft/2020-12/schema",
        "type": "object",
        "properties": {
            "TestDocument": {
                "$ref": "#/$defs/TestDocument"
            }
        },
        "additionalProperties": false,
        "required": [
            "TestDocument"
        ],
        "$defs": {
            "TestDocument": {
                "type": "object",
                "properties": {
                    "Address": {
                        "type": "object",
                        "properties": {
                            "CityName": {
                                "type": "string"
                            },
                            "Line": {
                                "type": "array",
                                "maxItems": 2,
                                "minItems": 1,
                                "items": {
                                    "type": "string"
                                }
                            }
                        },
                        "additionalProperties": false,
                        "required": [
                            "Line"
                        ]
                    },
                    "Name": {
                        "type": "array",
                        "maxItems": 2,
                        "minItems": 1,
                        "items": {
                            "type": "string"
                        }
                    }
                },
                "additionalProperties": false,
                "required": [
                    "Address",
                    "Name"
                ]
            }
        }
    }