Skip to content

Commit

Permalink
Merge pull request #527 from lsdch/fix-empty-slice-schema
Browse files Browse the repository at this point in the history
fix: nullable schemas for arrays/slices
  • Loading branch information
danielgtaylor authored Jul 29, 2024
2 parents 4b6a508 + 72a101d commit 9eae273
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 11 deletions.
1 change: 1 addition & 0 deletions schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -746,6 +746,7 @@ func schemaFromType(r Registry, t reflect.Type) *Schema {
s.ContentEncoding = "base64"
} else {
s.Type = TypeArray
s.Nullable = true
s.Items = r.Schema(t.Elem(), true, t.Name()+"Item")

if t.Kind() == reflect.Array {
Expand Down
22 changes: 11 additions & 11 deletions schema_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -204,12 +204,12 @@ func TestSchema(t *testing.T) {
{
name: "array",
input: [2]int{1, 2},
expected: `{"type": "array", "items": {"type": "integer", "format": "int64"}, "minItems": 2, "maxItems": 2}`,
expected: `{"type": ["array", "null"], "items": {"type": "integer", "format": "int64"}, "minItems": 2, "maxItems": 2}`,
},
{
name: "slice",
input: []int{1, 2, 3},
expected: `{"type": "array", "items": {"type": "integer", "format": "int64"}}`,
expected: `{"type": ["array", "null"], "items": {"type": "integer", "format": "int64"}}`,
},
{
name: "map",
Expand Down Expand Up @@ -285,7 +285,7 @@ func TestSchema(t *testing.T) {
"type": "object",
"properties": {
"value": {
"type": "array",
"type": ["array", "null"],
"minItems": 1,
"maxItems": 10,
"uniqueItems": true,
Expand Down Expand Up @@ -343,7 +343,7 @@ func TestSchema(t *testing.T) {
"type": "object",
"properties": {
"value": {
"type": "array",
"type": ["array", "null"],
"items": {
"type": "integer",
"format": "int64",
Expand Down Expand Up @@ -433,7 +433,7 @@ func TestSchema(t *testing.T) {
"type": "object",
"properties": {
"value": {
"type": "array",
"type": ["array", "null"],
"items": {
"type": "string"
},
Expand All @@ -453,7 +453,7 @@ func TestSchema(t *testing.T) {
"type": "object",
"properties": {
"value": {
"type": "array",
"type": ["array", "null"],
"items": {
"type": "integer",
"format": "int64"
Expand Down Expand Up @@ -744,7 +744,7 @@ func TestSchema(t *testing.T) {
},
"maxItems":1,
"minItems":1,
"type":"array"
"type":["array", "null"]
},
"byRef":{
"$ref":"#/components/schemas/RecursiveChildKey"
Expand All @@ -762,7 +762,7 @@ func TestSchema(t *testing.T) {
"items":{
"$ref":"#/components/schemas/RecursiveChildLoop"
},
"type":"array"}
"type":["array", "null"]}
},
"required":["slice","array","map","byValue", "byRef"],
"type":"object"
Expand Down Expand Up @@ -884,7 +884,7 @@ func TestSchema(t *testing.T) {
"additionalProperties":false,
"properties":{
"values":{
"type":"array",
"type":["array", "null"],
"items":{
"type":"string",
"minLength":1,
Expand Down Expand Up @@ -1041,7 +1041,7 @@ func TestSchema(t *testing.T) {
},
"maxItems":4,
"minItems":4,
"type":"array"
"type":["array", "null"]
}
},
"required":["value"],
Expand All @@ -1064,7 +1064,7 @@ func TestSchema(t *testing.T) {
},
"maxItems":4,
"minItems":4,
"type":"array"
"type":["array", "null"]
}
},
"required":["value"],
Expand Down

0 comments on commit 9eae273

Please sign in to comment.