MCPcopy Index your code
hub / github.com/oapi-codegen/oapi-codegen / TestSortParamsByPath

Function TestSortParamsByPath

pkg/codegen/utils_test.go:479–519  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

477}
478
479func TestSortParamsByPath(t *testing.T) {
480 strSchema := &openapi3.Schema{Type: &openapi3.Types{"string"}}
481
482 t.Run("reorders params to match path order", func(t *testing.T) {
483 params := []ParameterDefinition{
484 {ParamName: "b", In: "path", Spec: &openapi3.Parameter{Name: "b", Schema: &openapi3.SchemaRef{Value: strSchema}}},
485 {ParamName: "a", In: "path", Spec: &openapi3.Parameter{Name: "a", Schema: &openapi3.SchemaRef{Value: strSchema}}},
486 }
487 sorted, err := SortParamsByPath("/foo/{a}/bar/{b}", params)
488 require.NoError(t, err)
489 require.Len(t, sorted, 2)
490 assert.Equal(t, "a", sorted[0].ParamName)
491 assert.Equal(t, "b", sorted[1].ParamName)
492 })
493
494 t.Run("errors on missing parameter", func(t *testing.T) {
495 params := []ParameterDefinition{
496 {ParamName: "a", In: "path", Spec: &openapi3.Parameter{Name: "a", Schema: &openapi3.SchemaRef{Value: strSchema}}},
497 }
498 _, err := SortParamsByPath("/foo/{a}/bar/{b}", params)
499 assert.Error(t, err)
500 })
501
502 t.Run("handles duplicate path parameters", func(t *testing.T) {
503 // This is the Keycloak-style path where {client-uuid} appears twice.
504 // The spec only declares 3 unique parameters.
505 params := []ParameterDefinition{
506 {ParamName: "realm", In: "path", Spec: &openapi3.Parameter{Name: "realm", Schema: &openapi3.SchemaRef{Value: strSchema}}},
507 {ParamName: "client-uuid", In: "path", Spec: &openapi3.Parameter{Name: "client-uuid", Schema: &openapi3.SchemaRef{Value: strSchema}}},
508 {ParamName: "role-name", In: "path", Spec: &openapi3.Parameter{Name: "role-name", Schema: &openapi3.SchemaRef{Value: strSchema}}},
509 }
510 path := "/admin/realms/{realm}/clients/{client-uuid}/roles/{role-name}/composites/clients/{client-uuid}"
511 sorted, err := SortParamsByPath(path, params)
512 require.NoError(t, err)
513 // Should return 3 unique params in first-occurrence order
514 require.Len(t, sorted, 3)
515 assert.Equal(t, "realm", sorted[0].ParamName)
516 assert.Equal(t, "client-uuid", sorted[1].ParamName)
517 assert.Equal(t, "role-name", sorted[2].ParamName)
518 })
519}
520
521func TestReplacePathParamsWithStr(t *testing.T) {
522 result := ReplacePathParamsWithStr("/path/{param1}/{.param2}/{;param3*}/foo")

Callers

nothing calls this directly

Calls 4

SortParamsByPathFunction · 0.85
RunMethod · 0.80
LenMethod · 0.45
ErrorMethod · 0.45

Tested by

no test coverage detected