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

Panic with MapSeq #113

Open
prodion23 opened this issue Jun 14, 2023 · 2 comments
Open

Panic with MapSeq #113

prodion23 opened this issue Jun 14, 2023 · 2 comments

Comments

@prodion23
Copy link

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/v2@v2.5.8-0.20230210172309-cc007f32c523/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/v2@v2.5.8-0.20230210172309-cc007f32c523/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/v2@v2.5.8-0.20230210172309-cc007f32c523/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

@clbanning
Copy link
Owner

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?

@prodion23
Copy link
Author

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

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