Skip to content

Commit 255e8d8

Browse files
committed
Actually fix #624, thanks to @orensolo
Signed-off-by: Pierre Fenoll <pierrefenoll@gmail.com>
1 parent e887ba8 commit 255e8d8

File tree

4 files changed

+37
-23
lines changed

4 files changed

+37
-23
lines changed

openapi3filter/issue624_test.go

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -48,17 +48,22 @@ paths:
4848

4949
router, err := gorillamux.NewRouter(doc)
5050
require.NoError(t, err)
51-
httpReq, err := http.NewRequest(http.MethodGet, `/items?test=test1`, nil)
52-
require.NoError(t, err)
5351

54-
route, pathParams, err := router.FindRoute(httpReq)
55-
require.NoError(t, err)
52+
for _, testcase := range []string{`test1`, `test[1`} {
53+
t.Run(testcase, func(t *testing.T) {
54+
httpReq, err := http.NewRequest(http.MethodGet, `/items?test=`+testcase, nil)
55+
require.NoError(t, err)
5656

57-
requestValidationInput := &RequestValidationInput{
58-
Request: httpReq,
59-
PathParams: pathParams,
60-
Route: route,
57+
route, pathParams, err := router.FindRoute(httpReq)
58+
require.NoError(t, err)
59+
60+
requestValidationInput := &RequestValidationInput{
61+
Request: httpReq,
62+
PathParams: pathParams,
63+
Route: route,
64+
}
65+
err = ValidateRequest(ctx, requestValidationInput)
66+
require.NoError(t, err)
67+
})
6168
}
62-
err = ValidateRequest(ctx, requestValidationInput)
63-
require.NoError(t, err)
6469
}

openapi3filter/req_resp_decoder.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -227,7 +227,7 @@ type valueDecoder interface {
227227
// decodeStyledParameter returns a value of an operation's parameter from HTTP request for
228228
// parameters defined using the style format, and whether the parameter is supplied in the input.
229229
// The function returns ParseError when HTTP request contains an invalid value of a parameter.
230-
func decodeStyledParameter(param *openapi3.Parameter, input *RequestValidationInput) (interface{}, bool, error) {
230+
func decodeStyledParameter(param *openapi3.Parameter, input *RequestValidationInput, schema *openapi3.SchemaRef) (interface{}, bool, error) {
231231
sm, err := param.SerializationMethod()
232232
if err != nil {
233233
return nil, false, err
@@ -253,7 +253,7 @@ func decodeStyledParameter(param *openapi3.Parameter, input *RequestValidationIn
253253
return nil, false, fmt.Errorf("unsupported parameter's 'in': %s", param.In)
254254
}
255255

256-
return decodeValue(dec, param.Name, sm, param.Schema, param.Required)
256+
return decodeValue(dec, param.Name, sm, schema, param.Required)
257257
}
258258

259259
func decodeValue(dec valueDecoder, param string, sm *openapi3.SerializationMethod, schema *openapi3.SchemaRef, required bool) (interface{}, bool, error) {

openapi3filter/validate_request.go

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -129,20 +129,29 @@ func ValidateParameter(ctx context.Context, input *RequestValidationInput, param
129129
var value interface{}
130130
var err error
131131
var found bool
132-
var schema *openapi3.Schema
132+
var schemaRef *openapi3.SchemaRef
133133

134134
// Validation will ensure that we either have content or schema.
135-
if parameter.Content != nil {
136-
if value, schema, found, err = decodeContentParameter(parameter, input); err != nil {
137-
return &RequestError{Input: input, Parameter: parameter, Err: err}
138-
}
135+
if parameter.Content == nil {
136+
schemaRef = parameter.Schema
137+
} else if len(parameter.Content) != 1 {
138+
// We only know how to decode a parameter if it has one content, application/json
139+
err = fmt.Errorf("multiple content types for parameter %q", parameter.Name)
140+
return &RequestError{Input: input, Parameter: parameter, Err: err}
139141
} else {
140-
if value, found, err = decodeStyledParameter(parameter, input); err != nil {
142+
mt := parameter.Content.Get("application/json")
143+
if mt == nil {
144+
err = fmt.Errorf("parameter %q has no content schema", parameter.Name)
141145
return &RequestError{Input: input, Parameter: parameter, Err: err}
142146
}
143-
schema = parameter.Schema.Value
147+
schemaRef = mt.Schema
148+
}
149+
150+
if value, found, err = decodeStyledParameter(parameter, input, schemaRef); err != nil {
151+
return &RequestError{Input: input, Parameter: parameter, Err: err}
144152
}
145153

154+
schema := schemaRef.Value
146155
// Set default value if needed
147156
if value == nil && schema != nil && schema.Default != nil {
148157
value = schema.Default

openapi3filter/validation_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -328,15 +328,15 @@ func TestFilter(t *testing.T) {
328328
// enough.
329329
req = ExampleRequest{
330330
Method: "POST",
331-
URL: "http://example.com/api/prefix/v/suffix?contentArg={\"name\":\"bob\", \"id\":\"a\"}",
331+
URL: `http://example.com/api/prefix/v/suffix?contentArg={"name":"bob", "id":"a"}`,
332332
}
333333
err = expect(req, resp)
334334
require.NoError(t, err)
335335

336336
// Now it should fail due the ID being too long
337337
req = ExampleRequest{
338338
Method: "POST",
339-
URL: "http://example.com/api/prefix/v/suffix?contentArg={\"name\":\"bob\", \"id\":\"EXCEEDS_MAX_LENGTH\"}",
339+
URL: `http://example.com/api/prefix/v/suffix?contentArg={"name":"bob", "id":"EXCEEDS_MAX_LENGTH"}`,
340340
}
341341
err = expect(req, resp)
342342
require.IsType(t, &RequestError{}, err)
@@ -351,15 +351,15 @@ func TestFilter(t *testing.T) {
351351

352352
req = ExampleRequest{
353353
Method: "POST",
354-
URL: "http://example.com/api/prefix/v/suffix?contentArg2={\"name\":\"bob\", \"id\":\"a\"}",
354+
URL: `http://example.com/api/prefix/v/suffix?contentArg2={"name":"bob", "id":"a"}`,
355355
}
356356
err = expectWithDecoder(req, resp, customDecoder)
357357
require.NoError(t, err)
358358

359359
// Now it should fail due the ID being too long
360360
req = ExampleRequest{
361361
Method: "POST",
362-
URL: "http://example.com/api/prefix/v/suffix?contentArg2={\"name\":\"bob\", \"id\":\"EXCEEDS_MAX_LENGTH\"}",
362+
URL: `http://example.com/api/prefix/v/suffix?contentArg2={"name":"bob", "id":"EXCEEDS_MAX_LENGTH"}`,
363363
}
364364
err = expectWithDecoder(req, resp, customDecoder)
365365
require.IsType(t, &RequestError{}, err)

0 commit comments

Comments
 (0)