mirror of
https://github.com/jackc/pgx.git
synced 2025-05-31 11:42:24 +00:00
Fix JSON scan not completely overwriting destination
See https://github.com/jackc/pgtype/pull/185 for original report in pgx v4 / pgtype.
This commit is contained in:
parent
bb6c997102
commit
f8d088cfb6
@ -134,6 +134,9 @@ func (scanPlanJSONToJSONUnmarshal) Scan(src []byte, dst any) error {
|
|||||||
return fmt.Errorf("cannot scan NULL into %T", dst)
|
return fmt.Errorf("cannot scan NULL into %T", dst)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
elem := reflect.ValueOf(dst).Elem()
|
||||||
|
elem.Set(reflect.Zero(elem.Type()))
|
||||||
|
|
||||||
return json.Unmarshal(src, dst)
|
return json.Unmarshal(src, dst)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -93,3 +93,16 @@ func TestJSONCodecUnmarshalSQLNull(t *testing.T) {
|
|||||||
require.EqualError(t, err, "can't scan into dest[0]: cannot scan NULL into *int")
|
require.EqualError(t, err, "can't scan into dest[0]: cannot scan NULL into *int")
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestJSONCodecClearExistingValueBeforeUnmarshal(t *testing.T) {
|
||||||
|
defaultConnTestRunner.RunTest(context.Background(), t, func(ctx context.Context, t testing.TB, conn *pgx.Conn) {
|
||||||
|
m := map[string]any{}
|
||||||
|
err := conn.QueryRow(ctx, `select '{"foo": "bar"}'::json`).Scan(&m)
|
||||||
|
require.NoError(t, err)
|
||||||
|
require.Equal(t, map[string]any{"foo": "bar"}, m)
|
||||||
|
|
||||||
|
err = conn.QueryRow(ctx, `select '{"baz": "quz"}'::json`).Scan(&m)
|
||||||
|
require.NoError(t, err)
|
||||||
|
require.Equal(t, map[string]any{"baz": "quz"}, m)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user