Skip to content

Commit 7ccca52

Browse files
committed
Fix when first item is nullable.
Closes #2
1 parent 3b0c904 commit 7ccca52

File tree

2 files changed

+13
-2
lines changed

2 files changed

+13
-2
lines changed

inferred_schema.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ func (i *InferredSchema) Infer(value any, hints Hints) *InferredSchema {
7575
if i.SchemaType == SchemaTypeNullable {
7676
return &InferredSchema{
7777
SchemaType: SchemaTypeNullable,
78-
Nullable: i,
78+
Nullable: i.Nullable.Infer(value, hints),
7979
}
8080
}
8181

@@ -258,7 +258,6 @@ func (i *InferredSchema) Infer(value any, hints Hints) *InferredSchema {
258258
if subInfer, ok := i.Properties.Required[k]; ok {
259259
i.Properties.Required[k] = subInfer.Infer(v, hints.SubHints(k))
260260
} else if subInfer, ok := i.Properties.Optional[k]; ok {
261-
i.Properties.Optional = ensureMap(i.Properties.Optional)
262261
i.Properties.Optional[k] = subInfer.Infer(v, hints.SubHints(k))
263262
} else {
264263
i.Properties.Optional = ensureMap(i.Properties.Optional)

inferrer_test.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,18 @@ func TestInferString(t *testing.T) {
4343
},
4444
},
4545
},
46+
{
47+
description: "object first is null",
48+
values: []string{`{"name":null}`, `{"name":"Joe"}`},
49+
expectedSchema: Schema{
50+
Properties: map[string]Schema{
51+
"name": {
52+
Type: jtd.TypeString,
53+
Nullable: true,
54+
},
55+
},
56+
},
57+
},
4658
{
4759
description: "array",
4860
values: []string{`[1, 2, 3]`},

0 commit comments

Comments
 (0)