Skip to content

Allow null for arrays #48

@rafaeljusto

Description

@rafaeljusto

Describe the bug

In Go, any slice attribute can store nil instead of an allocated value.

s.Type = "array"

Here, it always sets the type as array, where I think it should be ["null","array"].

To Reproduce

package main

import (
	"encoding/json"
	"fmt"
	"os"

	"github.com/google/jsonschema-go/jsonschema"
)

func main() {
	schema, err := jsonschema.For[[]int64](&jsonschema.ForOptions{})
	if err != nil {
		fmt.Fprintf(os.Stderr, "failed to generate JSON schema: %v", err)
		os.Exit(1)
	}
	encoded, err := json.Marshal(schema)
	if err != nil {
		fmt.Fprintf(os.Stderr, "failed to encode JSON schema: %v", err)
		os.Exit(1)
	}
	fmt.Println(string(encoded))
}

Output:

{"type":"array","items":{"type":"integer"}}

Expected behavior
All slices should allow the null type:

{"type":["null","array"],"items":{"type":"integer"}}

Logs
Not required.

Additional context
No extra context required.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions