oatpp / oatpp-swagger

OpenApi 3.0.0 docs + Swagger UI for oatpp services
https://oatpp.io/
Apache License 2.0
95 stars 54 forks source link

No mapping for HTTP-method #63

Open EDDragonWolf opened 2 years ago

EDDragonWolf commented 2 years ago

Hi. I'm continuing to investigate the Swagger integration and found as I think a bug with endpoint mapping but I'm not sure if it's the right scope to discuss it.

I added the following code:

As result, my endpoint is available by the path http://127.0.0.1:8000/test. But when I try to make a request from the Swagger UI, it's sent to the http://127.0.0.1:8000/api/v2.0/test and returns the following error:

{
  "error": "No mapping for HTTP-method: 'GET', URL: '/api/v2.0/test'",
  "success": false
}

If I change the definition of my endpoint to ENDPOINT("GET", "/api/v2.0/test", test), it doesn't help since Swagger UI tries to send a request to http://127.0.0.1:8000/api/v2.0/api/v2.0/test.

So my API server works, but Swagger UI is broken.

Is it possible to add proper mapping to my API Controller? So my endpoint will use the test path but will be accessible by the api/v2/0/test path.

EDDragonWolf commented 2 years ago

Hi. I'm not sure if you had a chance to look at this issue. I have tried to solve it from my side. Please take a look at this commit https://github.com/EDDragonWolf/oatpp/commit/45180e8d0b6d25495b36d2559692ed57be64496f.

In short, I have added the possibility to register endpoints with a specific base path. So in the definition, the endpoint is still defined as ENDPOINT("GET", "test", test), but when I'm adding it to the router, I can do next:

docEndpoints.append(router->addController(controller, "/api/v2.0")->getEndpoints());

If you think it's a good solution, I can prepare PR. Or could you propose a better solution?

lganzzzo commented 2 years ago

Hello @EDDragonWolf ,

Thank you for your question.

I see this issue as follows:

The solution to this issue I believe should be fixing oatpp-swagger module, so that it can understand the "servers" parameter correctly removing the base-path part from the endpoint description in swagger-ui

Regards, Leonid

EDDragonWolf commented 2 years ago

Hello @lganzzzo,

You are right, it isn't exactly an issue of the main module, but from my point of view, it's a flexible way to handle the multiple servers. It allows registering the same ApiController for each supported base path explicitly.

Removing the base-path part from the endpoint declaration will work only for one server. To support multiple servers, you will need to declare the same endpoint several times. For example:

ENDPOINT("GET", "/staging/test", test_staging)
ENDPOINT("GET", "/dev/test", test_dev)

Or maybe I understood your idea wrong?

I think the base path should be provided dynamically during the registration.

With my changes, I also fixed another issue - the possibility of hosting a separate Swagger UI page for each version.

I hope you'll consider my thoughts and fix this issue.

Regards, Vitalius