-
-
Notifications
You must be signed in to change notification settings - Fork 435
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: default values count even if disabled (#767)
- Loading branch information
Showing
2 changed files
with
94 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
package openapi3_test | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
|
||
"github.com/getkin/kin-openapi/openapi3" | ||
) | ||
|
||
func TestIssue767(t *testing.T) { | ||
t.Parallel() | ||
|
||
tests := [...]struct { | ||
name string | ||
schema *openapi3.Schema | ||
value map[string]interface{} | ||
opts []openapi3.SchemaValidationOption | ||
checkErr require.ErrorAssertionFunc | ||
}{ | ||
{ | ||
name: "default values disabled should fail with minProps 1", | ||
schema: openapi3.NewSchema().WithProperties(map[string]*openapi3.Schema{ | ||
"foo": {Type: "boolean", Default: true}}).WithMinProperties(1), | ||
value: map[string]interface{}{}, | ||
opts: []openapi3.SchemaValidationOption{ | ||
openapi3.VisitAsRequest(), | ||
}, | ||
checkErr: require.Error, | ||
}, | ||
{ | ||
name: "default values enabled should pass with minProps 1", | ||
schema: openapi3.NewSchema().WithProperties(map[string]*openapi3.Schema{ | ||
"foo": {Type: "boolean", Default: true}}).WithMinProperties(1), | ||
value: map[string]interface{}{}, | ||
opts: []openapi3.SchemaValidationOption{ | ||
openapi3.VisitAsRequest(), | ||
openapi3.DefaultsSet(func() {}), | ||
}, | ||
checkErr: require.NoError, | ||
}, | ||
{ | ||
name: "default values enabled should pass with minProps 2", | ||
schema: openapi3.NewSchema().WithProperties(map[string]*openapi3.Schema{ | ||
"foo": {Type: "boolean", Default: true}, | ||
"bar": {Type: "boolean"}, | ||
}).WithMinProperties(2), | ||
value: map[string]interface{}{"bar": false}, | ||
opts: []openapi3.SchemaValidationOption{ | ||
openapi3.VisitAsRequest(), | ||
openapi3.DefaultsSet(func() {}), | ||
}, | ||
checkErr: require.NoError, | ||
}, | ||
{ | ||
name: "default values enabled should fail with maxProps 1", | ||
schema: openapi3.NewSchema().WithProperties(map[string]*openapi3.Schema{ | ||
"foo": {Type: "boolean", Default: true}, | ||
"bar": {Type: "boolean"}, | ||
}).WithMaxProperties(1), | ||
value: map[string]interface{}{"bar": false}, | ||
opts: []openapi3.SchemaValidationOption{ | ||
openapi3.VisitAsRequest(), | ||
openapi3.DefaultsSet(func() {}), | ||
}, | ||
checkErr: require.Error, | ||
}, | ||
{ | ||
name: "default values disabled should pass with maxProps 1", | ||
schema: openapi3.NewSchema().WithProperties(map[string]*openapi3.Schema{ | ||
"foo": {Type: "boolean", Default: true}, | ||
"bar": {Type: "boolean"}, | ||
}).WithMaxProperties(1), | ||
value: map[string]interface{}{"bar": false}, | ||
opts: []openapi3.SchemaValidationOption{ | ||
openapi3.VisitAsRequest(), | ||
}, | ||
checkErr: require.NoError, | ||
}, | ||
} | ||
|
||
for _, test := range tests { | ||
test := test | ||
t.Run(test.name, func(t *testing.T) { | ||
t.Parallel() | ||
err := test.schema.VisitJSON(test.value, test.opts...) | ||
test.checkErr(t, err) | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters