Files
zitadel/backend/v3/storage/database/repository/array_test.go
T
SilvanandGitHub f89aa05e92 fix: correctly unmarshal login names and metadata in user repository (#11775)
## Problem description

When calling `repository.UserRepository().Get(...)` the following error
got returned caused by scanning.

`Scan error: scanning: doing scan: scanFn: scany: scan row into struct
fields: sql: Scan error on column index 7, name "metadata": unsupported
Scan, storing driver.Value type []uint8 into type *[]*domain.Metadata`

## How the problem got solved

The issue was that json arrays were not properly unmarshalled during
scanning the rows into structs.

## Additional changes

The issue could also happen on login names, therefore it got fixed for
this field too. Other fields are not affected because we use
`json.RawMessage` for the specific user types.

## Additional information

Introduced in https://github.com/zitadel/zitadel/pull/10967
2026-03-06 07:56:37 +01:00

118 lines
1.9 KiB
Go

package repository_test
import (
"testing"
"github.com/stretchr/testify/require"
"github.com/zitadel/zitadel/backend/v3/storage/database"
"github.com/zitadel/zitadel/backend/v3/storage/database/repository"
)
type testObject struct {
ID string `json:"id"`
Number int `json:"number"`
Active bool `json:"active"`
}
func TestJSONArray_Scan(t *testing.T) {
type want struct {
err error
res []*testObject
}
tests := []struct {
name string
src any
want want
}{
{
name: "nil",
src: nil,
want: want{
err: nil,
res: nil,
},
},
{
name: "[]byte with data",
src: []byte(`[{"id":"1","number":1,"active":true}]`),
want: want{
err: nil,
res: []*testObject{{ID: "1", Number: 1, Active: true}},
},
},
{
name: "[]byte without data",
src: []byte(`[]`),
want: want{
err: nil,
res: nil,
},
},
{
name: "nil []byte",
src: []byte(nil),
want: want{
err: nil,
res: nil,
},
},
{
name: "empty []byte",
src: []byte{},
want: want{
err: nil,
res: nil,
},
},
{
name: "string with data",
src: `[{"id":"1","number":1,"active":true}]`,
want: want{
err: nil,
res: []*testObject{{ID: "1", Number: 1, Active: true}},
},
},
{
name: "string without data",
src: string(`[]`),
want: want{
err: nil,
res: nil,
},
},
{
name: "empty string",
src: "",
want: want{
err: nil,
res: nil,
},
},
{
name: "wrong type",
src: []int{1, 2, 3},
want: want{
err: repository.ErrScanSource,
res: nil,
},
},
{
name: "badly formatted JSON",
src: []byte(`this is not a JSON`),
want: want{
err: new(database.ScanError),
res: nil,
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
var a repository.JSONArray[*testObject]
gotErr := a.Scan(tt.src)
require.ErrorIs(t, gotErr, tt.want.err)
require.Len(t, a, len(tt.want.res))
})
}
}