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

fix(encoding/gxml): XML special character encoding error #3740

Merged
merged 7 commits into from
Sep 13, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions encoding/gxml/gxml.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,9 @@ func DecodeWithoutRoot(content []byte) (map[string]interface{}, error) {
// Encode encodes map `m` to an XML format content as bytes.
// The optional parameter `rootTag` is used to specify the XML root tag.
func Encode(m map[string]interface{}, rootTag ...string) ([]byte, error) {
mxj.XMLEscapeChars(true)
defer mxj.XMLEscapeChars(false)
oldme-git marked this conversation as resolved.
Show resolved Hide resolved

b, err := mxj.Map(m).Xml(rootTag...)
if err != nil {
err = gerror.Wrapf(err, `mxj.Map.Xml failed`)
Expand Down
23 changes: 23 additions & 0 deletions encoding/gxml/gxml_z_unit_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -206,3 +206,26 @@ func TestErrCase(t *testing.T) {
}
})
}

func Test_Issue3716(t *testing.T) {
gtest.C(t, func(t *gtest.T) {
var (
xml = `<Person><Bio>I am a software developer &amp; I love coding.</Bio><Email>[email protected]</Email><Name>&lt;&gt;&amp;&apos;&quot;AAA</Name></Person>`
m = map[string]interface{}{
"Person": map[string]interface{}{
"Name": "<>&'\"AAA",
"Email": "[email protected]",
"Bio": "I am a software developer & I love coding.",
},
}
)

xb, err := gxml.Encode(m)
t.AssertNil(err)
t.Assert(string(xb), xml)

dm, err := gxml.Decode(xb)
t.AssertNil(err)
t.Assert(dm, m)
})
}