Skip to content

Commit

Permalink
tests
Browse files Browse the repository at this point in the history
  • Loading branch information
candiduslynx committed Feb 20, 2024
1 parent 8e357fa commit be77f3d
Show file tree
Hide file tree
Showing 5 changed files with 232 additions and 3 deletions.
63 changes: 63 additions & 0 deletions fixtures/nullable_from_type_disabled.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
{
"$schema": "https://json-schema.org/draft/2020-12/schema",
"$id": "https://github.com/invopop/jsonschema/nullable-with-odd-fields",
"$ref": "#/$defs/NullableWithOddFields",
"$defs": {
"NullableWithOddFields": {
"properties": {
"simple_map": {
"additionalProperties": {
"type": "string"
},
"type": "object"
},
"map_with_nullable_values": {
"additionalProperties": {
"type": "string"
},
"type": "object"
},
"int_slice": {
"items": {
"type": "integer"
},
"type": "array"
},
"pointer_to_int_slice": {
"items": {
"type": "integer"
},
"type": "array"
},
"int_slice_with_nullable_values": {
"items": {
"type": "integer"
},
"type": "array"
},
"pointer_to_int_slice_with_nullable_values": {
"items": {
"type": "integer"
},
"type": "array"
},
"chan": true,
"unsafe_pointer": true,
"reader": true
},
"additionalProperties": false,
"type": "object",
"required": [
"simple_map",
"map_with_nullable_values",
"int_slice",
"pointer_to_int_slice",
"int_slice_with_nullable_values",
"pointer_to_int_slice_with_nullable_values",
"chan",
"unsafe_pointer",
"reader"
]
}
}
}
140 changes: 140 additions & 0 deletions fixtures/nullable_from_type_enabled.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
{
"$schema": "https://json-schema.org/draft/2020-12/schema",
"$id": "https://github.com/invopop/jsonschema/nullable-with-odd-fields",
"$ref": "#/$defs/NullableWithOddFields",
"$defs": {
"NullableWithOddFields": {
"properties": {
"simple_map": {
"oneOf": [
{
"additionalProperties": {
"type": "string"
},
"type": "object"
},
{
"type": "null"
}
]
},
"map_with_nullable_values": {
"oneOf": [
{
"additionalProperties": {
"oneOf": [
{
"type": "string"
},
{
"type": "null"
}
]
},
"type": "object"
},
{
"type": "null"
}
]
},
"int_slice": {
"oneOf": [
{
"items": {
"type": "integer"
},
"type": "array"
},
{
"type": "null"
}
]
},
"pointer_to_int_slice": {
"oneOf": [
{
"items": {
"type": "integer"
},
"type": "array"
},
{
"type": "null"
}
]
},
"int_slice_with_nullable_values": {
"oneOf": [
{
"items": {
"oneOf": [
{
"type": "integer"
},
{
"type": "null"
}
]
},
"type": "array"
},
{
"type": "null"
}
]
},
"pointer_to_int_slice_with_nullable_values": {
"oneOf": [
{
"items": {
"oneOf": [
{
"type": "integer"
},
{
"type": "null"
}
]
},
"type": "array"
},
{
"type": "null"
}
]
},
"chan": true,
"unsafe_pointer": {
"oneOf": [
true,
{
"type": "null"
}
]
},
"reader": {
"oneOf": [
true,
{
"type": "null"
}
]
}
},
"additionalProperties": false,
"type": "object",
"required": [
"simple_map",
"map_with_nullable_values",
"int_slice",
"pointer_to_int_slice",
"int_slice_with_nullable_values",
"pointer_to_int_slice_with_nullable_values",
"chan",
"unsafe_pointer",
"reader"
]
}
}
}
2 changes: 1 addition & 1 deletion reflect.go
Original file line number Diff line number Diff line change
Expand Up @@ -338,7 +338,7 @@ func (r *Reflector) reflectTypeToSchema(definitions Definitions, t reflect.Type)
case reflect.Map:
r.reflectMap(definitions, t, st)

case reflect.Interface:
case reflect.Interface, reflect.Chan, reflect.UnsafePointer:
// empty

case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64,
Expand Down
23 changes: 23 additions & 0 deletions reflect_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"encoding/json"
"flag"
"fmt"
"io"
"net"
"net/url"
"os"
Expand All @@ -12,6 +13,7 @@ import (
"strings"
"testing"
"time"
"unsafe"

"github.com/invopop/jsonschema/examples"

Expand Down Expand Up @@ -658,3 +660,24 @@ func TestJSONSchemaAlias(t *testing.T) {
compareSchemaOutput(t, "fixtures/schema_alias.json", r, &AliasObjectB{})
compareSchemaOutput(t, "fixtures/schema_alias_2.json", r, &AliasObjectC{})
}

type NullableWithOddFields struct {
SimpleMap map[string]string `json:"simple_map"`
MapWithNullableValues map[string]*string `json:"map_with_nullable_values"`

IntSlice []int `json:"int_slice"`
PointerToIntSlice *[]int `json:"pointer_to_int_slice"`
IntSliceWithNullableValues []*int `json:"int_slice_with_nullable_values"`
PointerToIntSliceWithNullableValues *[]*int `json:"pointer_to_int_slice_with_nullable_values"`

Chan chan int `json:"chan"`
UnsafePointer unsafe.Pointer `json:"unsafe_pointer"`
Reader io.Reader `json:"reader"`
}

func TestNullableFromType(t *testing.T) {
r := &Reflector{}
compareSchemaOutput(t, "fixtures/nullable_from_type_disabled.json", r, &NullableWithOddFields{})
r.NullableFromType = true
compareSchemaOutput(t, "fixtures/nullable_from_type_enabled.json", r, &NullableWithOddFields{})
}
7 changes: 5 additions & 2 deletions schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,11 +90,14 @@ var (

// MakeNullable will replace the schema that either matches the schema or `null` value:
// The resulting schema is wrapped via `oneOf` feature.
// This will be performed if the schema isn't already nullable (as `oneOf` is used).
//
// {"oneOf":[s,{"type":"null"}]}
func (t *Schema) MakeNullable() {
sc := *t
*t = Schema{OneOf: []*Schema{&sc, {Type: "null"}}}
if !t.IsNullable() {
sc := *t
*t = Schema{OneOf: []*Schema{&sc, {Type: "null"}}}
}
}

// IsNullable will test if the Schema is nullable in terms of MakeNullable
Expand Down

0 comments on commit be77f3d

Please sign in to comment.