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

ApiException partly missing when referencing multiple contract files #3448

Open kimbell opened 3 years ago

kimbell commented 3 years ago

I have multiple OpenApi contracts that I'm referencing with OpenApiReference in the csproj file. When I generate the code, I place each client in a different namespace. I have some types with same names that would clash in the same namespace.

In the generated code, I get ApiException code for one of the contracts, but not for the other. Since they are in different namespaces, this doesn't compile. I have tried setting the generateExceptionClasses and exceptionClass.

AverageMichiganJoe commented 3 years ago

I've been running into the same issue the past couple of days.

I've tried setting /GenerateExceptionClasses:true as an option in the csproj file on the second OpenApiReference in csproj, but it is ignored and the build output shows the option as being explicitly set to false on everything but the first OpenApiReference.

I would say this is related to #2839, but I don't think that one specifically calls out the fact that the option /GenerateExceptionClasses:true is being ignored when put in a csproj file.

RicoSuter commented 3 years ago

GenerateExceptionClasses:true is mainly used to turn this completely off ... it doesnt enforce it... so probably the check whether the exceptions are needed is flawed ATM.

RicoSuter commented 3 years ago

Hmm, maybe there is no such check:

https://github.com/RicoSuter/NSwag/blob/d69085067b2261c75f3b49a0e43a4c26aa49d743/src/NSwag.CodeGeneration.CSharp/Templates/File.liquid#L154

https://github.com/RicoSuter/NSwag/blob/d69085067b2261c75f3b49a0e43a4c26aa49d743/src/NSwag.CodeGeneration.CSharp/Models/CSharpFileTemplateModel.cs#L117

https://github.com/RicoSuter/NSwag/blob/4f345e1adc9c49d8498d4018250760aa69dd8dd2/src/NSwag.CodeGeneration.CSharp/CSharpClientGeneratorSettings.cs#L47

Maybe you turned off contracts gen?

https://github.com/RicoSuter/NSwag/blob/d69085067b2261c75f3b49a0e43a4c26aa49d743/src/NSwag.CodeGeneration.CSharp/Templates/File.liquid#L31

crackalak commented 2 years ago

just came across this issue myself and have had a look

@RicoSuter /GenerateExceptionClasses is explicity set to false for service references after the first one

https://github.com/RicoSuter/NSwag/blob/67cc25b63a92df49d185dfa15486ba9e60c0db39/src/NSwag.ApiDescription.Client/NSwag.ApiDescription.Client.targets#L21-L26

although the intention with this is that you should be able to explicitly turn this back on again with user provided options, the command line processor (NConsole) takes the first matching argument that it finds and not the last

https://github.com/RicoSuter/NConsole/blob/bf794a7d80c28a9d59ff84371ae5e7255545cf7e/src/NConsole/ArgumentAttribute.cs#L105

and so a command like this from the user explicitly setting /GenerateExceptionClasses:true would not work

openapi2csclient /className:{controller}Client /namespace:App /input:"C:\app\OpenAPI\service.json" /output:"obj\serviceClient.cs" /GenerateExceptionClasses:false /UseBaseUrl:false /GenerateExceptionClasses:true

JanRou commented 2 years ago

May be this post can help you out: https://github.com/RicoSuter/NSwag/issues/2839#issuecomment-1198017008

JanRou commented 2 years ago

This is the same solution: https://github.com/dotnet/aspnetcore/issues/18204#issuecomment-971911921