highsource / jaxb2-basics

Useful plugins and tools for JAXB2.
BSD 2-Clause "Simplified" License
108 stars 54 forks source link

Simplify plugin error -- Unable to honor this as-element-property customization. It is attached to a wrong place, or its inconsistent with other bindings. #133

Open siarheul opened 2 years ago

siarheul commented 2 years ago

Hello!

I try to generate JAXB sources from the XSD schema My JAXB generated sources contain infamous 'fooOrBarOrBaz' looking property. In my case it is 'actualRemoteSectorIndexOrActualLocalSectorIndexOrRemoteMacAddr' property. In order to get rid of it I try to use 'simplify' plugin. Unfortunately, I can't make it work. What is get is an error

[ERROR] Error while parsing schema(s).Location [ file:/C:/_code/jaxb2-basics-sample-basic-0.12.0/src/main/resources/test.xjb{9,49}].
com.sun.istack.SAXParseException2: compiler was unable to honor this as-element-property customization. It is attached to a wrong place, or its inconsistent with other bindings.
    at com.sun.tools.xjc.ErrorReceiver.error (ErrorReceiver.java:86)
    at com.sun.tools.xjc.reader.xmlschema.ErrorReporter.error (ErrorReporter.java:84)
    at com.sun.tools.xjc.reader.xmlschema.UnusedCustomizationChecker.check (UnusedCustomizationChecker.java:150)
    at com.sun.tools.xjc.reader.xmlschema.UnusedCustomizationChecker.check (UnusedCustomizationChecker.java:128)
    at com.sun.tools.xjc.reader.xmlschema.UnusedCustomizationChecker.modelGroup (UnusedCustomizationChecker.java:226)
    at com.sun.xml.xsom.impl.ModelGroupImpl.visit (ModelGroupImpl.java:110)
    at com.sun.tools.xjc.reader.xmlschema.UnusedCustomizationChecker.particle (UnusedCustomizationChecker.java:247)
    at com.sun.xml.xsom.impl.ParticleImpl.visit (ParticleImpl.java:117)
    at com.sun.tools.xjc.reader.xmlschema.UnusedCustomizationChecker.modelGroup (UnusedCustomizationChecker.java:228)
    at com.sun.xml.xsom.impl.ModelGroupImpl.visit (ModelGroupImpl.java:110)
    at com.sun.tools.xjc.reader.xmlschema.UnusedCustomizationChecker.particle (UnusedCustomizationChecker.java:247)
    at com.sun.xml.xsom.impl.ParticleImpl.visit (ParticleImpl.java:117)
    at com.sun.tools.xjc.reader.xmlschema.UnusedCustomizationChecker.complexType (UnusedCustomizationChecker.java:188)
    at com.sun.xml.xsom.impl.ComplexTypeImpl.visit (ComplexTypeImpl.java:280)
    at com.sun.tools.xjc.reader.xmlschema.UnusedCustomizationChecker.elementDecl (UnusedCustomizationChecker.java:234)
    at com.sun.xml.xsom.impl.ElementDecl.visit (ElementDecl.java:252)
    at com.sun.tools.xjc.reader.xmlschema.UnusedCustomizationChecker.particle (UnusedCustomizationChecker.java:247)
    at com.sun.xml.xsom.impl.ParticleImpl.visit (ParticleImpl.java:117)

This is my XSD:

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified">
  <xs:group name="radio-bridge-tg-radio-common___radio-top">
    <xs:sequence>
      <xs:element minOccurs="0" ref="radio-common"/>
    </xs:sequence>
  </xs:group>
  <xs:element name="radio-common" type="radio-bridge-tg-radio-common___links-top"/>
  <xs:complexType name="radio-bridge-tg-radio-common___links-top">
    <xs:sequence>
      <xs:element minOccurs="0" ref="links"/>
    </xs:sequence>
  </xs:complexType>
  <xs:element name="links">
    <xs:complexType>
      <xs:sequence>
        <xs:element minOccurs="0" maxOccurs="unbounded" ref="active"/>
      </xs:sequence>
    </xs:complexType>
  </xs:element>
  <xs:element name="active">
    <xs:complexType>
      <xs:sequence>
        <xs:element ref="remote-assigned-name"/>
        <xs:choice minOccurs="0" maxOccurs="unbounded">
          <xs:element ref="actual-remote-sector-index"/>
          <xs:element ref="actual-local-sector-index"/>
          <xs:element ref="remote-mac-addr"/>
          <xs:element ref="local-role"/>
          <xs:element ref="link-uptime"/>
        </xs:choice>
      </xs:sequence>
    </xs:complexType>
  </xs:element>
  <xs:element name="remote-assigned-name" type="radio-bridge-tg-types__node-assigned-name"/>
  <xs:element name="actual-remote-sector-index" type="radio-bridge-tg-types__radio-sector-index"/>
  <xs:element name="actual-local-sector-index" type="radio-bridge-tg-types__radio-sector-index"/>
  <xs:element name="remote-mac-addr" type="ietf-yang-types__mac-address"/>
  <xs:element name="local-role" type="radio-bridge-tg-radio-common__link-role"/>
  <xs:element name="link-uptime" type="xs:unsignedLong"/>
  <xs:simpleType name="radio-bridge-tg-types__node-assigned-name">
    <xs:restriction base="xs:string">
      <xs:pattern value="[a-z0-9]{1,8}"/>
    </xs:restriction>
  </xs:simpleType>
  <xs:simpleType name="radio-bridge-tg-types__radio-sector-index">
    <xs:restriction base="xs:unsignedByte">
      <xs:minInclusive value="1"/>
      <xs:maxInclusive value="4"/>
    </xs:restriction>
  </xs:simpleType>
  <xs:simpleType name="ietf-yang-types__mac-address">
    <xs:restriction base="xs:string">
      <xs:pattern value="[0-9a-fA-F]{2}(:[0-9a-fA-F]{2}){5}"/>
    </xs:restriction>
  </xs:simpleType>
  <xs:simpleType name="radio-bridge-tg-radio-common__link-role">
    <xs:restriction base="xs:token">
      <xs:enumeration value="initiator"/>
      <xs:enumeration value="responder"/>
    </xs:restriction>
  </xs:simpleType>
</xs:schema>

This is my XJB file:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<jaxb:bindings jaxb:version="2.1"
          xmlns:jaxb="http://java.sun.com/xml/ns/jaxb"
          xmlns:simplify="http://jaxb2-commons.dev.java.net/basic/simplify"
          xmlns:xs="http://www.w3.org/2001/XMLSchema"
          jaxb:extensionBindingPrefixes="simplify">
        <jaxb:bindings schemaLocation="test.xsd"
                       node="/xs:schema/xs:element[@name='active']/xs:complexType/xs:sequence/xs:choice">
                <simplify:as-element-property />
        </jaxb:bindings>
</jaxb:bindings>

This is my pom.xml file:

<build>
        <defaultGoal>test</defaultGoal>
        <plugins>
            <plugin>
                <groupId>org.jvnet.jaxb2.maven2</groupId>
                <artifactId>maven-jaxb2-plugin</artifactId>
                <version>0.14.0</version>
                <executions>
                    <execution>
                        <goals>
                            <goal>generate</goal>
                        </goals>
                    </execution>
                </executions>
                <configuration>
                        <args>
                        <arg>-XtoString</arg>
                        <arg>-Xequals</arg>
                        <arg>-XhashCode</arg>
                        <arg>-Xinheritance</arg>
                        <arg>-Xcopyable</arg>
                        <arg>-XenumValue</arg>
                        <arg>-Xsimplify</arg>
                    </args>
                    <plugins>
                        <plugin>
                            <groupId>org.jvnet.jaxb2_commons</groupId>
                            <artifactId>jaxb2-basics</artifactId>
                            <version>0.12.0</version>
                        </plugin>
                    </plugins>
                </configuration>
            </plugin>
            <plugin>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>1.7</source>
                    <target>1.7</target>
                </configuration>
            </plugin>
        </plugins>
    </build>
AddisonSnyder commented 2 years ago

Running into the same issue, but with types as well. image At first I was using class references which is not supported. But I still can't evade that error you're having, even though I'm now sticking to types.

Ceshion commented 1 year ago

I am seeing this without using class references too- when we are using the annotation on choice rather than on complexType, and two or more elements in the parent type share a type, so like e.g. this worked:

          <xs:complexType>
            <xs:choice maxOccurs="unbounded" minOccurs="0">
              <xs:annotation>
                <xs:appinfo>
                    <simplify:as-element-property/>
                </xs:appinfo>
              </xs:annotation>
              <xs:element name="sProp" type="xs:string"></xs:element>
              <xs:element name="fProp" type="xs:float"></xs:element>
              <xs:element name="bProp" type="xs:byte"></xs:element>
            </xs:choice>
          </xs:complexType>
        </xs:element>

but this did not:

          <xs:complexType>
            <xs:choice maxOccurs="unbounded" minOccurs="0">
              <xs:annotation>
                <xs:appinfo>
                    <simplify:as-element-property/>
                </xs:appinfo>
              </xs:annotation>
              <xs:element name="sProp" type="xs:string"></xs:element>
              <xs:element name="sTwoProp" type="xs:string"></xs:element> <!-- Adding this line breaks it -->
              <xs:element name="fProp" type="xs:float"></xs:element>
              <xs:element name="bProp" type="xs:byte"></xs:element>
            </xs:choice>
          </xs:complexType>
        </xs:element>

However, in that case it seemed like annotating the type element instead of choice did work:

          <xs:complexType>
            <xs:annotation>
              <xs:appinfo>
                <simplify:property name="sPropOrSTwoPropOrFProp">
                  <simplify:as-element-property/>
                </simplify:property>
              </xs:appinfo>
            </xs:annotation>
            <xs:choice maxOccurs="unbounded" minOccurs="0">
              <xs:element name="sProp" type="xs:string"></xs:element> <!-- We can coexist! -->
              <xs:element name="sTwoProp" type="xs:string"></xs:element> <!-- Yay! -->
              <xs:element name="fProp" type="xs:float"></xs:element>
              <xs:element name="bProp" type="xs:byte"></xs:element>
            </xs:choice>
          </xs:complexType>
        </xs:element>
imitbn commented 1 year ago

According to the documentation one of the xs:element has to be customized and not the xs:choice: https://github.com/highsource/jaxb2-basics/wiki/JAXB2-Simplify-Plugin#as-element-properties

The same to the bindings file:

<jaxb:bindings schemaLocation="<schema>" node="<path-to-comlex-type>/xs:choice/xs:element[1]">
    <simplify:as-element-property/>
</jaxb:bindings>
laurentschoelens commented 1 year ago

Hi @Ceshion / @imitbn / @AddisonSnyder / @siarheul

@imitbn : I guess you're right, you can only customize an element in xsd file.

In addition, jaxb2-basics have moved to jaxb-tools repository (former maven-jaxb2-plugin) and jakarta is on it's way. We also did reimport all wiki pages of this repo (and added some restored from previous highsource's confluence)

If documentation is not clear enough, please let me know so we can fix it there.

Also @Everyone : if you have any problems regarding this plugin usage, feel free also to post a project sample to illustrate.

Regards