Skip to content

Commit

Permalink
fix(pgdialect): handle nill array on jsonb column
Browse files Browse the repository at this point in the history
  • Loading branch information
rfarrjr committed Jan 29, 2025
1 parent 1422b77 commit 0dc4e3e
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 0 deletions.
5 changes: 5 additions & 0 deletions dialect/pgdialect/array_parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@ type arrayParser struct {
func newArrayParser(b []byte) *arrayParser {
p := new(arrayParser)

if b[0] == 'n' {
p.p.Reset(nil)
return p
}

if len(b) < 2 || (b[0] != '{' && b[0] != '[') || (b[len(b)-1] != '}' && b[len(b)-1] != ']') {
p.err = fmt.Errorf("pgdialect: can't parse array: %q", b)
return p
Expand Down
7 changes: 7 additions & 0 deletions internal/dbtest/pg_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -922,6 +922,7 @@ func TestPostgresJSONB(t *testing.T) {
ItemPtr *Item `bun:",type:jsonb"`
Items []Item `bun:",type:jsonb"`
ItemsP []*Item `bun:",type:jsonb"`
ItemsNull []*Item `bun:",type:jsonb"`
TextItemA []UserID `bun:"type:text[]"`
}

Expand All @@ -939,6 +940,7 @@ func TestPostgresJSONB(t *testing.T) {
ItemPtr: &item2,
Items: []Item{item1, item2},
ItemsP: []*Item{&item1, &item2},
ItemsNull: nil,
TextItemA: []UserID{uid1, uid2},
}
_, err := db.NewInsert().Model(model1).Exec(ctx)
Expand All @@ -962,4 +964,9 @@ func TestPostgresJSONB(t *testing.T) {
require.NoError(t, err)
require.Equal(t, []Item{item1, item2}, items)

err = db.NewSelect().Model((*Model)(nil)).
Column("items_null").
Scan(ctx, pgdialect.Array(&items))
require.NoError(t, err)
require.Equal(t, []Item{}, items)
}

0 comments on commit 0dc4e3e

Please sign in to comment.