swagger-ui icon indicating copy to clipboard operation
swagger-ui copied to clipboard

Protocol is not selectable

Open who opened this issue 11 years ago • 10 comments

When schemes is set to ["http","https"], Swagger UI does not show UI for a protocol selector and instead uses http.

Repro steps

  1. Open Swagger-ui with a spec containing multiple schemes.
  2. Expand the operations
  3. Click "Try it out"
  4. Observe that the scheme is always http.

Here's a spec for reproducing the issue:

{
    "swagger": "2.0",
    "info": {
        "version": "0.0.0",
        "title": "Simple API"
    },
    "schemes": [
        "http",
        "https"
    ],
    "paths": {
        "/": {
            "get": {
                "responses": {
                    "200": {
                        "description": "OK"
                    }
                }
            }
        }
    }
}

I have also created a gist of the above spec.

Side note: Swagger editor handles it correctly

who avatar May 07 '15 19:05 who

Any idea when is this planned?

rgogulakrishnan avatar Sep 02 '15 17:09 rgogulakrishnan

@FoxxMD @webron @rgogulakrishnan was this issue resolved in v2.1.3? I want to know if I should go ahead and download it.... my site is hosted on both https and http, and my schemes are [http,https] but swagger-ui uses http even when the host scheme is https, ofcourse the browser prevents the call to be made, so no output....

michaelukpong avatar Nov 19 '15 17:11 michaelukpong

It's not intended to be selectable by the end user. It will prefer the https protocol, and select it automatically if your site is being hosted by https.

fehguy avatar Nov 23 '16 23:11 fehguy

Given the milestone has been modified continuously above, I'm struggling to see when this issue was actually fixed, if it has been at all? I am using Swagger 2.0 and want to know what minimum version I need to go to to get this fix?

GavBurke avatar Mar 06 '17 12:03 GavBurke

@GavBurke the comment is pretty clear. It was not implemented and closed as won't fix.

We do have a new version coming soon where this will change though.

webron avatar Mar 06 '17 17:03 webron

@webron Not sure what comment you're referring to because on this issue, #1247, you haven't even replied....

Anyhoos, I got around the issue by setting the schemes to an empty array so the location.scheme is used instead of this.schemes[0] and I can now have HTTP on one site and HTTPS on the other without your tool breaking.

GavBurke avatar Mar 07 '17 08:03 GavBurke

I was referring to @fehguy's last comment before he actually closed the ticket.

webron avatar Mar 07 '17 08:03 webron

Ah okay, so that behaviour was introduced in v2.1.3?

GavBurke avatar Mar 07 '17 08:03 GavBurke

I am trying to use Swagger UI for the first time today, and I can't seem to dissuade it from using https on a development system, even though none of Swagger UI, the JSON definition, or the Swagger server itself are being served over https.

As suggested in the documentation, I just opened dist/index.html in a browser. I served up my local swagger JSON file using http-server --cors so that it provides http://localhost:8080/swagger.json which I entered in the box and clicked 'explore'. So far so good. However all the generated server calls are to https://localhost:10010/, so they don't work, because swagger project start runs the server using http, not https.

I have tried removing https from scheme. I have tried removing scheme entirely from the definition. It still uses https for requests.. Did I miss anything obvious, or do I need to customise the Swagger UI installation to make this work? It seems like this would be a common enough use case that it should work out of the box.

Edited to add: I got it working in the end. It seems the fundamental problem is opening dist/index.html as a file, which is neither http nor https. Just by serving the whole dist folder (http-server --cors) and opening it in a browser, it does use http for the server calls. I can't help thinking this might be made easier. A selectable protocol would have solved the problem.

ghost avatar Mar 12 '17 10:03 ghost

This work.

findReact() method

var findReact = function(dom) {
    for (var key in dom) {
        if (key.startsWith("__reactInternalInstance$")) {
            var compInternals = dom[key]._currentElement;
            var compWrapper = compInternals._owner;
            var comp = compWrapper._instance;
            return comp;
        }
    }
    return null;
};


Set https scheme:

var ui = SwaggerUIBundle({
   // ....
  onComplete: function() {
     var schemeSelect = document.querySelector('.scheme-container select');
     FindReact(schemeSelect).setScheme('https');
   }
});

giansalex avatar Sep 09 '18 03:09 giansalex