mxj icon indicating copy to clipboard operation
mxj copied to clipboard

Panic with MapSeq

Open prodion23 opened this issue 2 years ago • 2 comments

Hello, I'm trying to parse an XML:

<TopLevel>
    <NestedElem>http://foo.bar.com</NestedElem>
    TextValue
</TopLevel>

This parses fine; however, when attempting to convert the parsed struct back into an XML string using mxj.NewMapXmlSeq, I encounter a panic.

This is example reproduction:

func TestReproductionCase(t *testing.T) {
	exampleInput := `<TopLevel>
						<NestedElem>http://foo.bar.com</NestedElem>
						TextValue
					</TopLevel>`
	x := []byte(exampleInput)

        // Using msv, err := mxj.NewMapXml(x) doesn't panic
	msv, err := mxj.NewMapXmlSeq(x)
	if err != nil && err != io.EOF {
		t.Fatal("err:", err.Error())
	}
	fmt.Println("NewMapXmlSeq, x:\n", string(x))
	fmt.Println("NewMapXmlSeq, s:\n", msv.StringIndent())

	b, err := msv.XmlIndent("", "  ")
	if err != nil {
		t.Fatal("err:", err)
	}
	fmt.Println("NewMapXmlSeq, msv.XmlIndent():\n", string(b))
}

If I switch to mxj.NewMapXml it does work; but order isn't retained.

The panic is happening in the Less function because the TextValue isn't a map interface. Attaching a debugger screenshot before the panic & snippet from the stack here as well:

panic({0x1b3d960, 0xc00018db30})
	/Users/me/go/go1.20.1/src/runtime/panic.go:890 +0x262
github.com/clbanning/mxj/v2.elemListSeq.Less({0xc00018ec80, 0x2, 0x2}, 0x1, 0x0)
	/Users/me/go/pkg/mod/github.com/clbanning/mxj/[email protected]/xmlseq.go:873 +0x459
sort.insertionSort({0x1cb82e0, 0xc000010870}, 0x0, 0x2)
	/Users/me/go/go1.20.1/src/sort/zsortinterface.go:12 +0x91
sort.pdqsort({0x1cb82e0, 0xc000010870}, 0x0, 0x2, 0x2)
	/Users/me/go/go1.20.1/src/sort/zsortinterface.go:73 +0x9e
sort.Sort({0x1cb82e0, 0xc000010870})
	/Users/me/go/go1.20.1/src/sort/sort.go:48 +0x6f
github.com/clbanning/mxj/v2.mapToXmlSeqIndent(0x1, 0xc000225ad8, {0xc0001ce4b8, 0x8}, {0x1b32260, 0xc00018daa0}, 0xc000225b98)
	/Users/me/go/pkg/mod/github.com/clbanning/mxj/[email protected]/xmlseq.go:721 +0x294c
github.com/clbanning/mxj/v2.MapSeq.XmlIndent(0xc00018da70, {0x0, 0x0}, {0x1c0ee42, 0x2}, {0x0, 0x0, 0x0})
	/Users/me/go/pkg/mod/github.com/clbanning/mxj/[email protected]/xmlseq.go:567 +0x765
Screenshot 2023-06-14 at 2 20 41 PM

Do you think this is a scenario this library could account for, or should users handle this?

Thank you for any guidance

prodion23 avatar Jun 14 '23 18:06 prodion23

With regard to: <TopLevel> <NestedElem>http://foo.bar.com</NestedElem> TextValue </TopLevel>

I have never seen a mixed simple and compound element. Can you direct me to some documentation that the XML standard now supports such a construct?

clbanning avatar Jun 15 '23 20:06 clbanning

Hey thanks for the quick reply. (I also hadn't seen this previously but from what I can tell, it does seem "valid")

I believe this section is what you are looking for: https://www.w3.org/TR/xml/#sec-mixed-content Specifically:

An element type has mixed content when elements of that type may contain character data, optionally interspersed with child elements.]

I'm also not very familiar with xml standard; so if that is referencing something else just let me know. I couldn't find anything else related to mixed contents

prodion23 avatar Jun 15 '23 22:06 prodion23