Search code examples
.netswaggerswashbuckle.aspnetcore

Swagger: get swagger.json from external server but use the localserver as host


since generating the swagger json on the fly takes around 3seconds locally (due to reflection in dotnet) I try to achieve the following:

  1. The swagger.json for use in swagger-ui should sit on a CDN server so that it is not generated during runtime. It is generated via cli during build-time
  2. When I use swagger-ui to make calls the base-url should of course NOT be this cdn location but a relative location

This is what does not work:

var cdnPath = https://linktomycdn.com/staticswaggerstore
app.UseSwagger(c => c.SerializeAsV2 = true);
string swaggerUrl = $"{cdnPath}/swagger-{Version}.json";
app.UseSwaggerUI(options =>
{
  options.SwaggerEndpoint(swaggerUrl, $"My  API v{Version}");
  options.OAuthClientId($"{config.ClientAuthConfig.JwtClientId}");
  options.OAuthAppName($"{config.ClientAuthConfig.JwtClientApp}");
 });

What happens is that the server correctly loads the json file (which I created with a swagger-cli tool) but then when I try to make calls with swagger-ui it uses the cdnpath as base. so that no call works.

Can I "split up" the swagger.json location and the actual endpoint?


Solution

  • I got it working by generating the json file with a specific --host flag

    e.g.

    dotnet swagger tofile --serializeasv2 --host mycoolapi.com --output .swagger-1.0.0.json /app/MyDll.dll v1.0.0
    

    So now my API is using https://mycoolapi.com as host but the json sits on my CDN https://linktomycdn.com/staticswaggerstore. This is configured within the C# code as SwaggerEndpoint

    Make sure to not provide the schema (https) in the --host parameter