diff --git a/jsonschema/infer.go b/jsonschema/infer.go index d12e556..73d5ad8 100644 --- a/jsonschema/infer.go +++ b/jsonschema/infer.go @@ -216,7 +216,11 @@ func forType(t reflect.Type, seen map[reflect.Type]bool, ignore bool, schemas ma } case reflect.Slice, reflect.Array: - s.Type = "array" + if t.Kind() == reflect.Slice { + s.Types = []string{"null", "array"} + } else { + s.Type = "array" + } s.Items, err = forType(t.Elem(), seen, ignore, schemas) if err != nil { return nil, fmt.Errorf("computing element schema: %v", err) diff --git a/jsonschema/infer_test.go b/jsonschema/infer_test.go index 64a18f4..f6e82d8 100644 --- a/jsonschema/infer_test.go +++ b/jsonschema/infer_test.go @@ -96,6 +96,8 @@ func TestFor(t *testing.T) { {"level", forType[slog.Level](ignore), &schema{Type: "string"}}, {"bigint", forType[big.Int](ignore), &schema{Type: "string"}}, {"bigint", forType[*big.Int](ignore), &schema{Types: []string{"null", "string"}}}, + {"int64slice", forType[[]int64](ignore), &schema{Types: []string{"null", "array"}, Items: &schema{Type: "integer"}}}, + {"int64array", forType[[2]int64](ignore), &schema{Type: "array", Items: &schema{Type: "integer"}, MinItems: jsonschema.Ptr(2), MaxItems: jsonschema.Ptr(2)}}, {"custom", forType[custom](ignore), &schema{Type: "custom"}}, {"intmap", forType[map[string]int](ignore), &schema{ Type: "object", @@ -125,7 +127,7 @@ func TestFor(t *testing.T) { Type: "object", Properties: map[string]*schema{ "f": {Type: "integer", Description: "fdesc"}, - "G": {Type: "array", Items: &schema{Type: "number"}}, + "G": {Types: []string{"null", "array"}, Items: &schema{Type: "number"}}, "P": {Types: []string{"null", "boolean"}, Description: "pdesc"}, "PT": {Types: []string{"null", "string"}}, "NoSkip": {Type: "string"}, diff --git a/jsonschema/validate_test.go b/jsonschema/validate_test.go index 0808f4c..79c3f5e 100644 --- a/jsonschema/validate_test.go +++ b/jsonschema/validate_test.go @@ -494,7 +494,7 @@ func TestStructEmbedding(t *testing.T) { name: "ExportedPointer", targetType: reflect.TypeOf([]Banana{}), wantSchema: &Schema{ - Type: "array", + Types: []string{"null", "array"}, Items: &Schema{ Type: "object", Properties: map[string]*Schema{ @@ -515,7 +515,7 @@ func TestStructEmbedding(t *testing.T) { name: "UnExportedPointer", targetType: reflect.TypeOf([]Durian{}), wantSchema: &Schema{ - Type: "array", + Types: []string{"null", "array"}, Items: &Schema{ Type: "object", Properties: map[string]*Schema{ @@ -536,7 +536,7 @@ func TestStructEmbedding(t *testing.T) { name: "ExportedValue", targetType: reflect.TypeOf([]Fig{}), wantSchema: &Schema{ - Type: "array", + Types: []string{"null", "array"}, Items: &Schema{ Type: "object", Properties: map[string]*Schema{ @@ -557,7 +557,7 @@ func TestStructEmbedding(t *testing.T) { name: "UnExportedValue", targetType: reflect.TypeOf([]Honeyberry{}), wantSchema: &Schema{ - Type: "array", + Types: []string{"null", "array"}, Items: &Schema{ Type: "object", Properties: map[string]*Schema{