Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ValidateParameter does not apply default arrays correctly in queries #991

Closed
TheSadlig opened this issue Jul 23, 2024 · 0 comments · Fixed by #992 or #1000
Closed

ValidateParameter does not apply default arrays correctly in queries #991

TheSadlig opened this issue Jul 23, 2024 · 0 comments · Fixed by #992 or #1000

Comments

@TheSadlig
Copy link
Contributor

Hi,

When setting default arrays in query parameters, the applied default value is a string, due to the fmt.Sprintf(%v here:
https://github.com/getkin/kin-openapi/blob/master/openapi3filter/validate_request.go#L159

For example, if you default is []string{"A", "B", "C"}
then input.Request.URL.Query()

  • contains []string{"[A B C]"} (note that this is an array with ONE string)
  • but should contain: []string{"A", "B", "C"}

The following unit test reproduces the issues.

var (
	StringArraySchemaWithDefault = &openapi3.SchemaRef{
		Value: &openapi3.Schema{
			Type:    &openapi3.Types{"array"},
			Items:   stringSchema,
			Default: []string{"A", "B", "C"},
		},
	}
)

func TestValidateRequestDefault(t *testing.T) {
	type testCase struct {
		name       string
		param      *openapi3.Parameter
		query      string
		wantQuery  map[string][]string
	}

	testCases := []testCase{
		{
			name: "String Array In Query",
			param: &openapi3.Parameter{
				Name: "param", In: "query", Style: "form", Explode: explode,
				Schema: StringArraySchemaWithDefault,
			},
			wantQuery: map[string][]string{
				"param": {
					"A",
					"B",
					"C",
				},
			},
		},
	}

	for _, tc := range testCases {
		t.Run(tc.name, func(t *testing.T) {
			info := &openapi3.Info{
				Title:   "MyAPI",
				Version: "0.1",
			}
			doc := &openapi3.T{OpenAPI: "3.0.0", Info: info, Paths: openapi3.NewPaths()}
			op := &openapi3.Operation{
				OperationID: "test",
				Parameters:  []*openapi3.ParameterRef{{Value: tc.param}},
				Responses:   openapi3.NewResponses(),
			}
			doc.AddOperation("/test", http.MethodGet, op)
			err := doc.Validate(context.Background())
			require.NoError(t, err)
			router, err := legacyrouter.NewRouter(doc)
			require.NoError(t, err)

			req, err := http.NewRequest(http.MethodGet, "http://test.org/test?"+tc.query, nil)
			route, pathParams, err := router.FindRoute(req)
			require.NoError(t, err)

			input := &RequestValidationInput{Request: req, PathParams: pathParams, Route: route}

			err = ValidateParameter(context.Background(), input, tc.param)
			require.NoError(t, err)

			for k, v := range tc.wantQuery {
				require.Equal(t, v, input.GetQueryParams()[k])
			}
		})
	}
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
1 participant