RicoSuter / NSwag

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

Bypass transformOptions for Anonymous request #2540

Open radicalapps opened 4 years ago

radicalapps commented 4 years ago

I have an aspnetcore C# api and am successfully generating swagger and typescript (fetch) code.

Most of my Api methods require bearer authentication, and I am adding the header using transformOptions. This works as expected.

One of my methods has the [AllowAnonymous] attribute and, as expected, shows in the Swagger UI without the padlock icon.

I need to execute this call without the Bearer token. However, there does not appear to be a hook to enable the call to transformOptions to be bypassed for this method, or for the transformOptions implementation to discover that this specific all does not require authentication.

The only solution I can think of right now is to edit the Typescript after it has been generated. This seems less than ideal.

If this is not something that is already built in that I have missed, could it be added as a feature request?

Thanks, Jeff.

RicoSuter commented 4 years ago

I don't think there is something available out-of-the-box. I see two options:

timosnel commented 1 year ago

Did you ever find a clean way to do this?

radicalapps commented 1 year ago

Sorry Timo, not really!

timosnel commented 1 year ago

@RicoSuter the CSharp operation model contains security related properties which can be used in templating:

https://github.com/RicoSuter/NSwag/blob/fca7e8126d634a7971f10c36d274b4e397577ab3/src/NSwag.CodeGeneration.CSharp/Models/CSharpOperationModel.cs#L217-L221

Would you consider a PR where I add the same properties to TypeScriptOperationModel so it can be used in customized templates?

Currently I don't see any other way than hardcoding the anonymous operations, which defeats the purpose of having it correctly specified in the OpenApi spec.