jsonchema: support additional keywords

Support additional (extra) keywords beyond those specified in the
`Schema` type. The current behavior is additional keywords are dropped.

To follow up:

- There are similar util functions in `mcp/util.go` which don't seem to
be used. Maybe we should move it to `internal/util` instead of
`jsonchema/util.go` package?
- The method `[Un]MarshalJSON` is only on `pointer receiver`, is this
intended? (that means it will only work if we [un]marshal pointer to the
`Schema` instance)
- The fix is using the [un]marshalStructWithMap from `mcp/util.go` and
there are requirements for the function to work properly but these are
not strictly checked e.g. need `json:"-"` tag for the Extra field.

Fixes #69.
This commit is contained in:
Albert Sundjaja 2025-07-01 21:02:26 +10:00 • committed by GitHub
commit bfb9215d16
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -8,7 +8,11 @@ import (
"encoding/json"
"hash/maphash"
"reflect"
"strings"
"testing"
"github.com/google/go-cmp/cmp"
"github.com/google/go-cmp/cmp/cmpopts"
)
func TestEqual(t *testing.T) {
@ -125,3 +129,58 @@ func TestHash(t *testing.T) {
}
_ = hash(null)
}
func TestMarshalStructWithMap(t *testing.T) {
type S struct {
A int
B string `json:"b,omitempty"`
u bool
M map[string]any `json:"-"`
}
t.Run("basic", func(t *testing.T) {
s := S{A: 1, B: "two", M: map[string]any{"!@#": true}}
got, err := marshalStructWithMap(&s, "M")
if err != nil {
t.Fatal(err)
}
want := `{"A":1,"b":"two","!@#":true}`
if g := string(got); g != want {
t.Errorf("\ngot %s\nwant %s", g, want)
}
var un S
if err := unmarshalStructWithMap(got, &un, "M"); err != nil {
t.Fatal(err)
}
if diff := cmp.Diff(s, un, cmpopts.IgnoreUnexported(S{})); diff != "" {
t.Errorf("mismatch (-want, +got):\n%s", diff)
}
})
t.Run("duplicate", func(t *testing.T) {
s := S{A: 1, B: "two", M: map[string]any{"b": "dup"}}
_, err := marshalStructWithMap(&s, "M")
if err == nil || !strings.Contains(err.Error(), "duplicate") {
t.Errorf("got %v, want error with 'duplicate'", err)
}
})
t.Run("embedded", func(t *testing.T) {
type Embedded struct {
A int
B int
Extra map[string]any `json:"-"`
}
type S struct {
C int
Embedded
}
s := S{C: 1, Embedded: Embedded{A: 2, B: 3, Extra: map[string]any{"d": 4, "e": 5}}}
got, err := marshalStructWithMap(&s, "Extra")
if err != nil {
t.Fatal(err)
}
want := `{"C":1,"A":2,"B":3,"d":4,"e":5}`
if g := string(got); g != want {
t.Errorf("got %v, want %v", g, want)
}
})
}