From 7ccca52210a0b54f7599b862ff5a36297a104037 Mon Sep 17 00:00:00 2001 From: Simon Sawert Date: Wed, 11 Oct 2023 23:22:06 +0200 Subject: [PATCH] Fix when first item is nullable. Closes #2 --- inferred_schema.go | 3 +-- inferrer_test.go | 12 ++++++++++++ 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/inferred_schema.go b/inferred_schema.go index f7a7405..ca3766a 100644 --- a/inferred_schema.go +++ b/inferred_schema.go @@ -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), } } @@ -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) diff --git a/inferrer_test.go b/inferrer_test.go index 91bde3b..bb460dc 100644 --- a/inferrer_test.go +++ b/inferrer_test.go @@ -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]`},