jsonschema: marshal to bools (#247)
Marshal the empty schema to true, and its negation to false. This is technically a breaking behavior change, but a properly written consumer of JSON Schema will not notice. For #244. Fixes #230.
This commit is contained in:
parent
bb9087d252
commit
c132621eb0
3 changed files with 18 additions and 7 deletions
|
|
@ -204,6 +204,7 @@ func (s *Schema) MarshalJSON() ([]byte, error) {
|
||||||
if err := s.basicChecks(); err != nil {
|
if err := s.basicChecks(); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
// Marshal either Type or Types as "type".
|
// Marshal either Type or Types as "type".
|
||||||
var typ any
|
var typ any
|
||||||
switch {
|
switch {
|
||||||
|
|
@ -219,7 +220,19 @@ func (s *Schema) MarshalJSON() ([]byte, error) {
|
||||||
Type: typ,
|
Type: typ,
|
||||||
schemaWithoutMethods: (*schemaWithoutMethods)(s),
|
schemaWithoutMethods: (*schemaWithoutMethods)(s),
|
||||||
}
|
}
|
||||||
return marshalStructWithMap(&ms, "Extra")
|
bs, err := marshalStructWithMap(&ms, "Extra")
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
// Marshal {} as true and {"not": {}} as false.
|
||||||
|
// It is wasteful to do this here instead of earlier, but much easier.
|
||||||
|
switch {
|
||||||
|
case bytes.Equal(bs, []byte(`{}`)):
|
||||||
|
bs = []byte("true")
|
||||||
|
case bytes.Equal(bs, []byte(`{"not":true}`)):
|
||||||
|
bs = []byte("false")
|
||||||
|
}
|
||||||
|
return bs, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *Schema) UnmarshalJSON(data []byte) error {
|
func (s *Schema) UnmarshalJSON(data []byte) error {
|
||||||
|
|
|
||||||
|
|
@ -54,9 +54,9 @@ func TestJSONRoundTrip(t *testing.T) {
|
||||||
for _, tt := range []struct {
|
for _, tt := range []struct {
|
||||||
in, want string
|
in, want string
|
||||||
}{
|
}{
|
||||||
{`true`, `{}`}, // boolean schemas become object schemas
|
{`true`, `true`},
|
||||||
{`false`, `{"not":{}}`},
|
{`false`, `false`},
|
||||||
{`{"type":"", "enum":null}`, `{}`}, // empty fields are omitted
|
{`{"type":"", "enum":null}`, `true`}, // empty fields are omitted
|
||||||
{`{"minimum":1}`, `{"minimum":1}`},
|
{`{"minimum":1}`, `{"minimum":1}`},
|
||||||
{`{"minimum":1.0}`, `{"minimum":1}`}, // floating-point integers lose their fractional part
|
{`{"minimum":1.0}`, `{"minimum":1}`}, // floating-point integers lose their fractional part
|
||||||
{`{"minLength":1.0}`, `{"minLength":1}`}, // some floats are unmarshaled into ints, but you can't tell
|
{`{"minLength":1.0}`, `{"minLength":1}`}, // some floats are unmarshaled into ints, but you can't tell
|
||||||
|
|
|
||||||
4
mcp/testdata/conformance/server/tools.txtar
vendored
4
mcp/testdata/conformance/server/tools.txtar
vendored
|
|
@ -59,9 +59,7 @@ greet
|
||||||
"type": "string"
|
"type": "string"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"additionalProperties": {
|
"additionalProperties": false
|
||||||
"not": {}
|
|
||||||
}
|
|
||||||
},
|
},
|
||||||
"name": "greet"
|
"name": "greet"
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue