|
| 1 | +package openapi3_test |
| 2 | + |
| 3 | +import ( |
| 4 | + "testing" |
| 5 | + |
| 6 | + "github.com/stretchr/testify/require" |
| 7 | + |
| 8 | + "github.com/getkin/kin-openapi/openapi3" |
| 9 | +) |
| 10 | + |
| 11 | +func TestIssue767(t *testing.T) { |
| 12 | + t.Parallel() |
| 13 | + |
| 14 | + tests := [...]struct { |
| 15 | + name string |
| 16 | + schema *openapi3.Schema |
| 17 | + value map[string]interface{} |
| 18 | + opts []openapi3.SchemaValidationOption |
| 19 | + checkErr require.ErrorAssertionFunc |
| 20 | + }{ |
| 21 | + { |
| 22 | + name: "default values disabled should fail with minProps 1", |
| 23 | + schema: openapi3.NewSchema().WithProperties(map[string]*openapi3.Schema{ |
| 24 | + "foo": {Type: "boolean", Default: true}}).WithMinProperties(1), |
| 25 | + value: map[string]interface{}{}, |
| 26 | + opts: []openapi3.SchemaValidationOption{ |
| 27 | + openapi3.VisitAsRequest(), |
| 28 | + }, |
| 29 | + checkErr: require.Error, |
| 30 | + }, |
| 31 | + { |
| 32 | + name: "default values enabled should pass with minProps 1", |
| 33 | + schema: openapi3.NewSchema().WithProperties(map[string]*openapi3.Schema{ |
| 34 | + "foo": {Type: "boolean", Default: true}}).WithMinProperties(1), |
| 35 | + value: map[string]interface{}{}, |
| 36 | + opts: []openapi3.SchemaValidationOption{ |
| 37 | + openapi3.VisitAsRequest(), |
| 38 | + openapi3.DefaultsSet(func() {}), |
| 39 | + }, |
| 40 | + checkErr: require.NoError, |
| 41 | + }, |
| 42 | + { |
| 43 | + name: "default values enabled should pass with minProps 2", |
| 44 | + schema: openapi3.NewSchema().WithProperties(map[string]*openapi3.Schema{ |
| 45 | + "foo": {Type: "boolean", Default: true}, |
| 46 | + "bar": {Type: "boolean"}, |
| 47 | + }).WithMinProperties(2), |
| 48 | + value: map[string]interface{}{"bar": false}, |
| 49 | + opts: []openapi3.SchemaValidationOption{ |
| 50 | + openapi3.VisitAsRequest(), |
| 51 | + openapi3.DefaultsSet(func() {}), |
| 52 | + }, |
| 53 | + checkErr: require.NoError, |
| 54 | + }, |
| 55 | + { |
| 56 | + name: "default values enabled should fail with maxProps 1", |
| 57 | + schema: openapi3.NewSchema().WithProperties(map[string]*openapi3.Schema{ |
| 58 | + "foo": {Type: "boolean", Default: true}, |
| 59 | + "bar": {Type: "boolean"}, |
| 60 | + }).WithMaxProperties(1), |
| 61 | + value: map[string]interface{}{"bar": false}, |
| 62 | + opts: []openapi3.SchemaValidationOption{ |
| 63 | + openapi3.VisitAsRequest(), |
| 64 | + openapi3.DefaultsSet(func() {}), |
| 65 | + }, |
| 66 | + checkErr: require.Error, |
| 67 | + }, |
| 68 | + { |
| 69 | + name: "default values disabled should pass with maxProps 1", |
| 70 | + schema: openapi3.NewSchema().WithProperties(map[string]*openapi3.Schema{ |
| 71 | + "foo": {Type: "boolean", Default: true}, |
| 72 | + "bar": {Type: "boolean"}, |
| 73 | + }).WithMaxProperties(1), |
| 74 | + value: map[string]interface{}{"bar": false}, |
| 75 | + opts: []openapi3.SchemaValidationOption{ |
| 76 | + openapi3.VisitAsRequest(), |
| 77 | + }, |
| 78 | + checkErr: require.NoError, |
| 79 | + }, |
| 80 | + } |
| 81 | + |
| 82 | + for _, test := range tests { |
| 83 | + test := test |
| 84 | + t.Run(test.name, func(t *testing.T) { |
| 85 | + t.Parallel() |
| 86 | + err := test.schema.VisitJSON(test.value, test.opts...) |
| 87 | + test.checkErr(t, err) |
| 88 | + }) |
| 89 | + } |
| 90 | +} |
0 commit comments