Open Oposow opened 5 years ago
Do not change schemes and use
d.Servers.Add(new OpenApiServer() { Url = "https://foo.com" });
Hi,
Thanks for you answer. Unfortunatelly it doesn't change anything. Still have only one element in my servers list
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.
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.
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
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
};
});
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.
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
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.
@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?
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
Works perfectly without any other settings on UseOpenApi()
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.
Unfortunatelly in my swagger there is still only localhost server option.