Hi,
I'm setting the RedirectUrl using OAuth2RedirectUrl via the UseSwaggerUI method.
c.OAuth2RedirectUrl("http://hostname:12661/swagger/oauth2-redirect.html");
If I don't do this it defaults to localhost:3200.
All the samples I've come across has the hostname and port hardcoded as above. However, for obvious reasons a hardcoded value is no good for a prod environment. I would also rather not get this value from a config file.
Can anyone offer any suggestions how I can auto discover the hostname and port?
The only approach I can think of right now would be to use a custom version of index.html, described in the readme. There, you can use window.location to discover the full redirect url, and pass it to the swagger-ui component:
<script>
window.onload = function () {
var configObject = JSON.parse('%(ConfigObject)');
var oauthConfigObject = JSON.parse('%(OAuthConfigObject)');
// Apply mandatory parameters
configObject.dom_id = "#swagger-ui";
configObject.presets = [SwaggerUIBundle.presets.apis, SwaggerUIStandalonePreset];
configObject.layout = "StandaloneLayout";
// Discover and assign OAuth2 redirect URL
configObject.oauth2RedirectUrl = window.location + "oauth2-redirect.html";
// Build a system
const ui = SwaggerUIBundle(configObject);
// Apply OAuth config
ui.initOAuth(oauthConfigObject);
}
</script>
Trying to implement this.
I get an error from the example code in the readme.
app.UseSwaggerUI(c =>
{
c.IndexStream = () => GetType().GetTypeInfo().Assembly
.GetManifestResourceStream("CustomUIIndex.Swagger.index.html"); // requires file to be added as an embedded resource
});
An object reference is required for the non-static field, method, or property 'object.GetType()'
Edit: Solves that by moving it out of the static ApplicationBuilderExtensions
Any plans to fix this?
It worked with 1.x version on multiple environments without any additional configuration.
And now to maintain custom index.html does not look like very good solution.
Just here to say @domaindrivendev suggestion works for me. But yes, it's not the a good solution.
One thing I read over at the swagger ui site is that you should be able to set the url and port in your swagger file. But that's also a nogo for me, as I have to deploy this on multiple devices without changing a config file and entering the server url.
@Razzeee - see 893aa299547207fc74455399ec4292bd8aa8902f. Adds the host-aware, built-in default, so you don't have to.
Hi @domaindrivendev does that mean if I upgrade to Swashbuckle.AspNetCore 2.1.0 it should work like 1.x and be auto host-aware?
I've updated and it still tries to redirect to "http://localhost:3200/oauth2-redirect.html"
I think that's targeting anything greater than 2.1.0 not 2.1.0 itself.
Hey @Razzeee 2.1.0 is the latest version of Swashbuckle.AspNetCore. I don't think there is anything newer. I'm assuming the fix included in changeset 893aa29 was deployed with nugget package 2.1.0?
@patnolan
No, check the tag here: https://github.com/domaindrivendev/Swashbuckle.AspNetCore/commits/v2.1.0
I would be surprised if that was included in the nugget package. Anyway the changeset itself also confused me with the 2.1.0-beta-X stuff.
So to be clear, I don't think there is a released version with this fix yet.
Thanks @Razzeee
I wonder if anyone knows when the change will be available? This is a pretty big issue for us as we're deploying Swagger to so many environments.
2.1.1 build is baking and we speak. Should be available on Nuget shortly ...
Hey @domaindrivendev I've upgraded to Swashbuckle.AspNetCore 2.1.1 and my API loads but nothing is displayed.
I created a simple test api with Swashbuckle and it has the same issue (see attached). If I revert the test project back to 2.1.0 it works.
Is there something we need to do after upgrading to 2.1.1 for swagger to display?
Hi @domaindrivendev are you able to confirm if release Swashbuckle.AspNetCore 2.1.1 is broken?
Please try 2.2.0
Hi @domaindrivendev I can confirm that's fixed the issue.
Thanks heaps!
Hi @domaindrivendev I can confirm that's fixed the issue.
Thanks heaps!
How this solved, i cant see array of string which can take all dev qa prod local environments
Most helpful comment
@Razzeee - see 893aa299547207fc74455399ec4292bd8aa8902f. Adds the host-aware, built-in default, so you don't have to.