-
Notifications
You must be signed in to change notification settings - Fork 159
Expand file tree
/
Copy pathschemautil_test.go
More file actions
605 lines (585 loc) · 15.5 KB
/
schemautil_test.go
File metadata and controls
605 lines (585 loc) · 15.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
package anthropic
import (
"encoding/json"
"testing"
"github.com/invopop/jsonschema"
orderedmap "github.com/wk8/go-ordered-map/v2"
)
func ptr[T any](v T) *T { return &v }
func props(pairs ...any) *orderedmap.OrderedMap[string, *jsonschema.Schema] {
m := orderedmap.New[string, *jsonschema.Schema]()
for i := 0; i < len(pairs); i += 2 {
m.Set(pairs[i].(string), pairs[i+1].(*jsonschema.Schema))
}
return m
}
// normalizeJSON round-trips JSON through any to normalize key ordering,
// since json.Marshal sorts map keys deterministically.
func normalizeJSON(s string) string {
var v any
if err := json.Unmarshal([]byte(s), &v); err != nil {
return s
}
b, _ := json.Marshal(v)
return string(b)
}
func TestTransformSchema(t *testing.T) {
tests := []struct {
name string
input *jsonschema.Schema
expected string // expected JSON after transform
}{
{
name: "nil schema is a no-op",
input: nil,
expected: "",
},
{
name: "integer with unsupported constraints moves them to description",
input: &jsonschema.Schema{
Type: "integer",
Minimum: "1",
Maximum: "10",
Description: "A number",
},
expected: `{"type":"integer","description":"A number\n\n{maximum: 10, minimum: 1}"}`,
},
{
name: "object gets additionalProperties false and recurses into properties",
input: &jsonschema.Schema{
Type: "object",
Properties: props(
"name", &jsonschema.Schema{Type: "string"},
"age", &jsonschema.Schema{Type: "integer"},
),
Required: []string{"name"},
},
expected: `{"type":"object","properties":{"name":{"type":"string"},"age":{"type":"integer"}},"additionalProperties":false,"required":["name"]}`,
},
{
name: "object without properties gets empty properties",
input: &jsonschema.Schema{
Type: "object",
},
expected: `{"type":"object","properties":{},"additionalProperties":false}`,
},
{
name: "supported string format is preserved",
input: &jsonschema.Schema{
Type: "string",
Format: "date-time",
},
expected: `{"type":"string","format":"date-time"}`,
},
{
name: "unsupported string format moves to description",
input: &jsonschema.Schema{
Type: "string",
Format: "binary",
},
expected: `{"type":"string","description":"{format: binary}"}`,
},
{
name: "array with minItems 1 is preserved",
input: &jsonschema.Schema{
Type: "array",
Items: &jsonschema.Schema{Type: "string"},
MinItems: ptr(uint64(1)),
},
expected: `{"type":"array","items":{"type":"string"},"minItems":1}`,
},
{
name: "array with minItems 5 moves to description",
input: &jsonschema.Schema{
Type: "array",
Items: &jsonschema.Schema{Type: "string"},
MinItems: ptr(uint64(5)),
},
expected: `{"type":"array","items":{"type":"string"},"description":"{minItems: 5}"}`,
},
{
name: "array recurses into items",
input: &jsonschema.Schema{
Type: "array",
Items: &jsonschema.Schema{
Type: "object",
Properties: props(
"id", &jsonschema.Schema{Type: "integer"},
),
},
},
expected: `{"type":"array","items":{"type":"object","properties":{"id":{"type":"integer"}},"additionalProperties":false}}`,
},
{
name: "$ref strips all other properties",
input: &jsonschema.Schema{
Ref: "#/definitions/Person",
Type: "object",
},
expected: `{"$ref":"#/definitions/Person"}`,
},
{
name: "oneOf converted to anyOf",
input: &jsonschema.Schema{
OneOf: []*jsonschema.Schema{
{Type: "string"},
{Type: "number"},
},
},
expected: `{"anyOf":[{"type":"string"},{"type":"number"}]}`,
},
{
name: "anyOf variants are recursively transformed",
input: &jsonschema.Schema{
AnyOf: []*jsonschema.Schema{
{Type: "string"},
{Type: "object", Properties: props("x", &jsonschema.Schema{Type: "integer"})},
},
},
expected: `{"anyOf":[{"type":"string"},{"type":"object","properties":{"x":{"type":"integer"}},"additionalProperties":false}]}`,
},
{
name: "$defs are recursively transformed",
input: &jsonschema.Schema{
Type: "object",
Definitions: jsonschema.Definitions{
"Person": &jsonschema.Schema{
Type: "object",
Properties: props(
"name", &jsonschema.Schema{Type: "string"},
),
},
},
Properties: props(
"user", &jsonschema.Schema{Ref: "#/$defs/Person"},
),
},
expected: `{"type":"object","$defs":{"Person":{"type":"object","properties":{"name":{"type":"string"}},"additionalProperties":false}},"properties":{"user":{"$ref":"#/$defs/Person"}},"additionalProperties":false}`,
},
{
name: "no type and no anyOf clears the schema",
input: &jsonschema.Schema{
Description: "orphan",
},
// Annotation-only schemas are zeroed; a zero jsonschema.Schema
// marshals as `true` (the "match everything" schema).
expected: `true`,
},
{
name: "$schema version is stripped",
input: &jsonschema.Schema{
Version: "https://json-schema.org/draft/2020-12/schema",
Type: "object",
Properties: props(
"name", &jsonschema.Schema{Type: "string"},
),
},
expected: `{"type":"object","properties":{"name":{"type":"string"}},"additionalProperties":false,"description":"{$schema: https://json-schema.org/draft/2020-12/schema}"}`,
},
{
name: "invalid anyOf variants are dropped, not serialized as true",
input: &jsonschema.Schema{
AnyOf: []*jsonschema.Schema{
{Type: "string"},
{Description: "orphan: no type, no anyOf"},
},
},
expected: `{"anyOf":[{"type":"string"}]}`,
},
{
name: "invalid oneOf variants are dropped during conversion to anyOf",
input: &jsonschema.Schema{
OneOf: []*jsonschema.Schema{
{Type: "number"},
{Description: "orphan"},
},
},
expected: `{"anyOf":[{"type":"number"}]}`,
},
{
name: "unsupported allOf field renders as JSON in description",
input: &jsonschema.Schema{
Type: "object",
AllOf: []*jsonschema.Schema{
{Type: "string"},
},
Properties: props("x", &jsonschema.Schema{Type: "integer"}),
},
expected: `{"type":"object","properties":{"x":{"type":"integer"}},"additionalProperties":false,"description":"{allOf: [{\"type\":\"string\"}]}"}`,
},
{
name: "unsupported not field renders as JSON in description",
input: &jsonschema.Schema{
Type: "string",
Not: &jsonschema.Schema{Type: "number"},
},
expected: `{"type":"string","description":"{not: {\"type\":\"number\"}}"}`,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if tt.input == nil {
// Just verify no panic
transformSchema(nil)
return
}
transformSchema(tt.input)
gotBytes, _ := json.Marshal(tt.input)
got := normalizeJSON(string(gotBytes))
want := normalizeJSON(tt.expected)
if got != want {
// Pretty print for readable diffs
var gotAny, wantAny any
json.Unmarshal([]byte(got), &gotAny)
json.Unmarshal([]byte(tt.expected), &wantAny)
gotPretty, _ := json.MarshalIndent(gotAny, "", " ")
wantPretty, _ := json.MarshalIndent(wantAny, "", " ")
t.Errorf("transformSchema() mismatch:\ngot:\n%s\nwant:\n%s", gotPretty, wantPretty)
}
})
}
}
func TestTransformSchemaFromReflector(t *testing.T) {
type OrderItem struct {
Name string `json:"name"`
Quantity int `json:"quantity" jsonschema:"minimum=1"`
Price float64 `json:"price"`
}
type Order struct {
Items []OrderItem `json:"items" jsonschema:"description=List of items"`
Total float64 `json:"total"`
Currency string `json:"currency" jsonschema:"enum=USD,enum=EUR"`
}
reflector := jsonschema.Reflector{DoNotReference: true}
schema := reflector.Reflect(&Order{})
transformSchema(schema)
result, err := json.Marshal(schema)
if err != nil {
t.Fatalf("failed to marshal: %v", err)
}
var m map[string]any
if err := json.Unmarshal(result, &m); err != nil {
t.Fatalf("failed to unmarshal: %v", err)
}
// Top-level object must have additionalProperties: false
if ap, ok := m["additionalProperties"]; !ok || ap != false {
t.Errorf("expected top-level additionalProperties=false, got %v", m["additionalProperties"])
}
// $schema should be stripped (moved to description)
if _, ok := m["$schema"]; ok {
t.Error("expected $schema to be stripped from output")
}
// Properties should exist
props, ok := m["properties"].(map[string]any)
if !ok {
t.Fatal("expected properties map")
}
for _, key := range []string{"items", "total", "currency"} {
if _, ok := props[key]; !ok {
t.Errorf("expected property %q", key)
}
}
// Nested items schema should also have additionalProperties: false
itemsProp, ok := props["items"].(map[string]any)
if !ok {
t.Fatal("expected items property to be a map")
}
itemsSchema, ok := itemsProp["items"].(map[string]any)
if !ok {
t.Fatal("expected items.items (array element schema) to be a map")
}
if ap, ok := itemsSchema["additionalProperties"]; !ok || ap != false {
t.Errorf("expected nested additionalProperties=false, got %v", itemsSchema["additionalProperties"])
}
// Unsupported constraint (minimum=1 on quantity) should be in description
quantitySchema, ok := itemsSchema["properties"].(map[string]any)["quantity"].(map[string]any)
if !ok {
t.Fatal("expected quantity property")
}
desc, _ := quantitySchema["description"].(string)
if desc == "" {
t.Error("expected quantity to have a description with the minimum constraint")
}
}
func TestTransformSchemaMap(t *testing.T) {
tests := []struct {
name string
input map[string]any
expected map[string]any
}{
{
name: "basic integer with unsupported properties",
input: map[string]any{
"type": "integer",
"minimum": 1,
"maximum": 10,
"description": "A number",
},
expected: map[string]any{
"type": "integer",
"description": "A number\n\n{maximum: 10, minimum: 1}",
},
},
{
name: "object with properties",
input: map[string]any{
"type": "object",
"properties": map[string]any{
"name": map[string]any{
"type": "string",
},
"age": map[string]any{
"type": "integer",
},
},
"required": []string{"name"},
"additionalProperties": true,
},
expected: map[string]any{
"type": "object",
"properties": map[string]any{
"name": map[string]any{
"type": "string",
},
"age": map[string]any{
"type": "integer",
},
},
"required": []string{"name"},
"additionalProperties": false,
},
},
{
name: "string with supported format",
input: map[string]any{
"type": "string",
"format": "date-time",
},
expected: map[string]any{
"type": "string",
"format": "date-time",
},
},
{
name: "string with unsupported format",
input: map[string]any{
"type": "string",
"format": "binary",
},
expected: map[string]any{
"type": "string",
"description": "{format: binary}",
},
},
{
name: "array with minItems 1",
input: map[string]any{
"type": "array",
"items": map[string]any{
"type": "string",
},
"minItems": 1,
},
expected: map[string]any{
"type": "array",
"items": map[string]any{
"type": "string",
},
"minItems": 1,
},
},
{
name: "array with minItems 5",
input: map[string]any{
"type": "array",
"items": map[string]any{
"type": "string",
},
"minItems": 5,
},
expected: map[string]any{
"type": "array",
"items": map[string]any{
"type": "string",
},
"description": "{minItems: 5}",
},
},
{
name: "schema with $ref",
input: map[string]any{
"$ref": "#/definitions/Person",
"type": "object",
},
expected: map[string]any{
"$ref": "#/definitions/Person",
},
},
{
name: "schema with anyOf",
input: map[string]any{
"anyOf": []any{
map[string]any{
"type": "string",
},
map[string]any{
"type": "number",
},
},
},
expected: map[string]any{
"anyOf": []any{
map[string]any{
"type": "string",
},
map[string]any{
"type": "number",
},
},
},
},
{
name: "schema with oneOf converted to anyOf",
input: map[string]any{
"oneOf": []any{
map[string]any{
"type": "string",
},
map[string]any{
"type": "number",
},
},
},
expected: map[string]any{
"anyOf": []any{
map[string]any{
"type": "string",
},
map[string]any{
"type": "number",
},
},
},
},
{
name: "schema with $defs",
input: map[string]any{
"type": "object",
"$defs": map[string]any{
"Person": map[string]any{
"type": "object",
"properties": map[string]any{
"name": map[string]any{
"type": "string",
},
},
},
},
"properties": map[string]any{
"user": map[string]any{
"$ref": "#/$defs/Person",
},
},
},
expected: map[string]any{
"type": "object",
"$defs": map[string]any{
"Person": map[string]any{
"type": "object",
"properties": map[string]any{
"name": map[string]any{
"type": "string",
},
},
"additionalProperties": false,
},
},
"properties": map[string]any{
"user": map[string]any{
"$ref": "#/$defs/Person",
},
},
"additionalProperties": false,
},
},
{
name: "nil schema returns nil",
input: nil,
expected: nil,
},
{
name: "schema without type returns nil",
input: map[string]any{
"description": "A schema without type",
},
expected: nil,
},
{
name: "array items false remains false",
input: map[string]any{
"type": "array",
"items": false,
},
expected: map[string]any{
"type": "array",
"items": false,
},
},
{
name: "object property false remains false",
input: map[string]any{
"type": "object",
"properties": map[string]any{
"x": false,
},
},
expected: map[string]any{
"type": "object",
"properties": map[string]any{
"x": false,
},
"additionalProperties": false,
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
result := transformSchemaMap(tt.input)
// Compare via JSON to avoid Go type mismatches (e.g. []string vs []any,
// int vs float64) that arise from the JSON round-trip in transformSchemaMap.
resultJSON, _ := json.Marshal(result)
expectedJSON, _ := json.Marshal(tt.expected)
if string(resultJSON) != string(expectedJSON) {
resultPretty, _ := json.MarshalIndent(result, "", " ")
expectedPretty, _ := json.MarshalIndent(tt.expected, "", " ")
t.Errorf("transformSchemaMap() mismatch:\ngot:\n%s\nwant:\n%s", resultPretty, expectedPretty)
}
})
}
}
func TestStructuredSchemaHelpersPreserveFalseSubschemas(t *testing.T) {
tests := []struct {
name string
got any
expected string
}{
{
name: "BetaJSONSchemaOutputFormat preserves false child schemas",
got: BetaJSONSchemaOutputFormat(map[string]any{"type": "array", "items": false}).Schema,
expected: `{"type":"array","items":false}`,
},
{
name: "BetaToolInputSchema preserves false child schemas",
got: BetaToolInputSchema(map[string]any{"type": "object", "properties": map[string]any{"x": false}}).ExtraFields,
expected: `{"type":"object","properties":{"x":false},"additionalProperties":false}`,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, _ := json.Marshal(tt.got)
if normalizeJSON(string(got)) != normalizeJSON(tt.expected) {
t.Errorf("schema helper mismatch:\ngot:\n%s\nwant:\n%s", got, tt.expected)
}
})
}
}