dotnet / aspnetcore

ASP.NET Core is a cross-platform .NET framework for building modern cloud-based web applications on Windows, Mac, or Linux.
https://asp.net
MIT License
35.59k stars 10.06k forks source link

OpenApiReference default className breaks NSwag generated code when using MultipleClients #55556

Open vsfeedback opened 6 months ago

vsfeedback commented 6 months ago

This issue has been moved from a ticket on Developer Community.


[severity:It’s more difficult to complete my work]
When defining an <OpenApiReference CodeGenerator="NSwagCSharp"> in a csproj, one may specify the ClassName of the generated class(es) as well as an OperationGenerationMode, that determines, how many client classes are generated (Single/Multiple) and how their methods are named.
When you leave out the <ClassName> tag from the <OpenApiReference>, that is by default generated to be swaggerClient. The default /OperationGenerationMode of NSwag however is MultipleClientsFromOperationId. So when there are multiple endpoints defined in the swagger.json, the generated methods are all put into a single class named swaggerClient and most likely overlap in method names and therefore the class has multiple definitions of the same method and does not compile.
If the NSwag code generator does not receive a /className option, it is set to "{controller}Client" and works fine with the default /OperationGenerationMode. But it’s currently implossible to leave the option from the openapi2csclient call, because if you don’t define it, Visual Studio does.

So to have a working configuration one must use one of the following settings:

In any case, having the className generated by Visual Studio to be swaggerClient probably breaks the generated code.

I’m using NSwag 14.x and this behavior is tested with Visual Studio 17.9.5

My proposed solution is to not have Visual Studio generate a class name, but have NSwag figure it out.


Original Comments

Feedback Bot on 4/16/2024, 07:46 PM:

(private comment, text removed)

Feedback Bot on 4/17/2024, 01:57 AM:

(private comment, text removed)

Bill Hiebert [MSFT] on 5/2/2024, 03:23 PM:

(private comment, text removed)


Original Solutions

(no solutions)

captainsafia commented 6 months ago

@RicoSuter We received this ticket from some using the client generation integration with NSwg in Visual Studio. We currently set the ClassName parameter that is passed to NSwag to be based on the project name.

Do you have any thoughts on what the right default should be? Or perhaps, should ASP.NET Core not set one and leave it up to NSwag?

https://github.com/dotnet/aspnetcore/blob/da3aa27233a2cec2f6780884f71934b2f5e686ce/src/Tools/Extensions.ApiDescription.Client/src/GetOpenApiReferenceMetadata.cs#L111-L118

RicoSuter commented 1 week ago

I think as described the right default should be "{controller}Client" and "MultipleClientsFromOperationId" so that for each controller a client class is generated.