Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 14 additions & 1 deletion jsonschema/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,7 @@ func (s *Schema) MarshalJSON() ([]byte, error) {
if err := s.basicChecks(); err != nil {
return nil, err
}

// Marshal either Type or Types as "type".
var typ any
switch {
Expand All @@ -219,7 +220,19 @@ func (s *Schema) MarshalJSON() ([]byte, error) {
Type: typ,
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 {
Expand Down
6 changes: 3 additions & 3 deletions jsonschema/schema_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,9 @@ func TestJSONRoundTrip(t *testing.T) {
for _, tt := range []struct {
in, want string
}{
{`true`, `{}`}, // boolean schemas become object schemas
{`false`, `{"not":{}}`},
{`{"type":"", "enum":null}`, `{}`}, // empty fields are omitted
{`true`, `true`},
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we want to make both of these changes, or just 'false'?
I've not seen many examples of a schema being 'true', but have seen examples of a schema being 'false'.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems odd to do one without the other.

{`false`, `false`},
{`{"type":"", "enum":null}`, `true`}, // empty fields are omitted
{`{"minimum":1}`, `{"minimum":1}`},
{`{"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
Expand Down
4 changes: 1 addition & 3 deletions mcp/testdata/conformance/server/tools.txtar
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,7 @@ greet
"type": "string"
}
},
"additionalProperties": {
"not": {}
}
"additionalProperties": false
},
"name": "greet"
}
Expand Down