mirror of
https://github.com/bitechdev/ResolveSpec.git
synced 2026-09-29 03:22:00 +00:00
fix(bun): spread variadic args correctly in ColumnExpr
This commit is contained in:
@@ -339,7 +339,7 @@ func (b *BunSelectQuery) Column(columns ...string) common.SelectQuery {
|
|||||||
|
|
||||||
func (b *BunSelectQuery) ColumnExpr(query string, args ...interface{}) common.SelectQuery {
|
func (b *BunSelectQuery) ColumnExpr(query string, args ...interface{}) common.SelectQuery {
|
||||||
if len(args) > 0 {
|
if len(args) > 0 {
|
||||||
b.query = b.query.ColumnExpr(query, args)
|
b.query = b.query.ColumnExpr(query, args...)
|
||||||
} else {
|
} else {
|
||||||
b.query = b.query.ColumnExpr(query)
|
b.query = b.query.ColumnExpr(query)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,31 @@
|
|||||||
|
package database
|
||||||
|
|
||||||
|
import (
|
||||||
|
"strings"
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/stretchr/testify/require"
|
||||||
|
)
|
||||||
|
|
||||||
|
// TestBunSelectQuery_ColumnExpr_SpreadsArgs is a regression test for a bug
|
||||||
|
// where ColumnExpr passed its variadic args slice as a single argument
|
||||||
|
// (b.query.ColumnExpr(query, args) instead of args...), causing bun to
|
||||||
|
// serialize the arg slice itself (e.g. producing `'["{product,cost}"]'`
|
||||||
|
// instead of `'{product,cost}'` for a JSON path parameter).
|
||||||
|
func TestBunSelectQuery_ColumnExpr_SpreadsArgs(t *testing.T) {
|
||||||
|
db := setupBunTestDB(t)
|
||||||
|
defer db.Close()
|
||||||
|
|
||||||
|
adapter := NewBunAdapter(db)
|
||||||
|
|
||||||
|
sq := adapter.NewSelect().
|
||||||
|
Table("test_inserts").
|
||||||
|
ColumnExpr("(jsonvalue #>> ?::text[]) AS jsonvalue_product_cost", "{product,cost}")
|
||||||
|
|
||||||
|
bsq, ok := sq.(*BunSelectQuery)
|
||||||
|
require.True(t, ok, "expected *BunSelectQuery")
|
||||||
|
|
||||||
|
sqlStr := bsq.query.String()
|
||||||
|
require.NotContains(t, sqlStr, `["{product,cost}"]`, "arg slice must not be serialized as a JSON array: %s", sqlStr)
|
||||||
|
require.True(t, strings.Contains(sqlStr, `'{product,cost}'`), "expected the bound text[] literal in SQL: %s", sqlStr)
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user