In our QA and Prod environments that run our RESTful web services, port 80 is not open. So currently when I try to get to Swagger UI 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 am using Swashbuckle to configure Swagger. I also changed this line in the config, but it still doesn't 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 get to Swagger UI to run on it. 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 list my web methods, but it hangs when I click Try it out!
This is the output from the console: SCRIPT5: Access is denied. File: swagger-ui-min-js, Line: 10, Column: 4300
EDIT:
So I have been digging in some more and have gotten a little farther but still not where I want to be. If I view source on the Swagger index.html file, I can see the 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('{}')
};
Even though the I am navigating to the site as https and have the Swashbuckle scheme set to https, it is still generating the rootUrl as http. I think since I'm using Swashbuckle, I have to use it to configure the index.html because I don't have that file anywhere in my code so I guess Swashbuckle is generating it on the fly.
I did find out what I was missing when changing the path the swagger.json. It apparently needs the port number on there. So if I navigate to the swagger index page and manually change the path to the json file to be https://qa-server:443/product-catalog-api/swagger/docs/v1
everything works fine. So now I think I have isolated the problem down to how do I change the rootUrl in Swaggers index.html using Swashbuckle.
EDIT 2
Well, I think I have Swashbuckle configured correctly because it generates the index.html correctly on our dev server, but not qa so I guess the rest of the problem is down to some difference in environments or my package didn't get installed in qa properly.
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 did a test to further isolate the problem. We have an A10 load balancer in our QA environment. We stood up a new A10 for the dev environment to see what happened, and we now have the same problem in dev. The A10 was doing some http header manipulation that we removed to see if that was the problem but still getting the same thing. I believe with the way the servers are setup, SSL is being offloaded to the A10 and the box actually running my code is getting http. So when the Swashbuckle code runs, it is running under http causing the problem. I think I need a way to force it to always be https.
default: '443'
Go to http://localhost:8000/ in your address bar. This address lets you view the local web server. By default, web servers default to the index. html file in the directory, so it will show the Swagger UI file automatically.
Swashbuckle is an open source project for generating Swagger documents for Web APIs that are built with ASP.NET Core. There are three core components: AspNetCore. SwaggerGen - provides the functionality to generate JSON Swagger documents that describe the objects, methods, return types, etc.
I finally got it! Thanks to Sampada and strick01 for helping me isolate the problem. I found this article on github with the solution of forcing https with 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;
}
Swashbuckle generates the Swagger documentation for you when the HTTP request comes in to swagger/docs/v1
or swagger/ui/index
. If your request comes in via https
then the default index.html that it generates will contain a rootUrl
of https://yourdomain:443/yourapiapplication
. Similarly if it comes in via http
then the rootUrl
will be http://yourdomain:80/yourapiapplication
. Given this situation the primary candidate for your problems is caching. Have you enabled caching of the Swagger documentation via the overriding the default swagger provider in SwaggerConfig.cs? Or does your QA environment have proxy servers or a caching setup different from that in your dev one? Regenerating the documentation via a fresh request to your QA server via HTTPS should result in a correct rootUrl
in your index.html.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With