Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Critical parsing bug with scalaxb.fromXML #402

Open
margussipria opened this issue Oct 10, 2016 · 1 comment
Open

Critical parsing bug with scalaxb.fromXML #402

margussipria opened this issue Oct 10, 2016 · 1 comment

Comments

@margussipria
Copy link
Contributor

margussipria commented Oct 10, 2016

Critical bug with scalaxb.fromXML

I made little example xsd:

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" targetNamespace="http://sipria.eu/scalaxb-bug/" elementFormDefault="qualified" attributeFormDefault="unqualified">
    <xs:element name="ApplicationResponse">
        <xs:annotation>
            <xs:documentation>Wrapper for data. Used for transmitting data</xs:documentation>
        </xs:annotation>
        <xs:complexType>
            <xs:sequence>
                <xs:element name="Extension" type="xs:anyType" minOccurs="0"/>
                <xs:element name="FileType" minOccurs="0"/>
                <xs:element name="Content" type="xs:base64Binary" nillable="false" minOccurs="0"/>
            </xs:sequence>
        </xs:complexType>
    </xs:element>
</xs:schema>

using this sbt options:

    sourceGenerators in Compile <+= scalaxb in Compile,
    dispatchVersion in scalaxb in Compile := "0.11.3",
    packageNames in scalaxb in Compile := Map(
      uri("http://sipria.eu/scalaxb-bug/") -> "bug"
    )

Generated code is "correct":

package bug


case class ApplicationResponse(Extension: Option[scalaxb.DataRecord[Any]] = None,
  FileType: Option[scalaxb.DataRecord[Any]] = None,
  Content: Option[scalaxb.Base64Binary] = None)

When parsing xml that does not have first optionals, then first elements are filled with content indented for later elements

scala> scalaxb.fromXML[bug.ApplicationResponse](<ApplicationResponse xmlns="http://sipria.eu/scalaxb-bug/"><Content>UGlwcHVyaSAmIEthbmVsaQ==</Content></ApplicationResponse>)
res0: bug.ApplicationResponse = ApplicationResponse(Some(DataRecord({http://sipria.eu/scalaxb-bug/}Content,<Content xmlns="http://sipria.eu/scalaxb-bug/">UGlwcHVyaSAmIEthbmVsaQ==</Content>)),None,None)

// Content is rightly parsed only when all other optional elements exists

scala> scalaxb.fromXML[bug.ApplicationResponse](<ApplicationResponse xmlns="http://sipria.eu/scalaxb-bug/"><Extension></Extension><FileType></FileType><Content>UGlwcHVyaSAmIEthbmVsaQ==</Content></ApplicationResponse>)
res1: bug.ApplicationResponse = ApplicationResponse(Some(DataRecord({http://sipria.eu/scalaxb-bug/}Extension,<Extension xmlns="http://sipria.eu/scalaxb-bug/"></Extension>)),Some(DataRecord({http://sipria.eu/scalaxb-bug/}FileType,<FileType xmlns="http://sipria.eu/scalaxb-bug/"></FileType>)),Some(UGlwcHVyaSAmIEthbmVsaQ==))

Generated Format code as following:

  trait DefaultBugApplicationResponseFormat extends scalaxb.ElemNameParser[bug.ApplicationResponse] {
    val targetNamespace: Option[String] = Some("http://sipria.eu/scalaxb-bug/")

    def parser(node: scala.xml.Node, stack: List[scalaxb.ElemName]): Parser[bug.ApplicationResponse] =
      phrase(opt(any(_ => true)) ~ 
      opt(any(_ => true)) ~ 
      opt(scalaxb.ElemName(Some("http://sipria.eu/scalaxb-bug/"), "Content")) ^^
      { case p1 ~ p2 ~ p3 =>
      bug.ApplicationResponse(p1.headOption map { scalaxb.fromXML[scalaxb.DataRecord[Any]](_, scalaxb.ElemName(node) :: stack) },
        p2.headOption map { scalaxb.fromXML[scalaxb.DataRecord[Any]](_, scalaxb.ElemName(node) :: stack) },
        p3.headOption map { scalaxb.fromXML[scalaxb.Base64Binary](_, scalaxb.ElemName(node) :: stack) }) })

    def writesChildNodes(__obj: bug.ApplicationResponse, __scope: scala.xml.NamespaceBinding): Seq[scala.xml.Node] =
      Seq.concat(__obj.Extension map { x => scalaxb.toXML[scalaxb.DataRecord[Any]](x, x.namespace, x.key, __scope, true) } getOrElse {Nil},
        __obj.FileType map { x => scalaxb.toXML[scalaxb.DataRecord[Any]](x, x.namespace, x.key, __scope, true) } getOrElse {Nil},
        __obj.Content map { scalaxb.toXML[scalaxb.Base64Binary](_, Some("http://sipria.eu/scalaxb-bug/"), Some("Content"), __scope, false) } getOrElse {Nil})

  }
@hongwei1
Copy link

hongwei1 commented Mar 8, 2021

it is relevant to #561 , please fix it

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants