How to get Swagger interface to use port 443 with Swashbuckle?

On our QA and Prod networks that run our RESTful web services, port 80 is not open. Therefore, currently, when I try to get to the Swagger user interface in QA, I get this message and it just hangs:

fetching resource list: http://qa-server:80/product-catalog-api/swagger/docs/v1; Please wait.

I use Swashbuckle to configure Swagger. I also changed this line in the config, but it still does not work.

 // If schemes are not explicitly provided in a Swagger 2.0 document, then the scheme used to access // the docs is taken as the default. If your API supports multiple schemes and you want to be explicit // about them, you can use the "Schemes" option as shown below. // c.Schemes(new[] { "https" }); 

The SSL port 443 is open, so I would like to access the Swagger interface. I can manually change http://qa-server:80/product-catalog-api/swagger/docs/v1 to https://qa-server/product-catalog-api/swagger/docs/v1 and then Swagger will be enumerate my web methods, but it freezes when I click Try it out! This is the console output: SCRIPT5: Access is denied. File: swagger-ui-min-js, Line: 10, Column: 4300 SCRIPT5: Access is denied. File: swagger-ui-min-js, Line: 10, Column: 4300

EDIT:

So, I delved even more and got a little further, but still not where I want. If I look at the source in the Swagger index.html file, I see a problem:

 window.swashbuckleConfig = { rootUrl: 'http://qa-server:80/product-catalog-api', discoveryPaths: arrayFrom('swagger/docs/v1'), booleanValues: arrayFrom('true|false'), validatorUrl: stringOrNullFrom('null'), customScripts: arrayFrom(''), docExpansion: 'none', oAuth2Enabled: ('false' == 'true'), oAuth2ClientId: '', oAuth2ClientSecret: '', oAuth2Realm: '', oAuth2AppName: '', oAuth2ScopeSeperator: ' ', oAuth2AdditionalQueryStringParams: JSON.parse('{}') }; 

Despite the fact that I go to the site as https and have the Swashbuckle scheme set to https, it still generates rootUrl as http. I think that since I use Swashbuckle, I should use it to configure index.html because I don't have this file anywhere in my code, so I think Swashbuckle generates it on the fly.

I found out what I am missing when changing the swagger.json path. Apparently he needs a port number. Therefore, if I go to the swagger indexing page and manually change the path to the json file as https://qa-server:443/product-catalog-api/swagger/docs/v1 , everything will be fine. So now, I think I highlighted the problem before changing rootUrl in Swaggers index.html using Swashbuckle.

EDIT 2

Well, I think I configured Swashbuckle correctly because it correctly generates index.html on our dev server, but not qa, so I think the rest of the problem comes down to some difference in the environments or my package did not get installed correctly in qa.

DEV:

 window.swashbuckleConfig = { rootUrl: 'https://server-dev:443/product-catalog-api', discoveryPaths: arrayFrom('swagger/docs/v1'), booleanValues: arrayFrom('true|false'), validatorUrl: stringOrNullFrom('null'), customScripts: arrayFrom(''), docExpansion: 'none', oAuth2Enabled: ('false' == 'true'), oAuth2ClientId: '', oAuth2ClientSecret: '', oAuth2Realm: '', oAuth2AppName: '', oAuth2ScopeSeperator: ' ', oAuth2AdditionalQueryStringParams: JSON.parse('{}') }; 

QA:

 window.swashbuckleConfig = { rootUrl: 'http://qa-server:80/product-catalog-api', discoveryPaths: arrayFrom('swagger/docs/v1'), booleanValues: arrayFrom('true|false'), validatorUrl: stringOrNullFrom('null'), customScripts: arrayFrom(''), docExpansion: 'none', oAuth2Enabled: ('false' == 'true'), oAuth2ClientId: '', oAuth2ClientSecret: '', oAuth2Realm: '', oAuth2AppName: '', oAuth2ScopeSeperator: ' ', oAuth2AdditionalQueryStringParams: JSON.parse('{}') }; 

EDIT 3

We conducted a test to further isolate the problem. We have an A10 load balancer in our QA environment. We raised the new A10 for the development environment to find out what happened, and now we have the same problem in dev. A10 did some manipulation of the http headers that we removed to see if this was a problem, but still got the same thing. I believe that the servers are configured, SSL is uploaded to A10, and http gets into the field on which my code works. Therefore, when the Swashbuckle code works, it works under http, causing a problem. I think I need a way to make it always be https.

+5
source share
2 answers

I finally get it! Thanks to Sampada and strick01 for helping me isolate the problem. I found this article on github with a solution to force https using Swashbuckle:

https://github.com/domaindrivendev/Swashbuckle/issues/296

 config .EnableSwagger("docs/{apiVersion}", c => { ... c.RootUrl(ResolveBasePath); ... }) .EnableSwaggerUi(); private static string ResolveBasePath(HttpRequestMessage message) { var virtualPathRoot = message.GetRequestContext().VirtualPathRoot; var schemeAndHost = "https://" + message.RequestUri.Host; return new Uri(new Uri(schemeAndHost, UriKind.Absolute), virtualPathRoot).AbsoluteUri; } 
+7
source

Swashbuckle generates Swagger documentation for you when an HTTP request comes in swagger/docs/v1 or swagger/ui/index . If your request comes through https , then by default the index.html that it creates will contain rootUrl of https://yourdomain:443/yourapiapplication . Similarly, if it comes via http , then rootUrl will be http://yourdomain:80/yourapiapplication . Given this situation, the main candidate for your problems is caching. Did you enable caching of the Swagger documentation by overriding the default swagger provider in SwaggerConfig.cs? Or is there a proxy server in your QA environment or a caching setting different from the one your developer has? Regenerating the documentation with a new request to your QA server via HTTPS should result in the correct rootUrl in your index.html.

+1
source

Source: https://habr.com/ru/post/1247133/


All Articles