RicoSuter / NSwag

The Swagger/OpenAPI toolchain for .NET, ASP.NET Core and TypeScript.
http://NSwag.org
MIT License
6.79k stars 1.29k forks source link

Swagger Generator - can't add server to list #2441

Open Oposow opened 5 years ago

Oposow commented 5 years ago

I'm using Nswag 13.1.2 in .NET Core 3.0.0. I'm trying to add new Server and Scheme to my swagger document.

services.AddOpenApiDocument(c =>
{
    c.PostProcess = d =>
    {
        d.Schemes.Add(OpenApiSchema.Https);
        d.Servers.Add(new OpenApiServer() { Url = "foo.com" });
    };
});

Unfortunatelly in my swagger there is still only localhost server option. image

RicoSuter commented 5 years ago

Do not change schemes and use

d.Servers.Add(new OpenApiServer() { Url = "https://foo.com" });
Oposow commented 5 years ago

Hi,

Thanks for you answer. Unfortunatelly it doesn't change anything. Still have only one element in my servers list

Doolali commented 4 years ago

I'm also experiencing the same issue:

        services.AddOpenApiDocument(config =>
        {
            config.DocumentName = route;
            config.PostProcess = document =>
            {
                document.Servers.Add(new OpenApiServer { Url = "https://[ENDPOINT]" });
            };
        });

Still only generates a server list with my hostname in.

Doolali commented 4 years ago

So I took a look at the .NET Core OpenApiDocumentMiddleware.

This sets the Servers as the current Host then calls PostProcess; same thing for the Owin version of the Middleware. It doesn't look like there should be an issue. But having said that I would have expected Servers to be populated with a URL in the PostProcess action which it is not.

I've also tried creating an IDocumentProcessor to see if there was any more luck adding the Server in that way, but the .Clear() logic on the Server list seems to be foiling this effort.

RicoSuter commented 4 years ago

Its important to note that you can post process per document (in AddOpenApiDocument) and per request (in UseOpenApi) - the default server is only added when hosting in asp.net core (not via CLI) when an actual HTTP request is available after document post processing and before request post processing

whentotrade commented 4 years ago

I had the same problem. Thanks to the hint from @RicoSuter it was clear. The Servers.Add method doesnt work in AddOpenApiDocument, it had to be placed in the UserOpenApi PostProcess section in the startup configure method like this:

app.UseOpenApi(o => {
    o.PostProcess = (doc, httpReq) =>
    {
        doc.Servers.Clear(); //... remove local host, added via asp .net core
        doc.Servers.Add(new OpenApiServer { Url = "[YOUR SERVER URL]" });  //... add server
    };
});
RicoSuter commented 4 years ago

First AddOpenApiDocument() is called and then UseOpenApi() is called which overwrites the server with the actual running server. If nswag is run via the cli against the project (spec not served via http) then only AddOpenApiDocument() is called.

RicoSuter commented 4 years ago

The Servers.Add method doesnt work in AddOpenApiDocument,

It works but it is overwritten by UseOpenApi if its served via http.. so if you do cli and http you probably need to add it in both PostProcess

vic10us commented 4 years ago

The issue I was facing was that when hosting on multiple endpoints (http, https, proxy, etc..) only the first would be stored in the documentCache. I was able to fix this by doing the following:


app.UseOpenApi(options =>
{
    options.CreateDocumentCacheKey = request => request.GetServerUrl();
    options.PostProcess = (document, request) =>
    {
        var serverUrl = request.GetServerUrl();
        document.Servers.Clear();
        document.Servers.Add(new OpenApiServer { Url = serverUrl });
    };
});

This creates a unique document cache per server url instead of the default String.Empty. Works like a charm.

unekinn commented 3 years ago

@vic10us / @RicoSuter

The issue I was facing was that when hosting on multiple endpoints (http, https, proxy, etc..) only the first would be stored in the documentCache.

I was having this issue as well, and tried this solution. But to actually use request.GetServerUrl(), I had to copy HttpRequestExtension from here into my project, as the HttpRequestExtension class is internal.

After add doing that, the server url is correct both directly and through reverse proxy. But is there a better way to go about this? Since you didn't mention anything like that @vic10us I assume you didn't have to copy it into your project?

mathiash98 commented 1 year ago

NSwag has support for this by default now, BUT you need to configure the Dotnet application to actually use the forwardHeaders like described here: https://nickkell.medium.com/swagger-service-url-behind-reverse-proxy-3a2795229100

https://learn.microsoft.com/en-us/aspnet/core/host-and-deploy/proxy-load-balancer?view=aspnetcore-6.0#forwarded-headers-middleware-order

Works perfectly without any other settings on UseOpenApi()