Skip to content

Commit

Permalink
fix: json marshaling of 'contains' schema
Browse files Browse the repository at this point in the history
  • Loading branch information
RichardKnop committed May 2, 2024
1 parent 780655b commit e5a8e91
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 0 deletions.
18 changes: 18 additions & 0 deletions keywords_array.go
Original file line number Diff line number Diff line change
Expand Up @@ -273,13 +273,31 @@ 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
if err := json.Unmarshal(data, &sch); err != nil {
return err
}
*c = Contains(sch)

return nil
}

Expand Down
1 change: 1 addition & 0 deletions schema_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
6 changes: 6 additions & 0 deletions testdata/coding/contains.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"contains": {
"const": "foobar"
},
"type": "array"
}

0 comments on commit e5a8e91

Please sign in to comment.