Skip to content

Commit

Permalink
Fix when first item is nullable.
Browse files Browse the repository at this point in the history
Closes #2
  • Loading branch information
bombsimon committed Oct 11, 2023
1 parent 3b0c904 commit 7ccca52
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 2 deletions.
3 changes: 1 addition & 2 deletions inferred_schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ func (i *InferredSchema) Infer(value any, hints Hints) *InferredSchema {
if i.SchemaType == SchemaTypeNullable {
return &InferredSchema{
SchemaType: SchemaTypeNullable,
Nullable: i,
Nullable: i.Nullable.Infer(value, hints),
}
}

Expand Down Expand Up @@ -258,7 +258,6 @@ func (i *InferredSchema) Infer(value any, hints Hints) *InferredSchema {
if subInfer, ok := i.Properties.Required[k]; ok {
i.Properties.Required[k] = subInfer.Infer(v, hints.SubHints(k))
} else if subInfer, ok := i.Properties.Optional[k]; ok {
i.Properties.Optional = ensureMap(i.Properties.Optional)
i.Properties.Optional[k] = subInfer.Infer(v, hints.SubHints(k))
} else {
i.Properties.Optional = ensureMap(i.Properties.Optional)
Expand Down
12 changes: 12 additions & 0 deletions inferrer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,18 @@ func TestInferString(t *testing.T) {
},
},
},
{
description: "object first is null",
values: []string{`{"name":null}`, `{"name":"Joe"}`},
expectedSchema: Schema{
Properties: map[string]Schema{
"name": {
Type: jtd.TypeString,
Nullable: true,
},
},
},
},
{
description: "array",
values: []string{`[1, 2, 3]`},
Expand Down

0 comments on commit 7ccca52

Please sign in to comment.