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

Errors in OpenApiDocument.FromUrlAsync when the web server requires a user-agent #2685

Closed ryepup closed 4 years ago

ryepup commented 4 years ago

I'm trying to generate a client for a government API that has their server configured to reject requests that do not specify a User-Agent header. This seems like an odd thing to do, but I guess they are trying to track usage or detect abuse via that header.

Our workaround is to download the spec and generate from a file, but it would be nice to not need that.

I think we could fix this by specifying a header in DynamicApis before we make the request, but I'm unclear why we use DynamicApis instead of HttpClient directly.

RicoSuter commented 4 years ago

but I'm unclear why we use DynamicApis instead of HttpClient directly.

This is because in the beginning this was only a single .net standard 1.3 library where this API was not available... and we still want to support HTTP for those runtimes. But yeah we can set a user agent there, should be a breaking change for anyone, right? :-)

ryepup commented 4 years ago

that shouldn't be a breaking change. I can think of a few options:

Thoughts?

JohnnyK84 commented 4 years ago

Hi, Checking if there is any resolution to this? We have a containerized API hosted on AWS and when using Nswag to generate the client service we are getting this error back:

System.Net.Http.HttpRequestException: Response status code does not indicate success: 403 (Forbidden). at System.Net.Http.HttpResponseMessage.EnsureSuccessStatusCode() at CallSite.Target(Closure , CallSite , Object ) at System.Dynamic.UpdateDelegates.UpdateAndExecuteVoid1[T0](CallSite site, T0 arg0) at NJsonSchema.Infrastructure.DynamicApis.HttpGetAsync(String url) at NSwag.OpenApiDocument.FromUrlAsync(String url) in C:\projects\nswag\src\NSwag.Core\OpenApiDocument.cs:line 219 at NSwag.Commands.OutputCommandBase.ReadSwaggerDocumentAsync(String input) in C:\projects\nswag\src\NSwag.Commands\Commands\OutputCommandBase.cs:line 41 at NSwag.Commands.Generation.FromDocumentCommand.RunAsync() in C:\projects\nswag\src\NSwag.Commands\Commands\Generation\FromDocumentCommand.cs:line 62 at NSwag.Commands.Generation.FromDocumentCommand.RunAsync(CommandLineProcessor processor, IConsoleHost host) in C:\projects\nswag\src\NSwag.Commands\Commands\Generation\FromDocumentCommand.cs:line 53 at NSwag.Commands.NSwagDocumentBase.GenerateSwaggerDocumentAsync() in C:\projects\nswag\src\NSwag.Commands\NSwagDocumentBase.cs:line 280 at NSwag.Commands.NSwagDocument.ExecuteAsync() in C:\projects\nswag\src\NSwag.Commands\NSwagDocument.cs:line 81 at NSwag.Commands.Document.ExecuteDocumentCommand.ExecuteDocumentAsync(IConsoleHost host, String filePath) in C:\projects\nswag\src\NSwag.Commands\Commands\Document\ExecuteDocumentCommand.cs:line 86 at NSwag.Commands.Document.ExecuteDocumentCommand.RunAsync(CommandLineProcessor processor, IConsoleHost host) in C:\projects\nswag\src\NSwag.Commands\Commands\Document\ExecuteDocumentCommand.cs:line 48 at NConsole.CommandLineProcessor.ProcessSingleAsync(String[] args, Object input) at NConsole.CommandLineProcessor.ProcessAsync(String[] args, Object input) at NConsole.CommandLineProcessor.Process(String[] args, Object input) at NSwag.Commands.NSwagCommandProcessor.Process(String[] args) in C:\projects\nswag\src\NSwag.Commands\NSwagCommandProcessor.cs:line 56

Using Postman or accessing the swagger Json Schema directly through the browser is fine. We also experiemented with a basic .Net console app and were able to replicate the above error. When explicitly adding the user-agent header it works.

JohnnyK84 commented 4 years ago

So we just got around this by simply using a CURL request to download the JSON schema and output as a file within src code. Then pointing the config.nswag file to reference this local copy instead.

jeremyVignelles commented 4 years ago

Not sure there is something to add here in NSwag. If an option was introduced for setting the user agent, then others wouldn't understand :

I'd argue to keep the project and options simple, and for complex scenarios like these, download the file with a curl, a dotnet-script, a console app or whatever, and pass that file to NSwag.

RicoSuter commented 4 years ago

Totally agree to @jeremyVignelles 's answer.

ryepup commented 4 years ago

Closing this out, there are options to solve the problem outside nswag and all the proposed solutions open up pandora's box to endless scope/feature creep.

Thanks for consideration @RicoSuter and for the great counterpoint @jeremyVignelles.