RicoSuter / NSwag

The Swagger/OpenAPI toolchain for .NET, ASP.NET Core and TypeScript.
http://NSwag.org
MIT License
6.69k stars 1.24k forks source link

Error during code generation - NullReferenceException #3219

Open thomasjoscht opened 3 years ago

thomasjoscht commented 3 years ago

Hey,

I've a problem with my openapi.json file openapi.zip for a MicroStrategy REST API. During Code generation an error occurs.

System.InvalidOperationException: Error while rendering Liquid template CSharp/Client.Class: 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.

Runtime: Net50 at NSwag.CodeGeneration.Models.OperationModelBase`2.get_UnwrappedResultType() in C:\projects\nswag\src\NSwag.CodeGeneration\Models\OperationModelBase.cs:line 120 at NSwag.CodeGeneration.CSharp.Models.CSharpOperationModel.get_SyncResultType() in C:\projects\nswag\src\NSwag.CodeGeneration.CSharp\Models\CSharpOperationModel.cs:line 127

Do you have any hint why this happens? Perhaps the file is invalid? Does NSwag have any verbose log oportunities for analyzing this problem? Thx a lot.

Regards Thomas

thomasjoscht commented 3 years ago

Hey,

I've found two problems:

Wrong build type

The build action of file Client.Class.Annotations in NSwag.CodeGeneration.CSharp/Templates is wrong. It is set to nothing instead of "embedded". This must be fixed.

Missing schema handling

In class OperationModelBase.UnwrappedResultType no check for missing schema exists. In my case the openapi.json is a little bit invalid because of sometimes missing schema definitions. Perhaps this should be fixed for a default to string or void.

Regards Thomas