Skip to content

Commit e5a8e91

Browse files
committed
fix: json marshaling of 'contains' schema
1 parent 780655b commit e5a8e91

File tree

3 files changed

+25
-0
lines changed

3 files changed

+25
-0
lines changed

keywords_array.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -273,13 +273,31 @@ func (c Contains) JSONChildren() (res map[string]JSONPather) {
273273
return Schema(c).JSONChildren()
274274
}
275275

276+
// MarshalJSON implements the json.Marshaler interface for Schema
277+
func (c *Contains) MarshalJSON() ([]byte, error) {
278+
switch c.schemaType {
279+
case schemaTypeFalse:
280+
return []byte("false"), nil
281+
case schemaTypeTrue:
282+
return []byte("true"), nil
283+
default:
284+
obj := map[string]interface{}{}
285+
286+
for k, v := range c.keywords {
287+
obj[k] = v
288+
}
289+
return json.Marshal(obj)
290+
}
291+
}
292+
276293
// UnmarshalJSON implements the json.Unmarshaler interface for Contains
277294
func (c *Contains) UnmarshalJSON(data []byte) error {
278295
var sch Schema
279296
if err := json.Unmarshal(data, &sch); err != nil {
280297
return err
281298
}
282299
*c = Contains(sch)
300+
283301
return nil
284302
}
285303

schema_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -617,6 +617,7 @@ func TestJSONCoding(t *testing.T) {
617617
"testdata/coding/numeric.json",
618618
"testdata/coding/objects.json",
619619
"testdata/coding/strings.json",
620+
"testdata/coding/contains.json",
620621
}
621622

622623
for i, c := range cases {

testdata/coding/contains.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"contains": {
3+
"const": "foobar"
4+
},
5+
"type": "array"
6+
}

0 commit comments

Comments
 (0)