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

Bug: impossible to generate exception classes for non-first client #4890

Closed
bkoelman opened this issue May 12, 2024 · 0 comments · Fixed by #4891
Closed

Bug: impossible to generate exception classes for non-first client #4890

bkoelman opened this issue May 12, 2024 · 0 comments · Fixed by #4891

Comments

@bkoelman
Copy link
Contributor

bkoelman commented May 12, 2024

When a project contains multiple <OpenApiReference> entries, the ApiException class is only generated for the first entry. While this is a nice default, since v14 it has become impossible to generate exception classes for all entries (they live in separate namespaces).

The problematic line is in NSwag.ApiDescription.Client.targets:

<Command Condition="!%(FirstForGenerator) OR (%(NSwagGenerateExceptionClasses) != '' AND !%(NSwagGenerateExceptionClasses))">%(Command) /GenerateExceptionClasses:false</Command>

which always adds /GenerateExceptionClasses:false for the non-first entry, ignoring the configured value of NSwagGenerateExceptionClasses in <OpenApiReference>.

When trying to work around it by adding the argument explicitly using:

<Options>/GenerateExceptionClasses:true</Options>

the build fails because the /GenerateExceptionClasses parameter is now specified multiple times (which makes NConsole fail).

GenerateExceptionClassesDemo.zip contains a minimal repro. It fails to build the generated QueryStringsClient file (second entry in csproj), because it doesn't declare the ApiException class while referencing it.

To fix this, the problematic line should be replaced with:

<Command Condition="!%(FirstForGenerator) AND ('%(NSwagGenerateExceptionClasses)' == '')">%(Command) /generateExceptionClasses:false</Command>
<Command Condition="'%(NSwagGenerateExceptionClasses)' != ''">%(Command) /generateExceptionClasses:%(NSwagGenerateExceptionClasses)</Command>

The fix only adds /generateExceptionClasses:false for non-first entries that aren't configured. In all other cases, it takes the configured value, similar to all the other NSwag* properties. When /generateExceptionClasses is omitted, it defaults to true.

The same fix should be applied for NSwagGenerateResponseClasses, which works similarly.

Additionally, generation of the JsonInheritanceConverter, JsonInheritanceConverterAttribute, and JsonInheritanceAttribute classes could benefit from the same fix, but there's no NSwag* property to control whether they are generated.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant