pb33f / libopenapi-validator

OpenAPI validation extension for libopenapi, validate http requests and responses as well as schemas
https://pb33f.io/libopenapi/validation/
Other
58 stars 20 forks source link

Problems with escaped path arguments #104

Open arnaldur opened 1 week ago

arnaldur commented 1 week ago
func TestNewValidator_FindPathWithEncodedArg(t *testing.T) {

    spec := `openapi: 3.1.0
paths:
  /something/{string_contains_encoded}:
    put:
      operationId: putSomething
`

    doc, _ := libopenapi.NewDocument([]byte(spec))

    m, _ := doc.BuildV3Model()

    request, _ := http.NewRequest(http.MethodPut, "https://things.com/something/pkg%3Agithub%2Frs%2Fzerolog%40v1.18.0", nil)

    pathItem, errs, _ := FindPath(request, &m.Model)

    assert.Equal(t, 0, len(errs), "Errors found: %v", errs)
    assert.NotNil(t, pathItem)
}

This test currently fails because the encoded part is not handled correctly

Just changing the following line resolves the issue:

--- a/paths/paths.go
+++ b/paths/paths.go
@@ -154,7 +154,7 @@ func StripRequestPath(request *http.Request, document *v3.Document) string {
        basePaths := getBasePaths(document)

        // strip any base path
-       stripped := stripBaseFromPath(request.URL.Path, basePaths)
+       stripped := stripBaseFromPath(request.URL.EscapedPath(), basePaths)
        if request.URL.Fragment != "" {
                stripped = fmt.Sprintf("%s#%s", stripped, request.URL.Fragment)
        }
daveshanley commented 5 days ago

Please feel free to submit a PR! all submissions are welcome. This is a community project.