outofcoffee / imposter

Scriptable, multipurpose mock server. Run standalone mock servers, or embed mocks within your tests.
https://imposter.sh
Other
364 stars 59 forks source link

Path parameter placeholder matching with OpenAPI plugin #632

Open movinfinex opened 2 days ago

movinfinex commented 2 days ago

Hi, thanks for the useful testing tool.

I'm trying to reproduce something like the example in the docs here: https://docs.imposter.sh/openapi_plugin/#overriding-status-code

Here's a short config that tries to match requests using a placeholder in the path:

---
plugin: openapi
specFile: https://raw.githubusercontent.com/outofcoffee/imposter/refs/heads/main/mock/openapi/src/test/resources/openapi2/simple/petstore-expanded.yaml
basePath: /api

resources:
  - path: "/pets/{petId}"
    method: get
    response:
      statusCode: 404

However, it doesn't match requests, and I get a response generated from the schema instead:

$ http get http://localhost:8080/api/pets/123
HTTP/1.1 200 OK
Content-Type: application/json
Server: imposter
X-Imposter-Request: feb84f5b-ee6b-49e4-a428-2e6414e3be06
content-length: 49

{
    "id": 1,
    "name": "Fluffy",
    "tag": "cat"
}

The {petId} syntax is taken from the example code, but the docs below it actually say

The path property supports placeholders, using the Vert.x Web colon format

So I tried path: "/pets/:petId", but that doesn't match, either.

If I use path: "/pets/123", then the path matches and I get the 404, but that isn't parameterised, of course.

outofcoffee commented 1 day ago

Hi @movinfinex, as the spec also sets a basePath of /api, you can remove it from your config file. It may be duplicating the path to be /api/api/pets/{petId}.

If this doesn't help, please could you share the log output from startup?

movinfinex commented 20 hours ago

Okay, I did some more experimentation. The problem occurs when the placeholder name in the config file doesn't match the placeholder name in the OpenAPI schema. In the example above, the schema uses {id} and the config uses {petId}. My original real-world config had a similar issue. When I make the placeholder names match, everything works as expected.

FWIW, I was able to reproduce it with minimal configs with and without basePath usage.