NSwag icon indicating copy to clipboard operation
NSwag copied to clipboard

Visual Studio 2022 - NullReferenceException

Open fairking opened this issue 2 years ago • 3 comments

  1. Open Visual Studio 2022
  2. Create a new c# Library project .netstandart20
  3. 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. image image
  4. Try to compile the project, so it should generate Client Api.
  5. 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 ==========

fairking avatar Apr 12 '22 09:04 fairking

For those who has the same error the following workaround might help:

I used the swagger-codegen and it worked fine.

  1. Install Java on your Windows machine here.
  2. Download the swagger-codegen jar file here.
  3. 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.

fairking avatar Apr 12 '22 10:04 fairking

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	

fairking avatar May 13 '22 16:05 fairking

Hello, i had the same problem on this version. I updated to the latest version of nswag to resolve my issue.

stephenc01 avatar Aug 08 '22 16:08 stephenc01

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 image

@fairking check this issue if it is not the same. https://github.com/RicoSuter/NSwag/issues/3126#issuecomment-1345612679

janseris avatar Dec 11 '22 16:12 janseris