question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Swagger Generator - can't add server to list

See original GitHub issue

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

Issue Analytics

  • State:open
  • Created 4 years ago
  • Reactions:3
  • Comments:11 (4 by maintainers)

github_iconTop GitHub Comments

7reactions
vic10uscommented, Mar 20, 2020

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.

1reaction
larskinncommented, Jan 7, 2021

@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?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Can't generate server/client : TypeError: Cannot read ...
json to swagger.json . Which caused the editor not able to load code gen list for OpenAPI 3.0, and ended up showing list...
Read more >
Swagger UI Not Loading
I faced same issue: Swagger works just fine locally, but doesn't show UI once published to IIS. The problem was resolved by adding...
Read more >
Swagger Codegen Documentation
The Swagger Codegen is an open source code-generator to build server stubs and client SDKs ... To get a list of languages supported...
Read more >
Can not upload file when using a swagger generated ...
I was able to workaround the problem by modifying the generated server code to change the name "file" in @RequestPart("file") to the name ......
Read more >
Swagger UI Configuration
The swagger-config.yaml in the project root directory, if it exists, is baked into the application; configuration object passed as an argument to Swagger...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found