-
Notifications
You must be signed in to change notification settings - Fork 817
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
pgx doesn't encode json.RawMessage params #1763
Comments
Underlying types were already tried. But []byte is not a normal underlying type. It is a slice. But since is can be treated as a scalar instead of an array / slice we need to special case it. #1763
Fixed in 45f807f. |
Hi, I have a problem when encoding With simple_protocol, a It can be reproduced with the following test. package main
import (
"database/sql"
"encoding/json"
_ "github.com/jackc/pgx/v5/stdlib"
"math/rand"
"testing"
)
func prepareDBDriver(driver string) (*sql.DB, error) {
db, err := sql.Open(driver, "postgres://postgres:123456@127.0.0.1:5432/playground?sslmode=disable&default_query_exec_mode=simple_protocol")
if err != nil {
return nil, err
}
_, err = db.Exec(`CREATE TABLE IF NOT EXISTS value_test(
id SERIAL PRIMARY KEY,
text TEXT)`)
if err != nil {
return nil, err
}
return db, err
}
type point struct {
X, Y, Z int
}
func testEncodeJsonRawMessage(driver string, t *testing.T) {
db, err := prepareDBDriver(driver)
if err != nil {
t.Errorf("failed to initialize database: %v", err)
return
}
defer func(db *sql.DB) {
_ = db.Close()
}(db)
p := point{
X: rand.Intn(10),
Y: rand.Intn(10),
Z: 0,
}
pb, err := json.Marshal(&p)
if err != nil {
t.Errorf("failed to marshal %+v: %v", p, err)
return
}
pr := json.RawMessage(pb)
_, err = db.Exec(`INSERT INTO value_test(text) VALUES($1)`, pr)
if err != nil {
t.Errorf("failed to insert %s: %v", pr, err)
return
}
}
func TestPgxEncodeJsonRawMessage(t *testing.T) {
testEncodeJsonRawMessage("pgx", t)
}
|
The underlying type of json.RawMessage is a []byte so to avoid it being considered binary data we need to handle it specifically. This is done by registerDefaultPgTypeVariants. In addition, handle json.RawMessage in the JSONCodec PlanEncode to avoid it being mutated by json.Marshal. #1763
@kamatama41 Fixed in 88dfc22. The fundamental issue was that |
Just want to say THANK YOU to @jackc I was using pgx 5.4.3 and ran into this issue. I spent almost an hour trying to figure out looking at diffs and realized maybe it could be due to the fact we switched to pgbouncer and simple protocol. On 5.5.5 everything works again. |
Glad to see this fixed. I was doing this in my code, so it'll be nice to remove it. It felt out of place. |
Describe the bug
Unlike the pq library, the pgx stdlib doesn't encode types like
json.RawMessage
arguments.type json.RawMessage []byte
is defined in ecndoding/json.To Reproduce
json.RawMessage
as its args.Expected behavior
That is, pgx should encode commonly used built-in go library types correctly.
Or at least, any type with underlying type of core types, like
string
,[]byte
should be encoded correctly without extra configuration.Actual behavior
TestPqEncodeJsonRawMessage
: PASS, butTestPgxEncodeJsonRawMessage
FAILEDProgram complains:
Version
$ go version
-> go1.21.0 linux/amd64$ psql --no-psqlrc --tuples-only -c 'select version()'
->PostgreSQL 12.16 (Debian 12.16-1.pgdg120+1) on x86_64-pc-linux-gnu, compiled by gcc (Debian 12.2.0-14) 12.2.0, 64-bit$ grep 'github.com/jackc/pgx/v[0-9]' go.mod
-> v5.3.1Additional context
n/a
The text was updated successfully, but these errors were encountered: