andrewbober / xsd2jsonschema

A pure JavaScript library for translating complex XML Schemas into JSON Schemas.
https://www.xsd2jsonschema.org
Apache License 2.0
47 stars 25 forks source link

Wrong handling of root element #12

Open ghilainm opened 5 years ago

ghilainm commented 5 years ago

It seems like the handling of the root element is not correct.

Here is the xsd

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" targetNamespace="http:/xxx">
  <!--==========================================================-->
  <!--===== Root Element                                        -->
  <!--==========================================================-->
  <xs:element name="MyMessage" type="MyMessageType" />
  <!--==========================================================-->
  <!--===== Root Element Content                                -->
  <!--==========================================================-->
  <xs:complexType name="MyMessageType">
    <xs:sequence>
      <xs:element name="myField" type="xs:string">
      </xs:element>
    </xs:sequence>
  </xs:complexType>
</xs:schema>

Here is the generated json

definitions:
    MyMessage:
      $ref: "#/definitions/MyMessageType"
    MyMessageType:
      type: object
      properties:
        myField:
          type: string
      required:
        - myField

Here is the expected generated json

definitions:
    MyMessage:
      type: object
      properties:
        myField:
          type: string
      required:
        - myField
andrewbober commented 5 years ago

MyMessageType is added to the the definitions section so it can be used by both MyMessage and by other messages from other files using the well known "definitions" convention.

Just wondering why wouldn't you want MyMessageType to be added to the definitions section?

ghilainm commented 5 years ago

Here I was just following the JAXB way. They don't generate a MyMessageType, they only generate MyMessage.

MyMessage and MyMessageType define the same type, MyMessage is just an alias (at least in the json schema), so that's why it was weird for me.