Skip to content

Commit 2d21a2b

Browse files
authored
Merge pull request #2228 from jackc/fix-xml-decode-value
XMLCodec: fix DecodeValue to return a []byte
2 parents 5f33ee5 + 03f08ab commit 2d21a2b

File tree

2 files changed

+48
-1
lines changed

2 files changed

+48
-1
lines changed

pgtype/pgtype_default.go

+19-1
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,25 @@ func initDefaultMap() {
9191
defaultMap.RegisterType(&Type{Name: "varchar", OID: VarcharOID, Codec: TextCodec{}})
9292
defaultMap.RegisterType(&Type{Name: "xid", OID: XIDOID, Codec: Uint32Codec{}})
9393
defaultMap.RegisterType(&Type{Name: "xid8", OID: XID8OID, Codec: Uint64Codec{}})
94-
defaultMap.RegisterType(&Type{Name: "xml", OID: XMLOID, Codec: &XMLCodec{Marshal: xml.Marshal, Unmarshal: xml.Unmarshal}})
94+
defaultMap.RegisterType(&Type{Name: "xml", OID: XMLOID, Codec: &XMLCodec{
95+
Marshal: xml.Marshal,
96+
// xml.Unmarshal does not support unmarshalling into *any. However, XMLCodec.DecodeValue calls Unmarshal with a
97+
// *any. Wrap xml.Marshal with a function that copies the data into a new byte slice in this case. Not implementing
98+
// directly in XMLCodec.DecodeValue to allow for the unlikely possibility that someone uses an alternative XML
99+
// unmarshaler that does support unmarshalling into *any.
100+
//
101+
// https://github.com/jackc/pgx/issues/2227
102+
// https://github.com/jackc/pgx/pull/2228
103+
Unmarshal: func(data []byte, v any) error {
104+
if v, ok := v.(*any); ok {
105+
dstBuf := make([]byte, len(data))
106+
copy(dstBuf, data)
107+
*v = dstBuf
108+
return nil
109+
}
110+
return xml.Unmarshal(data, v)
111+
},
112+
}})
95113

96114
// Range types
97115
defaultMap.RegisterType(&Type{Name: "daterange", OID: DaterangeOID, Codec: &RangeCodec{ElementType: defaultMap.oidToType[DateOID]}})

pgtype/xml_test.go

+29
Original file line numberDiff line numberDiff line change
@@ -97,3 +97,32 @@ func TestXMLCodecPointerToPointerToString(t *testing.T) {
9797
require.Nil(t, s)
9898
})
9999
}
100+
101+
func TestXMLCodecDecodeValue(t *testing.T) {
102+
skipCockroachDB(t, "CockroachDB does not support XML.")
103+
defaultConnTestRunner.RunTest(context.Background(), t, func(ctx context.Context, _ testing.TB, conn *pgx.Conn) {
104+
for _, tt := range []struct {
105+
sql string
106+
expected any
107+
}{
108+
{
109+
sql: `select '<foo>bar</foo>'::xml`,
110+
expected: []byte("<foo>bar</foo>"),
111+
},
112+
} {
113+
t.Run(tt.sql, func(t *testing.T) {
114+
rows, err := conn.Query(ctx, tt.sql)
115+
require.NoError(t, err)
116+
117+
for rows.Next() {
118+
values, err := rows.Values()
119+
require.NoError(t, err)
120+
require.Len(t, values, 1)
121+
require.Equal(t, tt.expected, values[0])
122+
}
123+
124+
require.NoError(t, rows.Err())
125+
})
126+
}
127+
})
128+
}

0 commit comments

Comments
 (0)