eed3si9n / scalaxb

scalaxb is an XML data binding tool for Scala.
http://scalaxb.org/
MIT License
337 stars 156 forks source link

Extending base from different namespace puts elements from that namespace in the wrong namespace #135

Closed ghost closed 12 years ago

ghost commented 12 years ago

Given one xsd, say common.xsd:

--------------------------------common.xsd--------------------------------

<xs:schema xmlns="http://www.vmware.com/vcloud/v1.5" xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" targetNamespace="http://www.vmware.com/vcloud/v1.5" version="1.0">
...
        <xs:complexContent>
            <xs:extension base="VCloudExtensibleType">
                <xs:sequence>
                    <xs:element name="Link" type="LinkType" minOccurs="0" maxOccurs="unbounded">
                        <xs:annotation>
                            <xs:documentation source="modifiable">none</xs:documentation>
                            <xs:documentation xml:lang="en">
                                Optional link to an entity or operation associated
                                with this object.
                            </xs:documentation>
                        </xs:annotation>
                    </xs:element>
                </xs:sequence>
                <xs:attribute name="href" type="xs:anyURI" use="optional">
                    <xs:annotation>
                        <xs:documentation source="modifiable">always</xs:documentation>
                        <xs:documentation xml:lang="en">
                            Contains the URI to the entity.
                        </xs:documentation>
                    </xs:annotation>
                </xs:attribute>
                <xs:attribute name="type" type="xs:string" use="optional">
                    <xs:annotation>
                        <xs:documentation source="modifiable">always</xs:documentation>
                        <xs:documentation xml:lang="en">
                            Contains the type of the entity.
                        </xs:documentation>
                    </xs:annotation>
                </xs:attribute>
            </xs:extension>
        </xs:complexContent>
    </xs:complexType>
...

--------------------------------/common.xsd--------------------------------

And another, vmwextensions.xsd:

--------------------------------vmwextensions.xsd--------------------------------

<xs:schema xmlns="http://www.vmware.com/vcloud/extension/v1.5" xmlns:vcloud="http://www.vmware.com/vcloud/v1.5" xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" targetNamespace="http://www.vmware.com/vcloud/extension/v1.5" version="1.0">
...
    <xs:complexType name="VMWNetworkPoolReferencesType">
        <xs:annotation>
            <xs:documentation source="since">1.0</xs:documentation>
            <xs:documentation xml:lang="en">
                Represents a list of available network pools.
            </xs:documentation>
        </xs:annotation>
        <xs:complexContent>
            <xs:extension base="vcloud:ResourceType">
                <xs:sequence>
                    <xs:element name="NetworkPoolReference" type="vcloud:ReferenceType" minOccurs="0" maxOccurs="unbounded">
                        <xs:annotation>
                            <xs:documentation source="modifiable">none</xs:documentation>
                            <xs:documentation xml:lang="en">
                                Reference to a network pool.
                            </xs:documentation>
                        </xs:annotation>
                    </xs:element>
                </xs:sequence>
            </xs:extension>
        </xs:complexContent>
    </xs:complexType>
...

--------------------------------/vmwextensions.xsd--------------------------------

where VMWNetworkPoolReferencesType extends ResourceType from common.xsd, which is in the vcloud namespace, we find, in xmlprotocol.scala:

--------------------------------xmlprotocol.scala--------------------------------

...
  trait DefaultVcloudVMWNetworkPoolReferencesTypeFormat extends scalaxb.ElemNameParser[com.vmware.api.vcloud.VMWNetworkPoolReferencesType] {
    val targetNamespace: Option[String] = Some("http://www.vmware.com/vcloud/extension/v1.5")

    override def typeName: Option[String] = Some("VMWNetworkPoolReferencesType")

    def parser(node: scala.xml.Node, stack: List[scalaxb.ElemName]): Parser[com.vmware.api.vcloud.VMWNetworkPoolReferencesType] =
      rep(scalaxb.ElemName(Some("http://www.vmware.com/vcloud/extension/v1.5"), "VCloudExtension")) ~ 
      rep(scalaxb.ElemName(Some("http://www.vmware.com/vcloud/extension/v1.5"), "Link")) ~ 
      rep(scalaxb.ElemName(Some("http://www.vmware.com/vcloud/extension/v1.5"), "NetworkPoolReference")) ^^
      { case p1 ~ p2 ~ p3 =>
      com.vmware.api.vcloud.VMWNetworkPoolReferencesType(p1.toSeq map { scalaxb.fromXML[com.vmware.api.vcloud.VCloudExtensionType](_, scalaxb.ElemName(node) :: stack) },
        p2.toSeq map { scalaxb.fromXML[com.vmware.api.vcloud.LinkType](_, scalaxb.ElemName(node) :: stack) },
        p3.toSeq map { scalaxb.fromXML[com.vmware.api.vcloud.ReferenceTypable](_, scalaxb.ElemName(node) :: stack) },
        (node \ "@href").headOption map { scalaxb.fromXML[java.net.URI](_, scalaxb.ElemName(node) :: stack) },
        (node \ "@type").headOption map { scalaxb.fromXML[String](_, scalaxb.ElemName(node) :: stack) },
        scala.collection.immutable.ListMap((node match {
          case elem: scala.xml.Elem =>
            elem.attributes.toList flatMap {
              case scala.xml.UnprefixedAttribute(key, value, _) if key == "href" => Nil
              case scala.xml.UnprefixedAttribute(key, value, _) if key == "type" => Nil
              case scala.xml.UnprefixedAttribute(key, value, _) =>
                List(("@" + key, scalaxb.DataRecord(None, Some(key), value.text)))
              case scala.xml.PrefixedAttribute(pre, key, value, _) =>
                val ns = elem.scope.getURI(pre)
                List(("@{" + ns + "}" + key, scalaxb.DataRecord(Option[String](ns), Some(key), value.text)))
              case _ => Nil
            }
          case _ => Nil
        }): _*)) }

    override def writesAttribute(__obj: com.vmware.api.vcloud.VMWNetworkPoolReferencesType, __scope: scala.xml.NamespaceBinding): scala.xml.MetaData = {
      var attr: scala.xml.MetaData  = scala.xml.Null
      __obj.href foreach { x => attr = scala.xml.Attribute(null, "href", x.toString, attr) }
      __obj.typeValue foreach { x => attr = scala.xml.Attribute(null, "type", x.toString, attr) }
      __obj.attributes.toList map {
        case (key, x) => attr = scala.xml.Attribute((x.namespace map { __scope.getPrefix(_) }).orNull, x.key.orNull, x.value.toString, attr) }
      attr
    }

    def writesChildNodes(__obj: com.vmware.api.vcloud.VMWNetworkPoolReferencesType, __scope: scala.xml.NamespaceBinding): Seq[scala.xml.Node] =
      Seq.concat(__obj.VCloudExtension flatMap { scalaxb.toXML[com.vmware.api.vcloud.VCloudExtensionType](_, Some("http://www.vmware.com/vcloud/extension/v1.5"), Some("VCloudExtension"), __scope, false) },
        __obj.Link flatMap { scalaxb.toXML[com.vmware.api.vcloud.LinkType](_, Some("http://www.vmware.com/vcloud/extension/v1.5"), Some("Link"), __scope, false) },
        __obj.NetworkPoolReference flatMap { scalaxb.toXML[com.vmware.api.vcloud.ReferenceTypable](_, Some("http://www.vmware.com/vcloud/extension/v1.5"), Some("NetworkPoolReference"), __scope, false) })

  }
...

--------------------------------/xmlprotocol.scala--------------------------------

This fails to parse the following actual XML from vCloud Director:

--------------------------------XML from vCD--------------------------------

<vmext:VMWNetworkPoolReferences 
xsi:schemaLocation="http://www.vmware.com/vcloud/extension/v1.5 http://172.16.200.220/api/v1.5/schema/vmwextensions.xsd http://www.vmware.com/vcloud/v1.5 http://172.16.200.220/api/v1.5/schema/master.xsd" type="application/vnd.vmware.admin.vmwNetworkPoolReferences+xml" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:vcloud="http://www.vmware.com/vcloud/v1.5" xmlns:vmext="http://www.vmware.com/vcloud/extension/v1.5">
  <vcloud:Link href="https://172.16.200.220/api/admin/extension" type="application/vnd.vmware.admin.vmwExtension+xml" rel="up"></vcloud:Link>
  <vmext:NetworkPoolReference 
  href="https://172.16.200.220/api/admin/extension/networkPool/cfa023af-e9d7-49b5-baf0-6cd868af6025" name="Network-Pool-C4-Tenant-vCD-NI" type="application/vnd.vmware.admin.networkPool+xml">
</vmext:NetworkPoolReference>
</vmext:VMWNetworkPoolReferences>

--------------------------------/XML from vCD--------------------------------

The issue seems to be that scalaxb expects the "Link" element to be in the "http://www.vmware.com/vcloud/extension/v1.5" namespace instead of the "http://www.vmware.com/vcloud/v1.5" namespace. Removing the Link element from the XML results in a successful parse of the remainder.

The code that generates the XML appears to be of the opinion :-) that even though the NetworkPoolReference element is of type "vcloud:ReferenceType" it should be sent in the "http://www.vmware.com/vcloud/extension/v1.5" namespace, presumably because the element declaration is in that namespace. Hopefully that's correct, and all that needs to be resolved is the namespace of the inherited elements and attributes from the base.

eed3si9n commented 12 years ago

Fixed the issue, but I haven't confirmed it with your schemas. Published 0.6.8-SNAPSHOT both for the library and sbt plugin for 0.10.1.