(experimental) upgraded to encoding/json/v2

This commit is contained in:
Gani Georgiev committed 2026-08-18 18:16:22 +03:00
1 parent 4d4275c9aa
commit 97dd775455
125 files changed
+433 -364

No files matched your search

+7 -2
View File
@@ -4,7 +4,8 @@ import (
"cmp"
"context"
"database/sql"
"encoding/json"
"encoding/json/jsontext"
"encoding/json/v2"
"errors"
"fmt"
"slices"
@@ -186,7 +187,11 @@ func (app *BaseApp) ImportCollections(toImport []map[string]any, deleteMissing b
)
if err := validator.run(); err != nil {
// serialize the validation error(s)
serializedErr, _ := json.MarshalIndent(err, "", " ")
serializedErr, _ := json.Marshal(
err,
jsontext.WithIndentPrefix(""),
jsontext.WithIndent(" "),
)
return validation.Errors{"collections": validation.NewError(
"validation_collections_import_failure",
+2 -2
View File
@@ -1,7 +1,7 @@
package core_test
import (
"encoding/json"
"encoding/json/v2"
"strings"
"testing"
@@ -449,7 +449,7 @@ func TestImportCollectionsCreateRules(t *testing.T) {
t.Fatal(err)
}
raw, err := json.Marshal(collection)
raw, err := json.Marshal(collection, json.Deterministic(true))
if err != nil {
t.Fatal(err)
}
+10 -9
View File
@@ -1,7 +1,7 @@
package core
import (
"encoding/json"
"encoding/json/v2"
"fmt"
"strconv"
"strings"
@@ -522,8 +522,6 @@ func (m *Collection) unmarshalRawOptions() error {
// For new/"blank" Collection models it replaces the model with a factory
// instance and then unmarshal the provided data one on top of it.
func (m *Collection) UnmarshalJSON(b []byte) error {
type alias *Collection
// initialize the default fields
// (e.g. in case the collection was NOT created using the designated factories)
if m.IsNew() && m.Type == "" {
@@ -540,7 +538,8 @@ func (m *Collection) UnmarshalJSON(b []byte) error {
*m = *blank
}
return json.Unmarshal(b, alias(m))
type alias Collection
return json.Unmarshal(b, (*alias)(m))
}
// MarshalJSON implements the [json.Marshaler] interface.
@@ -550,10 +549,12 @@ func (m *Collection) UnmarshalJSON(b []byte) error {
func (m Collection) MarshalJSON() ([]byte, error) {
switch m.Type {
case CollectionTypeView:
return json.Marshal(struct {
alias := struct {
baseCollection
collectionViewOptions
}{m.baseCollection, m.collectionViewOptions})
}{m.baseCollection, m.collectionViewOptions}
return json.Marshal(alias, json.Deterministic(true))
case CollectionTypeAuth:
alias := struct {
baseCollection
@@ -582,15 +583,15 @@ func (m Collection) MarshalJSON() ([]byte, error) {
alias.OAuth2.Providers = redactedProviders
}
return json.Marshal(alias)
return json.Marshal(alias, json.Deterministic(true))
default:
return json.Marshal(m.baseCollection)
return json.Marshal(m.baseCollection, json.Deterministic(true))
}
}
// String returns a string representation of the current collection.
func (m Collection) String() string {
raw, _ := json.Marshal(m)
raw, _ := m.MarshalJSON()
return string(raw)
}
+3 -3
View File
@@ -2,7 +2,7 @@ package core_test
import (
"bytes"
"encoding/json"
"encoding/json/v2"
"fmt"
"strings"
"testing"
@@ -1016,8 +1016,8 @@ func TestOAuth2ProviderConfigInitProvider(t *testing.T) {
t.Fatalf("Expected PKCE %v, got %v", *s.expectedConfig.PKCE, provider.PKCE())
}
rawMeta, _ := json.Marshal(provider.Extra())
expectedMeta, _ := json.Marshal(s.expectedConfig.Extra)
rawMeta, _ := json.Marshal(provider.Extra(), json.Deterministic(true))
expectedMeta, _ := json.Marshal(s.expectedConfig.Extra, json.Deterministic(true))
if !bytes.Equal(rawMeta, expectedMeta) {
t.Fatalf("Expected PKCE %v, got %v", *s.expectedConfig.PKCE, provider.PKCE())
}
+4 -4
View File
@@ -2,7 +2,7 @@ package core_test
import (
"context"
"encoding/json"
"encoding/json/v2"
"errors"
"fmt"
"slices"
@@ -618,7 +618,7 @@ func TestCollectionUnmarshalJSON(t *testing.T) {
t.Fatal(err)
}
rawResult, err := json.Marshal(collection)
rawResult, err := json.Marshal(collection, json.Deterministic(true))
if err != nil {
t.Fatal(err)
}
@@ -829,7 +829,7 @@ func TestCollectionDBExport(t *testing.T) {
},
{
core.CollectionTypeAuth,
`{"createRule":"1=3","created":"2024-07-01 01:02:03.456Z","deleteRule":"1=5","fields":[{"help":"","hidden":false,"id":"f1_id","name":"f1","presentable":false,"required":false,"system":true,"type":"bool"},{"help":"","hidden":false,"id":"f2_id","name":"f2","presentable":false,"required":true,"system":false,"type":"bool"}],"id":"test_id","indexes":["CREATE INDEX idx1 on test_name(id)","CREATE INDEX idx2 on test_name(id)"],"listRule":"1=1","name":"test_name","options":{"authRule":null,"manageRule":"1=6","authAlert":{"enabled":false,"emailTemplate":{"subject":"","body":""}},"oauth2":{"providers":null,"mappedFields":{"id":"","name":"","username":"","avatarURL":""},"enabled":false},"passwordAuth":{"enabled":false,"identityFields":null},"mfa":{"enabled":false,"duration":0,"rule":""},"otp":{"enabled":false,"duration":0,"length":0,"emailTemplate":{"subject":"","body":""}},"authToken":{"duration":0},"passwordResetToken":{"duration":0},"emailChangeToken":{"duration":0},"verificationToken":{"duration":0},"fileToken":{"duration":0},"verificationTemplate":{"subject":"","body":""},"resetPasswordTemplate":{"subject":"","body":""},"confirmEmailChangeTemplate":{"subject":"","body":""}},"system":true,"type":"auth","updateRule":"1=4","updated":"2024-07-01 01:02:03.456Z","viewRule":"1=7"}`,
`{"createRule":"1=3","created":"2024-07-01 01:02:03.456Z","deleteRule":"1=5","fields":[{"help":"","hidden":false,"id":"f1_id","name":"f1","presentable":false,"required":false,"system":true,"type":"bool"},{"help":"","hidden":false,"id":"f2_id","name":"f2","presentable":false,"required":true,"system":false,"type":"bool"}],"id":"test_id","indexes":["CREATE INDEX idx1 on test_name(id)","CREATE INDEX idx2 on test_name(id)"],"listRule":"1=1","name":"test_name","options":{"authRule":null,"manageRule":"1=6","authAlert":{"enabled":false,"emailTemplate":{"subject":"","body":""}},"oauth2":{"providers":[],"mappedFields":{"id":"","name":"","username":"","avatarURL":""},"enabled":false},"passwordAuth":{"enabled":false,"identityFields":[]},"mfa":{"enabled":false,"duration":0,"rule":""},"otp":{"enabled":false,"duration":0,"length":0,"emailTemplate":{"subject":"","body":""}},"authToken":{"duration":0},"passwordResetToken":{"duration":0},"emailChangeToken":{"duration":0},"verificationToken":{"duration":0},"fileToken":{"duration":0},"verificationTemplate":{"subject":"","body":""},"resetPasswordTemplate":{"subject":"","body":""},"confirmEmailChangeTemplate":{"subject":"","body":""}},"system":true,"type":"auth","updateRule":"1=4","updated":"2024-07-01 01:02:03.456Z","viewRule":"1=7"}`,
},
}
@@ -860,7 +860,7 @@ func TestCollectionDBExport(t *testing.T) {
t.Fatal(err)
}
raw, err := json.Marshal(result)
raw, err := json.Marshal(result, json.Deterministic(true))
if err != nil {
t.Fatal(err)
}
+3 -3
View File
@@ -3,7 +3,7 @@ package core
import (
"bytes"
"database/sql"
"encoding/json"
"encoding/json/v2"
"errors"
"fmt"
"slices"
@@ -324,12 +324,12 @@ func resaveViewsWithChangedFields(app App, excludeIds ...string) error {
f.SetId("")
}
encodedNewFields, err := json.Marshal(newFields)
encodedNewFields, err := json.Marshal(newFields, json.Deterministic(true))
if err != nil {
return err
}
encodedOldFields, err := json.Marshal(oldFields)
encodedOldFields, err := json.Marshal(oldFields, json.Deterministic(true))
if err != nil {
return err
}
+3 -3
View File
@@ -2,7 +2,7 @@ package core_test
import (
"bytes"
"encoding/json"
"encoding/json/v2"
"testing"
"github.com/pocketbase/dbx"
@@ -278,12 +278,12 @@ func TestSingleVsMultipleValuesNormalization(t *testing.T) {
t.Fatalf("Failed to load record: %v", err)
}
encodedResult, err := json.Marshal(result)
encodedResult, err := json.Marshal(result, json.Deterministic(true))
if err != nil {
t.Fatalf("Failed to encode result: %v", err)
}
encodedExpectation, err := json.Marshal(s.expected)
encodedExpectation, err := json.Marshal(s.expected, json.Deterministic(true))
if err != nil {
t.Fatalf("Failed to encode expectation: %v", err)
}
+6 -2
View File
@@ -3,7 +3,7 @@ package core_test
import (
"context"
"database/sql"
"encoding/json"
"encoding/json/v2"
"fmt"
"slices"
"testing"
@@ -120,7 +120,11 @@ func TestTableInfo(t *testing.T) {
t.Run(fmt.Sprintf("%d_%s", i, s.tableName), func(t *testing.T) {
rows, _ := app.TableInfo(s.tableName)
raw, err := json.Marshal(rows)
raw, err := json.Marshal(
rows,
json.Deterministic(true),
json.FormatNilSliceAsNull(true),
)
if err != nil {
t.Fatal(err)
}
+5 -5
View File
@@ -1,7 +1,7 @@
package core_test
import (
"encoding/json"
"encoding/json/v2"
"net/http"
"strings"
"testing"
@@ -183,7 +183,7 @@ func TestRequestEventRequestInfo(t *testing.T) {
t.Fatalf("Failed to resolve request info: %v", err)
}
raw, err := json.Marshal(info)
raw, err := json.Marshal(info, json.Deterministic(true))
if err != nil {
t.Fatalf("Failed to serialize request info: %v", err)
}
@@ -205,7 +205,7 @@ func TestRequestEventRequestInfo(t *testing.T) {
t.Fatalf("Failed to resolve request info: %v", err)
}
raw, err := json.Marshal(info)
raw, err := json.Marshal(info, json.Deterministic(true))
if err != nil {
t.Fatalf("Failed to serialize request info: %v", err)
}
@@ -308,7 +308,7 @@ func TestRequestInfoClone(t *testing.T) {
// check the original data
// ---
originalRaw, err := json.Marshal(info)
originalRaw, err := json.Marshal(info, json.Deterministic(true))
if err != nil {
t.Fatalf("Failed to serialize original request info: %v", err)
}
@@ -321,7 +321,7 @@ func TestRequestInfoClone(t *testing.T) {
// check the clone data
// ---
cloneRaw, err := json.Marshal(clone)
cloneRaw, err := json.Marshal(clone, json.Deterministic(true))
if err != nil {
t.Fatalf("Failed to serialize clone request info: %v", err)
}
+16 -16
View File
@@ -3,7 +3,7 @@ package core_test
import (
"bytes"
"context"
"encoding/json"
"encoding/json/v2"
"errors"
"fmt"
"slices"
@@ -98,7 +98,7 @@ func TestFileFieldPrepareValue(t *testing.T) {
if err != nil {
t.Fatal(err)
}
f1Raw, err := json.Marshal(f1)
f1Raw, err := json.Marshal(f1, json.Deterministic(true))
if err != nil {
t.Fatal(err)
}
@@ -143,7 +143,7 @@ func TestFileFieldPrepareValue(t *testing.T) {
t.Fatal(err)
}
vRaw, err := json.Marshal(v)
vRaw, err := json.Marshal(v, json.Deterministic(true))
if err != nil {
t.Fatal(err)
}
@@ -212,7 +212,7 @@ func TestFileFieldDriverValue(t *testing.T) {
}
}
vRaw, err := json.Marshal(v)
vRaw, err := json.Marshal(v, json.Deterministic(true))
if err != nil {
t.Fatal(err)
}
@@ -699,7 +699,7 @@ func TestFileFieldFindGetter(t *testing.T) {
v := getter(record)
raw, err := json.Marshal(v)
raw, err := json.Marshal(v, json.Deterministic(true))
if err != nil {
t.Fatal(err)
}
@@ -816,7 +816,7 @@ func TestFileFieldFindSetter(t *testing.T) {
setter(record, s.value)
raw, err := json.Marshal(record.Get(s.field.GetName()))
raw, err := json.Marshal(record.Get(s.field.GetName()), json.Deterministic(true))
if err != nil {
t.Fatal(err)
}
@@ -915,8 +915,8 @@ func TestFileFieldIntercept(t *testing.T) {
tests.TestValidationErrors(t, err, []string{"text"})
raw, _ := json.Marshal(record.GetRaw("file_many"))
expectedRaw, _ := json.Marshal([]any{f1.Name, f3})
raw, _ := json.Marshal(record.GetRaw("file_many"), json.Deterministic(true))
expectedRaw, _ := json.Marshal([]any{f1.Name, f3}, json.Deterministic(true))
if !bytes.Equal(expectedRaw, raw) {
t.Fatalf("Expected file field value\n%s\ngot\n%s", expectedRaw, raw)
}
@@ -935,8 +935,8 @@ func TestFileFieldIntercept(t *testing.T) {
t.Fatalf("Expected save to succeed, got %v", err)
}
raw, _ := json.Marshal(record.GetRaw("file_many"))
expectedRaw, _ := json.Marshal([]any{f1.Name, f3.Name})
raw, _ := json.Marshal(record.GetRaw("file_many"), json.Deterministic(true))
expectedRaw, _ := json.Marshal([]any{f1.Name, f3.Name}, json.Deterministic(true))
if !bytes.Equal(expectedRaw, raw) {
t.Fatalf("Expected file field value\n%s\ngot\n%s", expectedRaw, raw)
}
@@ -956,8 +956,8 @@ func TestFileFieldIntercept(t *testing.T) {
t.Fatalf("Expected save to succeed, got %v", err)
}
raw, _ := json.Marshal(record.GetRaw("file_many"))
expectedRaw, _ := json.Marshal([]any{f3.Name, f4.Name})
raw, _ := json.Marshal(record.GetRaw("file_many"), json.Deterministic(true))
expectedRaw, _ := json.Marshal([]any{f3.Name, f4.Name}, json.Deterministic(true))
if !bytes.Equal(expectedRaw, raw) {
t.Fatalf("Expected file field value\n%s\ngot\n%s", expectedRaw, raw)
}
@@ -1067,8 +1067,8 @@ func TestFileFieldInterceptTx(t *testing.T) {
t.Fatalf("Expected save to succeed, got %v", err)
}
raw, _ := json.Marshal(record.GetRaw("file_many"))
expectedRaw, _ := json.Marshal([]any{f1.Name, f3.Name})
raw, _ := json.Marshal(record.GetRaw("file_many"), json.Deterministic(true))
expectedRaw, _ := json.Marshal([]any{f1.Name, f3.Name}, json.Deterministic(true))
if !bytes.Equal(expectedRaw, raw) {
t.Fatalf("Expected file field value\n%s\ngot\n%s", expectedRaw, raw)
}
@@ -1090,8 +1090,8 @@ func TestFileFieldInterceptTx(t *testing.T) {
t.Fatalf("Expected save to succeed, got %v", err)
}
raw, _ := json.Marshal(record.GetRaw("file_many"))
expectedRaw, _ := json.Marshal([]any{f3.Name, f4.Name})
raw, _ := json.Marshal(record.GetRaw("file_many"), json.Deterministic(true))
expectedRaw, _ := json.Marshal([]any{f3.Name, f4.Name}, json.Deterministic(true))
if !bytes.Equal(expectedRaw, raw) {
t.Fatalf("Expected file field value\n%s\ngot\n%s", expectedRaw, raw)
}
+2 -2
View File
@@ -2,7 +2,7 @@ package core_test
import (
"context"
"encoding/json"
"encoding/json/v2"
"fmt"
"testing"
@@ -57,7 +57,7 @@ func TestGeoPointFieldPrepareValue(t *testing.T) {
t.Fatal(err)
}
raw, err := json.Marshal(v)
raw, err := json.Marshal(v, json.Deterministic(true))
if err != nil {
t.Fatal(err)
}
+2 -2
View File
@@ -2,7 +2,7 @@ package core_test
import (
"context"
"encoding/json"
"encoding/json/v2"
"errors"
"fmt"
"strings"
@@ -496,7 +496,7 @@ func TestPasswordFieldFindSetter(t *testing.T) {
setter(record, s.value)
raw, err := json.Marshal(record.Get(s.field.GetName()))
raw, err := json.Marshal(record.Get(s.field.GetName()), json.Deterministic(true))
if err != nil {
t.Fatal(err)
}
+4 -4
View File
@@ -2,7 +2,7 @@ package core_test
import (
"context"
"encoding/json"
"encoding/json/v2"
"fmt"
"testing"
@@ -119,7 +119,7 @@ func TestRelationFieldPrepareValue(t *testing.T) {
t.Fatal(err)
}
vRaw, err := json.Marshal(v)
vRaw, err := json.Marshal(v, json.Deterministic(true))
if err != nil {
t.Fatal(err)
}
@@ -181,7 +181,7 @@ func TestRelationFieldDriverValue(t *testing.T) {
}
}
vRaw, err := json.Marshal(v)
vRaw, err := json.Marshal(v, json.Deterministic(true))
if err != nil {
t.Fatal(err)
}
@@ -590,7 +590,7 @@ func TestRelationFieldFindSetter(t *testing.T) {
setter(record, s.value)
raw, err := json.Marshal(record.Get(s.field.GetName()))
raw, err := json.Marshal(record.Get(s.field.GetName()), json.Deterministic(true))
if err != nil {
t.Fatal(err)
}
+4 -4
View File
@@ -2,7 +2,7 @@ package core_test
import (
"context"
"encoding/json"
"encoding/json/v2"
"fmt"
"testing"
@@ -119,7 +119,7 @@ func TestSelectFieldPrepareValue(t *testing.T) {
t.Fatal(err)
}
vRaw, err := json.Marshal(v)
vRaw, err := json.Marshal(v, json.Deterministic(true))
if err != nil {
t.Fatal(err)
}
@@ -181,7 +181,7 @@ func TestSelectFieldDriverValue(t *testing.T) {
}
}
vRaw, err := json.Marshal(v)
vRaw, err := json.Marshal(v, json.Deterministic(true))
if err != nil {
t.Fatal(err)
}
@@ -503,7 +503,7 @@ func TestSelectFieldFindSetter(t *testing.T) {
setter(record, s.value)
raw, err := json.Marshal(record.Get(s.field.GetName()))
raw, err := json.Marshal(record.Get(s.field.GetName()), json.Deterministic(true))
if err != nil {
t.Fatal(err)
}
+1 -1
View File
@@ -2,7 +2,7 @@ package core_test
import (
"context"
"encoding/json"
"encoding/json/v2"
"reflect"
"strings"
"testing"
+4 -4
View File
@@ -2,7 +2,7 @@ package core
import (
"database/sql/driver"
"encoding/json"
"encoding/json/v2"
"fmt"
"slices"
"strconv"
@@ -277,7 +277,7 @@ func (l *FieldsList) add(pos int, newField Field) {
// String returns the string representation of the current list.
func (l FieldsList) String() string {
v, _ := json.Marshal(l)
v, _ := l.MarshalJSON()
return string(v)
}
@@ -355,12 +355,12 @@ func (l FieldsList) MarshalJSON() ([]byte, error) {
wrapper = append(wrapper, data)
}
return json.Marshal(wrapper)
return json.Marshal(wrapper, json.Deterministic(true))
}
// Value implements the [driver.Valuer] interface.
func (l FieldsList) Value() (driver.Value, error) {
data, err := json.Marshal(l)
data, err := l.MarshalJSON()
return string(data), err
}
+1 -1
View File
@@ -2,7 +2,7 @@ package core_test
import (
"bytes"
"encoding/json"
"encoding/json/v2"
"slices"
"strconv"
"strings"
+2 -2
View File
@@ -1,7 +1,7 @@
package core_test
import (
"encoding/json"
"encoding/json/v2"
"fmt"
"testing"
"time"
@@ -63,7 +63,7 @@ func TestLogsStats(t *testing.T) {
t.Fatal(err)
}
encoded, _ := json.Marshal(result)
encoded, _ := json.Marshal(result, json.Deterministic(true))
if string(encoded) != expected {
t.Fatalf("Expected\n%q\ngot\n%q", expected, string(encoded))
}
+1 -1
View File
@@ -1,7 +1,7 @@
package core_test
import (
"encoding/json"
"encoding/json/v2"
"fmt"
"testing"
"time"
+2 -2
View File
@@ -1,7 +1,7 @@
package core
import (
"encoding/json"
"encoding/json/v2"
"errors"
"fmt"
"slices"
@@ -354,7 +354,7 @@ func (r *RecordFieldResolver) resolveStaticRequestField(path ...string) (*search
// if that doesn't work, try encoding it
if castErr != nil {
encoded, jsonErr := json.Marshal(v)
encoded, jsonErr := json.Marshal(v, json.Deterministic(true))
if jsonErr == nil {
val = string(encoded)
}
+2 -2
View File
@@ -1,7 +1,7 @@
package core
import (
"encoding/json"
"encoding/json/v2"
"errors"
"fmt"
"reflect"
@@ -333,7 +333,7 @@ func (r *runner) processRequestBodyEachModifier(bodyField Field) (*search.Resolv
}
bodyItems := toSlice(r.resolver.requestInfo.Body[bodyField.GetName()])
bodyItemsRaw, err := json.Marshal(bodyItems)
bodyItemsRaw, err := json.Marshal(bodyItems, json.Deterministic(true))
if err != nil {
return nil, fmt.Errorf("cannot serialize the data for field %q", r.activeProps[2])
}
+2 -2
View File
@@ -1,7 +1,7 @@
package core_test
import (
"encoding/json"
"encoding/json/v2"
"regexp"
"slices"
"strings"
@@ -940,7 +940,7 @@ func TestRecordFieldResolverResolveStaticRequestInfoFields(t *testing.T) {
t.Fatalf("Expected parameter r.Identifier %q, got %q", paramName, r.Identifier)
}
encodedParamValue, _ := json.Marshal(paramValue)
encodedParamValue, _ := json.Marshal(paramValue, json.Deterministic(true))
if string(encodedParamValue) != s.expectParamValue {
t.Fatalf("Expected r.Params %#v for %s, got %#v", s.expectParamValue, r.Identifier, string(encodedParamValue))
}
+4 -4
View File
@@ -3,7 +3,7 @@ package core
import (
"bytes"
"context"
"encoding/json"
"encoding/json/v2"
"errors"
"fmt"
"log"
@@ -1218,12 +1218,12 @@ func areValuesEqual(a any, b any) bool {
bv, ok := b.(types.JSONRaw)
return ok && bytes.Equal(av, bv)
default:
aRaw, err := json.Marshal(a)
aRaw, err := json.Marshal(a, json.Deterministic(true))
if err != nil {
return false
}
bRaw, err := json.Marshal(b)
bRaw, err := json.Marshal(b, json.Deterministic(true))
if err != nil {
return false
}
@@ -1324,7 +1324,7 @@ func (record *Record) PublicExport() map[string]any {
//
// Only the data exported by `PublicExport()` will be serialized.
func (m Record) MarshalJSON() ([]byte, error) {
return json.Marshal(m.PublicExport())
return json.Marshal(m.PublicExport(), json.Deterministic(true))
}
// UnmarshalJSON implements the [json.Unmarshaler] interface.
+15 -13
View File
@@ -4,7 +4,7 @@ import (
"bytes"
"context"
"database/sql"
"encoding/json"
"encoding/json/v2"
"errors"
"fmt"
"regexp"
@@ -31,7 +31,7 @@ func TestNewRecord(t *testing.T) {
m := core.NewRecord(collection)
rawData, err := json.Marshal(m.FieldsData()) // should be initialized with the defaults
rawData, err := json.Marshal(m.FieldsData(), json.Deterministic(true)) // should be initialized with the defaults
if err != nil {
t.Fatal(err)
}
@@ -520,7 +520,7 @@ func TestRecordMergeExpand(t *testing.T) {
result := m.Expand()
raw, err := json.Marshal(result)
raw, err := json.Marshal(result, json.Deterministic(true))
if err != nil {
t.Fatal(err)
}
@@ -566,7 +566,7 @@ func TestRecordMergeExpandNilCheck(t *testing.T) {
m := core.NewRecord(collection)
m.MergeExpand(s.expand)
raw, err := json.Marshal(m)
raw, err := json.Marshal(m, json.Deterministic(true))
if err != nil {
t.Fatal(err)
}
@@ -663,7 +663,7 @@ func TestRecordFieldsData(t *testing.T) {
m.Set("field2", 456)
m.Set("unknown", 789)
raw, err := json.Marshal(m.FieldsData())
raw, err := json.Marshal(m.FieldsData(), json.Deterministic(true))
if err != nil {
t.Fatal(err)
}
@@ -691,7 +691,7 @@ func TestRecordCustomData(t *testing.T) {
m.Set("field2", 456)
m.Set("unknown", 789)
raw, err := json.Marshal(m.CustomData())
raw, err := json.Marshal(m.CustomData(), json.Deterministic(true))
if err != nil {
t.Fatal(err)
}
@@ -1103,14 +1103,16 @@ func TestRecordGetUnsavedFiles(t *testing.T) {
t.Run(fmt.Sprintf("%d_%#v", i, s.key), func(t *testing.T) {
v := record.GetUnsavedFiles(s.key)
raw, err := json.Marshal(v)
raw, err := json.Marshal(v,
json.Deterministic(true),
json.FormatNilSliceAsNull(true),
)
if err != nil {
t.Fatal(err)
}
rawStr := string(raw)
if rawStr != s.expected {
t.Fatalf("Expected\n%s\ngot\n%s", s.expected, rawStr)
if str := string(raw); str != s.expected {
t.Fatalf("Expected\n%s\ngot\n%s", s.expected, str)
}
})
}
@@ -1164,7 +1166,7 @@ func TestRecordUnmarshalJSONField(t *testing.T) {
t.Fatalf("Expected hasErr %v, got %v", s.expectError, hasErr)
}
raw, _ := json.Marshal(s.destination)
raw, _ := json.Marshal(s.destination, json.Deterministic(true))
if v := string(raw); v != s.expectedJSON {
t.Fatalf("Expected %q, got %q", s.expectedJSON, v)
}
@@ -1271,7 +1273,7 @@ func TestRecordDBExport(t *testing.T) {
t.Fatal(err)
}
raw, err := json.Marshal(result)
raw, err := json.Marshal(result, json.Deterministic(true))
if err != nil {
t.Fatal(err)
}
@@ -1515,7 +1517,7 @@ func TestRecordPublicExportAndMarshalJSON(t *testing.T) {
m.Unhide(s.unhideFields...)
m.Hide(s.hideFields...)
exportResult, err := json.Marshal(m.PublicExport())
exportResult, err := json.Marshal(m.PublicExport(), json.Deterministic(true))
if err != nil {
t.Fatal(err)
}
+3 -3
View File
@@ -3,7 +3,7 @@ package core_test
import (
"context"
"database/sql"
"encoding/json"
"encoding/json/v2"
"errors"
"strings"
"testing"
@@ -240,7 +240,7 @@ func TestExpandRecords(t *testing.T) {
t.Errorf("Expected %d failures, got %d\n%v", s.expectExpandFailures, len(failed), failed)
}
encoded, _ := json.Marshal(records)
encoded, _ := json.Marshal(records, json.Deterministic(true))
encodedStr := string(encoded)
totalExpandProps := strings.Count(encodedStr, `"`+core.FieldNameExpand+`":`)
totalEmptyExpands := strings.Count(encodedStr, `"`+core.FieldNameExpand+`":{}`)
@@ -420,7 +420,7 @@ func TestExpandRecord(t *testing.T) {
t.Errorf("Expected %d failures, got %d\n%v", s.expectExpandFailures, len(failed), failed)
}
encoded, _ := json.Marshal(record)
encoded, _ := json.Marshal(record, json.Deterministic(true))
encodedStr := string(encoded)
totalExpandProps := strings.Count(encodedStr, `"`+core.FieldNameExpand+`":`)
totalEmptyExpands := strings.Count(encodedStr, `"`+core.FieldNameExpand+`":{}`)
+3 -3
View File
@@ -1,7 +1,7 @@
package core_test
import (
"encoding/json"
"encoding/json/v2"
"errors"
"fmt"
"slices"
@@ -107,7 +107,7 @@ func TestRecordQueryOne(t *testing.T) {
t.Fatal(err)
}
raw, err := json.Marshal(s.model)
raw, err := json.Marshal(s.model, json.Deterministic(true))
if err != nil {
t.Fatal(err)
}
@@ -192,7 +192,7 @@ func TestRecordQueryAll(t *testing.T) {
t.Fatal(err)
}
raw, err := json.Marshal(s.result)
raw, err := json.Marshal(s.result, json.Deterministic(true))
if err != nil {
t.Fatal(err)
}
+4 -4
View File
@@ -2,7 +2,7 @@ package core
import (
"context"
"encoding/json"
"encoding/json/v2"
"errors"
"fmt"
"os"
@@ -237,7 +237,7 @@ func (s *Settings) String() string {
s.mu.RLock()
defer s.mu.RUnlock()
raw, _ := json.Marshal(s)
raw, _ := s.MarshalJSON()
return string(raw)
}
@@ -264,7 +264,7 @@ func (s *Settings) DBExport(app App) (map[string]any, error) {
s.settings.SuperuserIPs = []string{}
}
encoded, err := json.Marshal(s.settings)
encoded, err := json.Marshal(s.settings, json.Deterministic(true))
if err != nil {
return nil, err
}
@@ -361,7 +361,7 @@ func (s *Settings) MarshalJSON() ([]byte, error) {
copy.SuperuserIPs = []string{}
}
return json.Marshal(copy)
return json.Marshal(copy, json.Deterministic(true))
}
// -------------------------------------------------------------------
+7 -7
View File
@@ -1,7 +1,7 @@
package core_test
import (
"encoding/json"
"encoding/json/v2"
"fmt"
"os"
"strings"
@@ -112,12 +112,12 @@ func TestSettingsMerge(t *testing.T) {
t.Fatal(err)
}
s1Encoded, err := json.Marshal(s1)
s1Encoded, err := json.Marshal(s1, json.Deterministic(true))
if err != nil {
t.Fatal(err)
}
s2Encoded, err := json.Marshal(s2)
s2Encoded, err := json.Marshal(s2, json.Deterministic(true))
if err != nil {
t.Fatal(err)
}
@@ -138,12 +138,12 @@ func TestSettingsClone(t *testing.T) {
t.Fatal(err)
}
s1Bytes, err := json.Marshal(s1)
s1Bytes, err := json.Marshal(s1, json.Deterministic(true))
if err != nil {
t.Fatal(err)
}
s2Bytes, err := json.Marshal(s2)
s2Bytes, err := json.Marshal(s2, json.Deterministic(true))
if err != nil {
t.Fatal(err)
}
@@ -174,7 +174,7 @@ func TestSettingsMarshalJSON(t *testing.T) {
settings.S3.Secret = testSecret
settings.Backups.S3.Secret = testSecret
raw, err := json.Marshal(settings)
raw, err := json.Marshal(settings, json.Deterministic(true))
if err != nil {
t.Fatal(err)
}
@@ -228,7 +228,7 @@ func TestSettingsValidate(t *testing.T) {
`"rateLimits":{`,
}
errBytes, _ := json.Marshal(err)
errBytes, _ := json.Marshal(err, json.Deterministic(true))
jsonErr := string(errBytes)
for _, expected := range expectations {
if !strings.Contains(jsonErr, expected) {
+1 -1
View File
@@ -2,7 +2,7 @@ package core
import (
"database/sql"
"encoding/json"
"encoding/json/v2"
"errors"
"fmt"
"os"
+3 -3
View File
@@ -1,7 +1,7 @@
package core_test
import (
"encoding/json"
"encoding/json/v2"
"fmt"
"slices"
"testing"
@@ -530,7 +530,7 @@ func TestCreateViewFields(t *testing.T) {
}
if len(s.expectFields) != len(result) {
serialized, _ := json.Marshal(result)
serialized, _ := json.Marshal(result, json.Deterministic(true))
t.Fatalf("Expected %d fields, got %d: \n%s", len(s.expectFields), len(result), serialized)
}
@@ -835,7 +835,7 @@ func TestDryRunView(t *testing.T) {
// check fields
// ---
if len(s.expectFields) != len(result.Fields) {
serialized, _ := json.Marshal(result.Fields)
serialized, _ := json.Marshal(result.Fields, json.Deterministic(true))
t.Fatalf("Expected %d fields, got %d: \n%s", len(s.expectFields), len(result.Fields), serialized)
}
for name, typ := range s.expectFields {