|
| 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 | +} |
0 commit comments