Skip to content

Commit 8334f82

Browse files
committed
jsonschema tags
1 parent 5ebb338 commit 8334f82

File tree

2 files changed

+61
-9
lines changed

2 files changed

+61
-9
lines changed

jsonschema/infer.go

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ package jsonschema
99
import (
1010
"fmt"
1111
"reflect"
12+
"regexp"
1213

1314
"github.com/modelcontextprotocol/go-sdk/internal/util"
1415
)
@@ -36,8 +37,12 @@ import (
3637
// - complex numbers
3738
// - unsafe pointers
3839
//
39-
// The types must not have cycles.
4040
// It will return an error if there is a cycle in the types.
41+
//
42+
// For recognizes struct field tags named "jsonschema".
43+
// A jsonschema tag on a field is used as the description for the corresponding property.
44+
// For future compatibility, descriptions must not start with "WORD=", where WORD is a
45+
// sequence of non-whitespace characters.
4146
func For[T any]() (*Schema, error) {
4247
// TODO: consider skipping incompatible fields, instead of failing.
4348
seen := make(map[reflect.Type]bool)
@@ -126,10 +131,20 @@ func forType(t reflect.Type, seen map[reflect.Type]bool) (*Schema, error) {
126131
if s.Properties == nil {
127132
s.Properties = make(map[string]*Schema)
128133
}
129-
s.Properties[info.Name], err = forType(field.Type, seen)
134+
fs, err := forType(field.Type, seen)
130135
if err != nil {
131136
return nil, err
132137
}
138+
if tag, ok := field.Tag.Lookup("jsonschema"); ok {
139+
if tag == "" {
140+
return nil, fmt.Errorf("empty jsonschema tag on struct field %s.%s", t, field.Name)
141+
}
142+
if disallowedPrefixRegexp.MatchString(tag) {
143+
return nil, fmt.Errorf("tag must not begin with 'WORD=': %q", tag)
144+
}
145+
fs.Description = tag
146+
}
147+
s.Properties[info.Name] = fs
133148
if !info.Settings["omitempty"] && !info.Settings["omitzero"] {
134149
s.Required = append(s.Required, info.Name)
135150
}
@@ -144,3 +159,6 @@ func forType(t reflect.Type, seen map[reflect.Type]bool) (*Schema, error) {
144159
}
145160
return s, nil
146161
}
162+
163+
// Disallow jsonschema tag values beginning "WORD=", for future expansion.
164+
var disallowedPrefixRegexp = regexp.MustCompile("^[^ \t\n]*=")

jsonschema/infer_test.go

Lines changed: 41 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,11 @@ func forType[T any]() *jsonschema.Schema {
2323

2424
func TestFor(t *testing.T) {
2525
type schema = jsonschema.Schema
26+
27+
type S struct {
28+
B int `jsonschema:"bdesc"`
29+
}
30+
2631
tests := []struct {
2732
name string
2833
got *jsonschema.Schema
@@ -45,9 +50,9 @@ func TestFor(t *testing.T) {
4550
{
4651
"struct",
4752
forType[struct {
48-
F int `json:"f"`
53+
F int `json:"f" jsonschema:"fdesc"`
4954
G []float64
50-
P *bool
55+
P *bool `jsonschema:"pdesc"`
5156
Skip string `json:"-"`
5257
NoSkip string `json:",omitempty"`
5358
unexported float64
@@ -56,13 +61,13 @@ func TestFor(t *testing.T) {
5661
&schema{
5762
Type: "object",
5863
Properties: map[string]*schema{
59-
"f": {Type: "integer"},
64+
"f": {Type: "integer", Description: "fdesc"},
6065
"G": {Type: "array", Items: &schema{Type: "number"}},
61-
"P": {Types: []string{"null", "boolean"}},
66+
"P": {Types: []string{"null", "boolean"}, Description: "pdesc"},
6267
"NoSkip": {Type: "string"},
6368
},
6469
Required: []string{"f", "G", "P"},
65-
AdditionalProperties: &jsonschema.Schema{Not: &jsonschema.Schema{}},
70+
AdditionalProperties: falseSchema(),
6671
},
6772
},
6873
{
@@ -75,7 +80,37 @@ func TestFor(t *testing.T) {
7580
"Y": {Type: "integer"},
7681
},
7782
Required: []string{"X", "Y"},
78-
AdditionalProperties: &jsonschema.Schema{Not: &jsonschema.Schema{}},
83+
AdditionalProperties: falseSchema(),
84+
},
85+
},
86+
{
87+
"nested and embedded",
88+
forType[struct {
89+
A S
90+
S
91+
}](),
92+
&schema{
93+
Type: "object",
94+
Properties: map[string]*schema{
95+
"A": {
96+
Type: "object",
97+
Properties: map[string]*schema{
98+
"B": {Type: "integer", Description: "bdesc"},
99+
},
100+
Required: []string{"B"},
101+
AdditionalProperties: falseSchema(),
102+
},
103+
"S": {
104+
Type: "object",
105+
Properties: map[string]*schema{
106+
"B": {Type: "integer", Description: "bdesc"},
107+
},
108+
Required: []string{"B"},
109+
AdditionalProperties: falseSchema(),
110+
},
111+
},
112+
Required: []string{"A", "S"},
113+
AdditionalProperties: falseSchema(),
79114
},
80115
},
81116
}
@@ -205,7 +240,6 @@ func TestForWithCycle(t *testing.T) {
205240
}
206241

207242
for _, test := range tests {
208-
test := test // prevent loop shadowing
209243
t.Run(test.name, func(t *testing.T) {
210244
err := test.fn()
211245
if test.shouldErr && err == nil {

0 commit comments

Comments
 (0)