From 074eecbb7ef5eb7761befda8781a8d189fdd5c9f Mon Sep 17 00:00:00 2001 From: Richard Knop Date: Thu, 2 May 2024 14:42:52 +0100 Subject: [PATCH] fix: json marshaling of 'contains' schema --- keywords_array.go | 17 +++++++++++++++++ schema_test.go | 1 + testdata/coding/contains.json | 6 ++++++ 3 files changed, 24 insertions(+) create mode 100644 testdata/coding/contains.json diff --git a/keywords_array.go b/keywords_array.go index 60badaa..c93067a 100644 --- a/keywords_array.go +++ b/keywords_array.go @@ -273,6 +273,23 @@ func (c Contains) JSONChildren() (res map[string]JSONPather) { return Schema(c).JSONChildren() } +// MarshalJSON implements the json.Marshaler interface for Schema +func (c *Contains) MarshalJSON() ([]byte, error) { + switch c.schemaType { + case schemaTypeFalse: + return []byte("false"), nil + case schemaTypeTrue: + return []byte("true"), nil + default: + obj := map[string]interface{}{} + + for k, v := range c.keywords { + obj[k] = v + } + return json.Marshal(obj) + } +} + // UnmarshalJSON implements the json.Unmarshaler interface for Contains func (c *Contains) UnmarshalJSON(data []byte) error { var sch Schema diff --git a/schema_test.go b/schema_test.go index e2fb452..95e0f13 100644 --- a/schema_test.go +++ b/schema_test.go @@ -617,6 +617,7 @@ func TestJSONCoding(t *testing.T) { "testdata/coding/numeric.json", "testdata/coding/objects.json", "testdata/coding/strings.json", + "testdata/coding/contains.json", } for i, c := range cases { diff --git a/testdata/coding/contains.json b/testdata/coding/contains.json new file mode 100644 index 0000000..0561b81 --- /dev/null +++ b/testdata/coding/contains.json @@ -0,0 +1,6 @@ +{ + "contains": { + "const": "foobar" + }, + "type": "array" +} \ No newline at end of file