Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
vsfeedback opened this issue May 6, 2024 · 1 comment
Labels
area-mvc Includes: MVC, Actions and Controllers, Localization, CORS, most templates Author: Migration Bot 🤖 The issue was created by a issue mover bot. The author may not be the actual author. feature-openapi
Milestone

Comments

@vsfeedback
Copy link

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:

  • a single, no template-string <ClassName> or no <ClassName> at all together with OperationGenerationMode set to SingleClientFromOperationId or SingleClientFromPathSegments
  • a templated <ClassName> like the default "{controller}Client" with (any?) of the available OperationGenerationMode`

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)

@dotnet-issue-labeler dotnet-issue-labeler bot added the area-mvc Includes: MVC, Actions and Controllers, Localization, CORS, most templates label May 6, 2024
@dotnet-policy-service dotnet-policy-service bot added the Author: Migration Bot 🤖 The issue was created by a issue mover bot. The author may not be the actual author. label May 6, 2024
@captainsafia
Copy link
Member

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

if (string.IsNullOrEmpty(className))
{
var outputFilename = Path.GetFileNameWithoutExtension(outputPath);
className = CSharpIdentifier.SanitizeIdentifier(outputFilename);
MetadataSerializer.SetMetadata(newItem, "ClassName", className);
}

@captainsafia captainsafia added this to the Backlog milestone May 6, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-mvc Includes: MVC, Actions and Controllers, Localization, CORS, most templates Author: Migration Bot 🤖 The issue was created by a issue mover bot. The author may not be the actual author. feature-openapi
Projects
None yet
Development

No branches or pull requests

2 participants