oapi-codegen icon indicating copy to clipboard operation
oapi-codegen copied to clipboard

Cannot disable server name validation if the servers array includes a base path.

Open codestation opened this issue 2 years ago • 6 comments

According to #882 , setting swagger.Servers = nil skips the server name matching, but the problem is if for example one has a base path in the server url entry (for example the petstore) it causes that all the routes return no matching operation was found.

In my case this is my server array and a path:

servers:
  - url: 'http://localhost:8000/apis/myapp/v1'
paths:
  "/health/live":
  ...

Calling http://localhost:8000/apis/myapp/v1/health/live fails if Servers is nil. I tried to debug and found that it tries to match /apis/myapp/v1/health/live (the request) with ^/health/live$ (the route).

Maybe doing Servers = nil is a bad idea as it drops critical information (the base path), so another way should be implemented.

codestation avatar Jun 22 '23 21:06 codestation

I have the same issue, if I want to use some prefix on stage server:

servers:
  - url: "https://stg.myserver/api/v1/"

paths:
  /health:
...

I catch 404 if run local and fetch http://localhost:8080/v1/health

evgeniy-krivenko avatar Sep 13 '23 10:09 evgeniy-krivenko

Generally its just better to use the base url in the servers section. Since in case of future versions of the APIs, the paths do change quite often.

Also in case you have a swagger UI, there is clear distinction between the v1 and v2 APIs in the individual paths themselves.

pratyushbarik avatar Oct 06 '23 06:10 pratyushbarik

I fixed it by using this where "/api/v1" is your prefix

swagger.Servers = openapi3.Servers{&openapi3.Server{URL: "/api/v1"}}

aboozaid avatar Oct 15 '23 14:10 aboozaid

swagger.Servers = openapi3.Servers{&openapi3.Server{URL: "/api/v1"}}

Lord and saviour 🙏.

marmiha avatar Jan 29 '24 18:01 marmiha

Maybe this should be added to the documentation somewhere. This wasted quite a bit of my time until I found this thread.

jptrs93 avatar Jun 05 '24 07:06 jptrs93

Maybe this should be added to the documentation somewhere. This wasted quite a bit of my time until I found this thread.

Same.

erazr avatar Jul 02 '24 21:07 erazr