typescript-generator icon indicating copy to clipboard operation
typescript-generator copied to clipboard

Add ability to combine useLibraryDefinition with primitivePropertiesRequired

Open oolubayo opened this issue 3 years ago • 0 comments

We are generating java classes from an XSD using JAXB.

We have some XSDs that look like this:

<xs:element name="someClass">
  <xs:complexType>
    <xs:simpleContent>
      <xs:extension base="xs:hexBinary">
        <xs:attribute name="format" type="custom:numberFormat" />
      </xs:extension>
    </xs:simpleContent>
  </xs:complexType>
</xs:element>

This will output XML like:

<someClass format="hex">FF00</someClass>

The resulting class will look like:

public class SomeClass {
    @XmlValue
    @XmlJavaTypeAdapter(HexBinaryAdapter.class)
    @XmlSchemaType(name = "hexBinary")
    protected byte[] value;

    @XmlAttribute(name = "format")
    protected NumberFormat format;

    // Other methods (getters, setters, etc.) hidden for simplicity
}

When using the typescript-generator with jsonLibrary set to "jaxb", and optionalProperties set to "useLibraryDefinition"

We get:

export interface SomeClass {
  value?: any;
  format?: NumberFormat;
}

Both fields are optional, but what we actually want are for both fields to be marked as required.

We can easily make format required, by modifying the XSD to:

<xs:attribute name="format" type="custom:numberFormat" use="required" />  

This will update the format Java property to:

    @XmlAttribute(name = "format", required = true) 
    protected NumberFormat format;

From here, we have 2 options that would solve our use case.

  1. We need to be able to either mark all primitives as required (best option as primitives should generally always be required).
  • Currently this only works if optionalProperties is set to "useSpecifiedAnnotations"
  1. Allow us to use the "useLibraryDefinition" value for optionalProperties while also enabling "requiredAnnotations" and we can put @XmlValue as a required annotation.

oolubayo avatar Jun 25 '21 14:06 oolubayo