diff --git a/pkg/schema/schema_json.go b/pkg/schema/schema_json.go index 2253923067a0dce2ab47a9e8055e6bc1562fb526..0fe8a50b061cecabef84d358b53751139e6a5b21 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 0000000000000000000000000000000000000000..f9e6be1becbaa336b6aaf6e8ee177042b2cb22ff --- /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) + }) + } +}