Skip to content

Commit 46e0df8

Browse files
authored
openapi3filter: use option to skip setting defaults on validation (#708)
1 parent a0b67a0 commit 46e0df8

File tree

2 files changed

+92
-1
lines changed

2 files changed

+92
-1
lines changed

openapi3filter/issue707_test.go

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
package openapi3filter
2+
3+
import (
4+
"net/http"
5+
"strings"
6+
"testing"
7+
8+
"github.com/stretchr/testify/require"
9+
10+
"github.com/getkin/kin-openapi/openapi3"
11+
"github.com/getkin/kin-openapi/routers/gorillamux"
12+
)
13+
14+
func TestIssue707(t *testing.T) {
15+
loader := openapi3.NewLoader()
16+
ctx := loader.Context
17+
spec := `
18+
openapi: 3.0.0
19+
info:
20+
version: 1.0.0
21+
title: Sample API
22+
paths:
23+
/items:
24+
get:
25+
description: Returns a list of stuff
26+
parameters:
27+
- description: parameter with a default value
28+
explode: true
29+
in: query
30+
name: param-with-default
31+
schema:
32+
default: 124
33+
type: integer
34+
style: form
35+
required: false
36+
responses:
37+
'200':
38+
description: Successful response
39+
`[1:]
40+
41+
doc, err := loader.LoadFromData([]byte(spec))
42+
require.NoError(t, err)
43+
44+
err = doc.Validate(ctx)
45+
require.NoError(t, err)
46+
47+
router, err := gorillamux.NewRouter(doc)
48+
require.NoError(t, err)
49+
50+
tests := []struct {
51+
name string
52+
options *Options
53+
expectedQuery string
54+
}{
55+
{
56+
name: "no defaults are added to requests parameters",
57+
options: &Options{
58+
SkipSettingDefaults: true,
59+
},
60+
expectedQuery: "",
61+
},
62+
63+
{
64+
name: "defaults are added to requests",
65+
expectedQuery: "param-with-default=124",
66+
},
67+
}
68+
69+
for _, testcase := range tests {
70+
t.Run(testcase.name, func(t *testing.T) {
71+
httpReq, err := http.NewRequest(http.MethodGet, "/items", strings.NewReader(""))
72+
require.NoError(t, err)
73+
74+
route, pathParams, err := router.FindRoute(httpReq)
75+
require.NoError(t, err)
76+
77+
requestValidationInput := &RequestValidationInput{
78+
Request: httpReq,
79+
PathParams: pathParams,
80+
Route: route,
81+
Options: testcase.options,
82+
}
83+
err = ValidateRequest(ctx, requestValidationInput)
84+
require.NoError(t, err)
85+
86+
require.NoError(t, err)
87+
require.Equal(t, testcase.expectedQuery,
88+
httpReq.URL.RawQuery, "default value must not be included")
89+
})
90+
}
91+
}

openapi3filter/validate_request.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ func ValidateParameter(ctx context.Context, input *RequestValidationInput, param
137137
}
138138

139139
// Set default value if needed
140-
if value == nil && schema != nil && schema.Default != nil {
140+
if !options.SkipSettingDefaults && value == nil && schema != nil && schema.Default != nil {
141141
value = schema.Default
142142
req := input.Request
143143
switch parameter.In {

0 commit comments

Comments
 (0)