rapi-doc / RapiDoc

RapiDoc -WebComponent for OpenAPI Spec
https://rapidocweb.com
MIT License
1.71k stars 285 forks source link

Wrong server URL #29

Closed dagnelies closed 5 years ago

dagnelies commented 5 years ago

Hi,

In my swagger definition, I have "host":"localhost:8080".

However, RapiDoc strangely declares that the API server is //api-docs//localhost:8080/

...which causes all "try" requests to fail of course.

AFAIK api-docs is solely the name of the swagger definition file.

mrin9 commented 5 years ago

However, RapiDoc strangely declares that the API server is //api-docs//localhost:8080/

is the server declared as //api-docs//localhost:8080/ or /localhost:8080/api-docs ? What is the location of your swagger spec?

General assumption is, If there is no server provided in the spec, then rapidoc considers the location of the spec as the base-url for making the API calls

the other way to workaround it, if you have not defined a server in your spec then provide it as an attribute in the element

<rapi-doc spec-url="swagger.json" server-url="localhost:8080"> </rapi-doc>

or if you want to dynamically set it

document.addEventListener('DOMContentLoaded', function () {
   document.getElementsByTagName("rapi-doc")[0].setAttribute("server-url", location.origin+"/api")
});
dagnelies commented 5 years ago

Thanks for the prompt response. The use case is pretty common:

Somehow the API server in Rapi becomes //api-docs//localhost:8080/ ...very confusing.


If I add server-url="localhost:8080" to the component, both servers are suggested, which is still undiserable.


More examples:

Rapi Def API server(s)
<rapi-doc spec-url="/v2/api-docs"></rapi-doc> //api-docs//localhost:8080/
<rapi-doc spec-url="http://localhost:8080/v2/api-docs"></rapi-doc> http://localhost:8080//localhost:8080/
<rapi-doc spec-url="/v2/api-docs" server-url="localhost:8080"></rapi-doc> //api-docs//localhost:8080/ and localhost:8080
<rapi-doc spec-url="http://localhost:8080/v2/api-docs" server-url="localhost:8080"></rapi-doc> http://localhost:8080//localhost:8080/ and localhost:8080
dagnelies commented 5 years ago

My guess is that my Swagger Definition does not contain "schemes", and because of that, the converter swagger2openApi somehow fails to provide correct "servers" ...or something of that sort.

See https://github.com/mrin9/RapiDoc/blob/7aaf6f652a383372cde2d99e47e8beb73128bc3c/src/utils/parse-utils.js#L179

...I only have host and basePath

dagnelies commented 5 years ago

I can confirm that when adding schemes in the swagger definition. It would be great if RapiDoc could infer the scheme from the current URL if it is missing ...I think it's an optional thingy anyway.

mrin9 commented 5 years ago

Thanks for the detailed analysis, However, if its behaving the way you mentioned is definitely buggy, can you share your swagger spec, and I will see how best can we answer this issue

dagnelies commented 5 years ago

The swagger spec is 2000 lines, so it's a bit verbose ;) Here is the "top-level":

{  
   "swagger":"2.0",
   "info":{...},
   "host":"localhost:8080",
   "basePath":"/",
   "tags":[...],
   "schemes":["http","https"], <- adding/removing this line make the API server resolution work/break
   "paths":{...},
   "securityDefinitions":{...},
   "definitions":{...}
}
mrin9 commented 5 years ago

Thanks for sharing, I tried to address this issue release 5.0.4, can you confirm if it takes care of your use case

dagnelies commented 5 years ago

Works, thanks for the prompt fix! 👍 🎉