Skip to content

Commit cb687bf

Browse files
authored
openapi3: fix default values count even when disabled (#767) (#770)
1 parent 47d329d commit cb687bf

File tree

2 files changed

+94
-6
lines changed

2 files changed

+94
-6
lines changed

openapi3/issue767_test.go

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
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+
}

openapi3/schema.go

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1789,12 +1789,10 @@ func (schema *Schema) visitJSONObject(settings *schemaValidationSettings, value
17891789
reqRO := settings.asreq && propSchema.Value.ReadOnly && !settings.readOnlyValidationDisabled
17901790
repWO := settings.asrep && propSchema.Value.WriteOnly && !settings.writeOnlyValidationDisabled
17911791

1792-
if value[propName] == nil {
1793-
if dlft := propSchema.Value.Default; dlft != nil && !reqRO && !repWO {
1794-
value[propName] = dlft
1795-
if f := settings.defaultsSet; f != nil {
1796-
settings.onceSettingDefaults.Do(f)
1797-
}
1792+
if f := settings.defaultsSet; f != nil && value[propName] == nil {
1793+
if dflt := propSchema.Value.Default; dflt != nil && !reqRO && !repWO {
1794+
value[propName] = dflt
1795+
settings.onceSettingDefaults.Do(f)
17981796
}
17991797
}
18001798

0 commit comments

Comments
 (0)