NSwag
NSwag copied to clipboard
Visual Studio 2022 - NullReferenceException
- Open Visual Studio 2022
- Create a new c# Library project .netstandart20
- Add a new OpenApi service reference with the following api url: https://eazycollectservices.github.io/EazyCollectAPIv3/swagger.json and c# language selected, also specify some namespace and class name.
- Try to compile the project, so it should generate Client Api.
- In Output window you have the following error:
1>------ Rebuild All started: Project: WebRef.AccessPay, Configuration: Debug Any CPU ------
1>
1>GenerateNSwagCSharp:
1> "C:\Users\Admin\.nuget\packages\nswag.msbuild\13.0.5\build\../tools/Win/NSwag.exe" openapi2csclient /className:AccessPay /namespace:WebRef.AccessPay /input:C:\Code\ProjectFutureState\WebReferences\WebRef.AccessPay\OpenAPIs\swagger.json /output:obj\swaggerClient.cs
1>NSwag command line tool for .NET 4.6.1+ WinX64, toolchain v13.0.5.0 (NJsonSchema v10.0.22.0 (Newtonsoft.Json v11.0.0.0))
1>Visit http://NSwag.org for more information.
1>NSwag bin directory: C:\Users\Admin\.nuget\packages\nswag.msbuild\13.0.5\tools\Win
1>System.InvalidOperationException: Error while rendering Liquid template CSharp/Client.Class:
1>System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. ---> System.NullReferenceException: Object reference not set to an instance of an object.
1> at NSwag.CodeGeneration.Models.OperationModelBase`2.get_UnwrappedResultType()
1> at NSwag.CodeGeneration.CSharp.Models.CSharpOperationModel.get_ResultType()
1> --- End of inner exception stack trace ---
1> at DotLiquid.Context.HandleError(Exception ex)
1> at DotLiquid.Block.RenderAll(List`1 list, Context context, TextWriter result)
1> at DotLiquid.Document.Render(Context context, TextWriter result)
1> at DotLiquid.Template.RenderInternal(TextWriter result, RenderParameters parameters)
1> at DotLiquid.Template.Render(TextWriter writer, RenderParameters parameters)
1> at DotLiquid.Template.Render(RenderParameters parameters)
1> at NJsonSchema.CodeGeneration.DefaultTemplateFactory.LiquidTemplate.Render() ---> System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. ---> System.NullReferenceException: Object reference not set to an instance of an object.
1> at NSwag.CodeGeneration.Models.OperationModelBase`2.get_UnwrappedResultType()
1> at NSwag.CodeGeneration.CSharp.Models.CSharpOperationModel.get_ResultType()
1> --- End of inner exception stack trace ---
1> at DotLiquid.Context.HandleError(Exception ex)
1> at DotLiquid.Block.RenderAll(List`1 list, Context context, TextWriter result)
1> at DotLiquid.Document.Render(Context context, TextWriter result)
1> at DotLiquid.Template.RenderInternal(TextWriter result, RenderParameters parameters)
1> at DotLiquid.Template.Render(TextWriter writer, RenderParameters parameters)
1> at DotLiquid.Template.Render(RenderParameters parameters)
1> at NJsonSchema.CodeGeneration.DefaultTemplateFactory.LiquidTemplate.Render()
1> --- End of inner exception stack trace ---
1> at NJsonSchema.CodeGeneration.DefaultTemplateFactory.LiquidTemplate.Render()
1> at NSwag.CodeGeneration.CSharp.CSharpClientGenerator.<GenerateClientTypes>d__8.MoveNext()
1> at System.Collections.Generic.List`1.InsertRange(Int32 index, IEnumerable`1 collection)
1> at NSwag.CodeGeneration.ClientGeneratorBase`3.GenerateAllClientTypes()
1> at NSwag.CodeGeneration.ClientGeneratorBase`3.GenerateFile(ClientGeneratorOutputType outputType)
1> at NSwag.Commands.CodeGeneration.SwaggerToCSharpClientCommand.<<RunAsync>b__83_0>d.MoveNext()
1>--- End of stack trace from previous location where exception was thrown ---
1> at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
1> at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
1> at NSwag.Commands.CodeGeneration.SwaggerToCSharpClientCommand.<RunAsync>d__83.MoveNext()
1>--- End of stack trace from previous location where exception was thrown ---
1> at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
1> at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
1> at NSwag.Commands.CodeGeneration.SwaggerToCSharpClientCommand.<RunAsync>d__82.MoveNext()
1>--- End of stack trace from previous location where exception was thrown ---
1> at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
1> at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
1> at NConsole.CommandLineProcessor.<ProcessSingleAsync>d__12.MoveNext()
1>--- End of stack trace from previous location where exception was thrown ---
1> at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
1> at NConsole.CommandLineProcessor.<ProcessAsync>d__11.MoveNext()
1>--- End of stack trace from previous location where exception was thrown ---
1> at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
1> at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
1> at NConsole.CommandLineProcessor.Process(String[] args, Object input)
1> at NSwag.Commands.NSwagCommandProcessor.Process(String[] args)
1>C:\Users\Admin\.nuget\packages\nswag.apidescription.client\13.0.5\build\NSwag.ApiDescription.Client.targets(28,5): error MSB3073: The command ""C:\Users\Admin\.nuget\packages\nswag.msbuild\13.0.5\build\../tools/Win/NSwag.exe" openapi2csclient /className:AccessPay /namespace:WebRef.AccessPay /input:C:\Code\ProjectFutureState\WebReferences\WebRef.AccessPay\OpenAPIs\swagger.json /output:obj\swaggerClient.cs " exited with code -1.
1>Done building project "WebRef.AccessPay.csproj" -- FAILED.
========== Rebuild All: 0 succeeded, 1 failed, 0 skipped ==========
For those who has the same error the following workaround might help:
I used the swagger-codegen and it worked fine.
- Install Java on your Windows machine here.
- Download the swagger-codegen jar file here.
- Run the following command:
cd "C:\Program Files (x86)\Java\jre1.8.0_321\bin"
java -jar "C:\Users\Admin\Downloads\swagger-codegen-cli-2.4.26.jar" generate -i https://eazycollectservices.github.io/EazyCollectAPIv3/swagger.json -l csharp -o C:\Code\MyProject
It will generate everything for you. You can also look at the other options by the following command: java -jar "C:\Users\Admin\Downloads\swagger-codegen-cli-2.4.26.jar" config-help -l csharp
.
where the C:\Code\MyProject
is your project folder.
Hope it will save someone's life.
This is the command VS executes:
Severity Code Description Project File Line Suppression State
Error MSB3073 The command ""C:\Users\Admin\.nuget\packages\nswag.msbuild\13.0.5\build\../tools/Win/NSwag.exe" openapi2csclient /className:Api /namespace:WebRef.AccessPay /input:C:\Code\WebApplication1\WebApplication1\OpenAPIs\swagger.json /output:obj\swaggerClient.cs " exited with code -1. WebApplication1 C:\Users\Admin\.nuget\packages\nswag.apidescription.client\13.0.5\build\NSwag.ApiDescription.Client.targets 28
Hello, i had the same problem on this version. I updated to the latest version of nswag to resolve my issue.
For OpenAPI 3 definition you will need this version https://repo1.maven.org/maven2/io/swagger/codegen/v3/swagger-codegen-cli/3.0.20/swagger-codegen-cli-3.0.20.jar
But the same can be easily done via https://editor.swagger.io/
using this menu
@fairking check this issue if it is not the same. https://github.com/RicoSuter/NSwag/issues/3126#issuecomment-1345612679