Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion jsonschema/infer.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
4 changes: 3 additions & 1 deletion jsonschema/infer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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"},
Expand Down
8 changes: 4 additions & 4 deletions jsonschema/validate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand All @@ -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{
Expand All @@ -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{
Expand All @@ -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{
Expand Down