jsonschema: implement JSON Pointers for schemas
Implement enough of the JSON Pointer spec to suffice for JSON Schema. For details, see the comment at the top of json_pointer.go. Change-Id: Ic297f1a1a08903b4b3c6666fe57ac1f45c96c0f7 Reviewed-on: https://go-review.googlesource.com/c/tools/+/670535 Reviewed-by: Alan Donovan <adonovan@google.com> LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
This commit is contained in:
parent
435f5447df
commit
9031106300
1 changed files with 228 additions and 0 deletions
78
jsonschema/json_pointer_test.go
Normal file
78
jsonschema/json_pointer_test.go
Normal file
|
|
@ -0,0 +1,78 @@
|
|||
// Copyright 2025 The Go MCP SDK Authors. All rights reserved.
|
||||
// Use of this source code is governed by an MIT-style
|
||||
// license that can be found in the LICENSE file.
|
||||
|
||||
package jsonschema
|
||||
|
||||
import (
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestDereferenceJSONPointer(t *testing.T) {
|
||||
s := &Schema{
|
||||
AllOf: []*Schema{{}, {}},
|
||||
Defs: map[string]*Schema{
|
||||
"": {Properties: map[string]*Schema{"": {}}},
|
||||
"A": {},
|
||||
"B": {
|
||||
Defs: map[string]*Schema{
|
||||
"X": {},
|
||||
"Y": {},
|
||||
},
|
||||
},
|
||||
"/~": {},
|
||||
"~1": {},
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range []struct {
|
||||
ptr string
|
||||
want any
|
||||
}{
|
||||
{"", s},
|
||||
{"/$defs/A", s.Defs["A"]},
|
||||
{"/$defs/B", s.Defs["B"]},
|
||||
{"/$defs/B/$defs/X", s.Defs["B"].Defs["X"]},
|
||||
{"/$defs//properties/", s.Defs[""].Properties[""]},
|
||||
{"/allOf/1", s.AllOf[1]},
|
||||
{"/$defs/~1~0", s.Defs["/~"]},
|
||||
{"/$defs/~01", s.Defs["~1"]},
|
||||
} {
|
||||
got, err := dereferenceJSONPointer(s, tt.ptr)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if got != tt.want {
|
||||
t.Errorf("%s:\ngot %+v\nwant %+v", tt.ptr, got, tt.want)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestDerefernceJSONPointerErrors(t *testing.T) {
|
||||
s := &Schema{
|
||||
Type: "t",
|
||||
Items: &Schema{},
|
||||
Required: []string{"a"},
|
||||
}
|
||||
for _, tt := range []struct {
|
||||
ptr string
|
||||
want string // error must contain this string
|
||||
}{
|
||||
{"x", "does not begin"}, // parse error: no initial '/'
|
||||
{"/minItems", "does not refer to a schema"},
|
||||
{"/minItems/x", "navigated to nil"},
|
||||
{"/required/-", "not supported"},
|
||||
{"/required/01", "leading zeroes"},
|
||||
{"/required/x", "invalid int"},
|
||||
{"/required/1", "out of bounds"},
|
||||
{"/properties/x", "no key"},
|
||||
} {
|
||||
_, err := dereferenceJSONPointer(s, tt.ptr)
|
||||
if err == nil {
|
||||
t.Errorf("%q: succeeded, want failure", tt.ptr)
|
||||
} else if !strings.Contains(err.Error(), tt.want) {
|
||||
t.Errorf("%q: error is %q, which does not contain %q", tt.ptr, err, tt.want)
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Reference in a new issue