From 36e8b6a965cd0b0a7977340c8eea5c9672f168a3 Mon Sep 17 00:00:00 2001 From: Alex Petraky <petraky@perx.ru> Date: Thu, 8 Aug 2024 17:30:05 +0000 Subject: [PATCH] =?UTF-8?q?fix(core):=20=D0=98=D1=81=D0=BF=D1=80=D0=B0?= =?UTF-8?q?=D0=B2=D0=BB=D0=B5=D0=BD=D0=B0=20=D0=BE=D1=88=D0=B8=D0=B1=D0=BA?= =?UTF-8?q?=D0=B0=20"panic:=20runtime=20error:=20invalid=20memory=20addres?= =?UTF-8?q?s=20or=20nil=20pointer=20dereference"=20=D0=BF=D1=80=D0=B8=20?= =?UTF-8?q?=D0=B2=D1=8B=D0=B7=D0=BE=D0=B2=D0=B5=20UnmarshalJSON=20=D0=BF?= =?UTF-8?q?=D1=80=D0=B8=20=D0=BF=D0=B5=D1=80=D0=B5=D0=B4=D0=B0=D1=87=D0=B5?= =?UTF-8?q?=20"null"?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Close #PRXS-2770 --- pkg/schema/schema_json.go | 5 +++ pkg/schema/schema_json_test.go | 76 ++++++++++++++++++++++++++++++++++ 2 files changed, 81 insertions(+) create mode 100644 pkg/schema/schema_json_test.go diff --git a/pkg/schema/schema_json.go b/pkg/schema/schema_json.go index 22539230..0fe8a50b 100644 --- a/pkg/schema/schema_json.go +++ b/pkg/schema/schema_json.go @@ -29,6 +29,11 @@ func (s *Schema) UnmarshalJSON(b []byte) error { if err := jsoniter.Unmarshal(b, &j); err != nil { return errors.Wrapf(err, "error unmarshal json into field") } + + if j == nil { + return nil + } + s.Loaded = j.Loaded s.Metadata = j.Metadata diff --git a/pkg/schema/schema_json_test.go b/pkg/schema/schema_json_test.go new file mode 100644 index 00000000..f9e6be1b --- /dev/null +++ b/pkg/schema/schema_json_test.go @@ -0,0 +1,76 @@ +package schema + +import ( + "testing" + + "git.perx.ru/perxis/perxis-go/pkg/schema/field" + "github.com/stretchr/testify/assert" +) + +func TestSchema_UnmarshalJSON(t *testing.T) { + s := `{ + "ui": { + "options": { + "fields": [ + "text" + ] + } + }, + "type": "object", + "params": { + "inline": false, + "fields": { + "text": { + "title": "text", + "ui": { + "widget": "StringInput" + }, + "type": "string", + "params": {} + } + } + }, + "loaded": false, + "metadata": null +}` + tests := []struct { + name string + b []byte + want *Schema + wantErr bool + }{ + { + name: "No panic with 'null' value in b", + b: []byte("null"), + want: New(), + wantErr: false, + }, + { + name: "UnmarshalJSON error #1", + b: []byte(""), + want: New(), + wantErr: true, + }, + { + name: "UnmarshalJSON error #2", + b: []byte("abc"), + want: New(), + wantErr: true, + }, + { + name: "OK", + b: []byte(s), + want: New("text", field.String().SetTitle("text").WithUI(&field.UI{Widget: "StringInput"})), + wantErr: false, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + schema := New() + if err := schema.UnmarshalJSON(tt.b); (err != nil) != tt.wantErr { + t.Errorf("UnmarshalJSON() error = %v, wantErr %v", err, tt.wantErr) + } + assert.Equal(t, tt.want, schema) + }) + } +} -- GitLab