question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

'Missing ApiException' compiler error

See original GitHub issue

Currently I have a project that uses multiple openapi.json v3 files. We used the official openapi tools described in this link: https://docs.microsoft.com/en-us/aspnet/core/web-api/microsoft.dotnet-openapi?view=aspnetcore-3.1

Because of the following code the generated c# client do not compile:

<ItemGroup>
  <!-- @(CurrentOpenApiReference) item group will never contain more than one item. -->
  <CurrentOpenApiReference>
    <Command>$(_NSwagCommand) openapi2csclient /className:%(ClassName) /namespace:%(Namespace)</Command>
  </CurrentOpenApiReference>
  <CurrentOpenApiReference>
    <Command Condition="! %(FirstForGenerator)">%(Command) /GenerateExceptionClasses:false</Command>
  </CurrentOpenApiReference>
  <CurrentOpenApiReference>
    <Command>%(Command) /input:"%(FullPath)" /output:"%(OutputPath)" %(Options)</Command>
  </CurrentOpenApiReference>
</ItemGroup>

Why did you make this design decision of not generating the Exception code for the remaining clients? The commented affirmation of only one item is not true.

I managed it to work by adding a AdditionalNamespaceUsages options but for me it feels like a hack.

Below I added a sample of a csproj to reproduce the issue:

<Project Sdk="Microsoft.NET.Sdk">
  
  <PropertyGroup>
    <TargetFramework>netcoreapp3.1</TargetFramework>
  </PropertyGroup>
      
  <ItemGroup> 
    <PackageReference Include="Newtonsoft.Json" Version="12.0.2" /> 
    <PackageReference Include="NSwag.ApiDescription.Client" Version="13.5.0" />
  </ItemGroup>
  
  <ItemGroup>
    <OpenApiReference Include=".\OpenApi1.json">
      <Namespace>Integration.ExternalApi1</Namespace>
    </OpenApiReference>
    
    <OpenApiReference Include=".\OpenApi2.json">
      <Namespace>Integration.ExternalApi2</Namespace>
      <!-- Workaround to make the generated code compile -->
      <!--<Options>/AdditionalNamespaceUsages:Integration.ExternalApi1</Options>-->
    </OpenApiReference>
  </ItemGroup>
  
</Project>

Issue Analytics

  • State:open
  • Created 3 years ago
  • Reactions:11
  • Comments:13

github_iconTop GitHub Comments

7reactions
WizX20commented, Feb 10, 2021

I stumbled on this one when I had to generate a new version of a client side-by-side.

I had a look at https://github.com/RicoSuter/NSwag/blob/85ae862fd6d68173a201a79e0ad06e0be2ec5de1/src/NSwag.CodeGeneration.CSharp/CSharpClientGeneratorSettings.cs to check the options we have.

Finally, I decided not to use a generated ApiException class, but instead move that to my own file.

  1. Move the ApiException partial classes from the first generated client to it’s own ApiException.cs file with the namespace ‘Example.Api.Client.Exceptions’
  2. Use the GenerateExceptionClasses:false in the options:
    <OpenApiReference Include="Example.openapi.v1_3.json" CodeGenerator="NSwagCSharp" Namespace="Example.Api.Clients.v1_3" Condition="'$(Configuration)'=='Debug'">
      <OutputPath>..\Clients\Example.openapi.v1_3Client.cs</OutputPath>
      <Options>/GenerateClientInterfaces:true /GenerateExceptionClasses:false /AdditionalNamespaceUsages:Example.Api.Client.Exceptions</Options>
    </OpenApiReference>
    <OpenApiReference Include="Example.openapi.v1_4.json" CodeGenerator="NSwagCSharp" Namespace="Example.Api.Clients.v1_4" Condition="'$(Configuration)'=='Debug'">
      <OutputPath>..\Clients\Example.openapi.v1_4Client.cs</OutputPath>
      <Options>/GenerateClientInterfaces:true /GenerateExceptionClasses:false /AdditionalNamespaceUsages:Example.Api.Client.Exceptions</Options>
    </OpenApiReference>
4reactions
iannicktlcommented, Nov 30, 2022

I do have the same issue. Used the following workaround:

  1. Create a new file (I named it NSwagClientError_Missing_Code_Generation_Of_ApiException_OnTheSecondClient.cs)
  2. Pass the code below
  3. Replace the namespace [YourNameSpaceFoNswagGenerateClient] and the class name [YourClassNameForNswagGenerateClient]. The 2 must match what you put on generated NSwag client

Here is the ApiException code

{
    /// <summary>
    /// https://github.com/RicoSuter/NSwag/issues/2839
    /// </summary>
    public partial class [YourClassNameForNswagGenerateClient]
    {

        public partial class ApiException : System.Exception
        {
            public int StatusCode { get; private set; }

            public string Response { get; private set; }

            public System.Collections.Generic.IReadOnlyDictionary<string, System.Collections.Generic.IEnumerable<string>> Headers { get; private set; }

            public ApiException(string message, int statusCode, string response, System.Collections.Generic.IReadOnlyDictionary<string, System.Collections.Generic.IEnumerable<string>> headers, System.Exception innerException)
                : base(message + "\n\nStatus: " + statusCode + "\nResponse: \n" + ((response == null) ? "(null)" : response.Substring(0, response.Length >= 512 ? 512 : response.Length)), innerException)
            {
                StatusCode = statusCode;
                Response = response;
                Headers = headers;
            }

            public override string ToString()
            {
                return string.Format("HTTP Response: \n\n{0}\n\n{1}", Response, base.ToString());
            }
        }

        [System.CodeDom.Compiler.GeneratedCode("NSwag", "13.17.0.0 (NJsonSchema v10.8.0.0 (Newtonsoft.Json v13.0.0.0))")]
        public partial class ApiException<TResult> : ApiException
        {
            public TResult Result { get; private set; }

            public ApiException(string message, int statusCode, string response, System.Collections.Generic.IReadOnlyDictionary<string, System.Collections.Generic.IEnumerable<string>> headers, TResult result, System.Exception innerException)
                : base(message, statusCode, response, headers, innerException)
            {
                Result = result;
            }
        }
    }
}
Read more comments on GitHub >

github_iconTop Results From Across the Web

Developers - 'Missing ApiException' compiler error -
Currently I have a project that uses multiple openapi.json v3 files. We used the official openapi tools described in this link: ...
Read more >
ApiException required Throwable, found CustomException
When I do this the compiler is complaining telling me that the types are incompatible and that it's expecting Throwable but it found...
Read more >
GetIdeas example - ApiException not instance of Throwable?
I ask because the error that java.rmi.RemoteException is not in your classpath indicates that you are missing some key dependencies. If it helps,...
Read more >
» Compilation errors
error, 198, Argument $method is missing from the Docblock of getUrl ... error, 5, No summary for class \mixisLv\Reamaze\Exceptions\ApiException ...
Read more >
Reference to type 'Task<>' claims it is defined in 'System. ...
Hi, I'm liking the idea of the new Checkout API (it's just what I need), and after following through the Quick Start guide...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found