Skip to content

Commit 748c325

Browse files
authored
don't record schemas in map during inference (#9)
The intial map of schemas should not be modified during inference. The schemas that get created during the walk may have keywords set, like "description", that should not appear elsewhere in the parent. We don't need to add to the schemas map to detect cycles: we already have `seen` for that.
1 parent 37f539b commit 748c325

File tree

2 files changed

+19
-1
lines changed

2 files changed

+19
-1
lines changed

jsonschema/infer.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -231,7 +231,6 @@ func forType(t reflect.Type, seen map[reflect.Type]bool, ignore bool, schemas ma
231231
s.Types = []string{"null", s.Type}
232232
s.Type = ""
233233
}
234-
schemas[t] = s
235234
return s, nil
236235
}
237236

jsonschema/infer_test.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -338,3 +338,22 @@ func TestForWithCycle(t *testing.T) {
338338
func falseSchema() *jsonschema.Schema {
339339
return &jsonschema.Schema{Not: &jsonschema.Schema{}}
340340
}
341+
342+
func TestDupSchema(t *testing.T) {
343+
// Verify that we don't repeat schema contents, even if we clone the actual schemas.
344+
type args struct {
345+
S string `jsonschema:"str"`
346+
A []string `jsonschema:"arr"`
347+
}
348+
349+
s := forType[args](false)
350+
if g, w := s.Properties["S"].Description, "str"; g != w {
351+
t.Errorf("S: got %q, want %q", g, w)
352+
}
353+
if g, w := s.Properties["A"].Description, "arr"; g != w {
354+
t.Errorf("A: got %q, want %q", g, w)
355+
}
356+
if g, w := s.Properties["A"].Items.Description, ""; g != w {
357+
t.Errorf("A.items: got %q, want %q", g, w)
358+
}
359+
}

0 commit comments

Comments
 (0)