NSwag
NSwag copied to clipboard
Error while rendering Liquid template CSharp/Class
HI,
Here is the code I am using to generate the C# classed based on Json Schema 👍 var generator = new CSharpGenerator(schema); generator.Settings.ClassStyle = CSharpClassStyle.Poco; var file = generator.GenerateFile();
However I am getting an error at Line : 3 saying "Error while rendering Liquid template CSharp/Class."
Can you please me out.
Regards Kalyan
Can you provide the json schema?
@RicoSuter Same error here when trying to generate either TS or C# clients for these APIs
Note that at least ng-swagger-gen can generate a client for Angular from the same document with the default configuration
{
"$schema": "./node_modules/ng-swagger-gen/ng-swagger-gen-schema.json",
"swagger": "https://try.gitea.io/swagger.v1.json",
"output": "src/app/api",
"prefix": "Api",
"ignoreUnusedModels": true,
"minParamsForContainer": 2,
"sortParams": "desc",
"defaultTag": "Api",
"removeStaleFiles": true,
"modelIndex": true,
"serviceIndex": true,
"apiModule": true,
"enumModule": true,
"generateExamples": false,
"camelCase": false
}
C# Client
System.InvalidOperationException: Error while rendering Liquid template CSharp/File. ---> System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. ---> System.InvalidOperationException: Sequence contains more than one matching element
Runtime: Default
at System.Linq.Enumerable.SingleOrDefault[TSource](IEnumerable`1 source, Func`2 predicate)
at NSwag.SwaggerResponse.IsBinary(SwaggerOperation operation)
at NSwag.CodeGeneration.CSharp.Models.CSharpFileTemplateModel.<>c__DisplayClass31_0.<get_GenerateFileResponseClass>b__1(KeyValuePair`2 r)
at System.Linq.Enumerable.Any[TSource](IEnumerable`1 source, Func`2 predicate)
at System.Linq.Enumerable.Any[TSource](IEnumerable`1 source, Func`2 predicate)
--- End of inner exception stack trace ---
at DotLiquid.Context.HandleError(Exception ex)
at DotLiquid.Block.RenderAll(List`1 list, Context context, TextWriter result)
at DotLiquid.Document.Render(Context context, TextWriter result)
at DotLiquid.Template.RenderInternal(TextWriter result, RenderParameters parameters)
at DotLiquid.Template.Render(TextWriter writer, RenderParameters parameters)
at DotLiquid.Template.Render(RenderParameters parameters)
at NJsonSchema.CodeGeneration.DefaultTemplateFactory.LiquidTemplate.Render()
--- End of inner exception stack trace ---
at NJsonSchema.CodeGeneration.DefaultTemplateFactory.LiquidTemplate.Render()
at NSwag.CodeGeneration.ClientGeneratorBase`3.GenerateFile(SwaggerDocument document, ClientGeneratorOutputType type)
at NSwag.Commands.CodeGeneration.SwaggerToCSharpClientCommand.<<RunAsync>b__83_0>d.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at NSwag.Commands.CodeGeneration.SwaggerToCSharpClientCommand.<RunAsync>d__83.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at NSwag.Commands.CodeGeneration.SwaggerToCSharpClientCommand.<RunAsync>d__82.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at NSwag.Commands.NSwagDocument.<ExecuteAsync>d__8.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at NSwag.Commands.Document.ExecuteDocumentCommand.<ExecuteDocumentAsync>d__9.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at NSwag.Commands.Document.ExecuteDocumentCommand.<RunAsync>d__8.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at NConsole.CommandLineProcessor.<ProcessSingleAsync>d__12.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at NConsole.CommandLineProcessor.<ProcessAsync>d__11.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at NConsole.CommandLineProcessor.Process(String[] args, Object input)
at NSwag.Commands.NSwagCommandProcessor.Process(String[] args)
TypeScript Client
System.InvalidOperationException: Error while rendering Liquid template TypeScript/FetchClient. ---> System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. ---> System.InvalidOperationException: Sequence contains more than one matching element
Runtime: Default
at System.Linq.Enumerable.SingleOrDefault[TSource](IEnumerable`1 source, Func`2 predicate)
at NSwag.SwaggerResponse.IsBinary(SwaggerOperation operation)
at NSwag.SwaggerResponse.IsEmpty(SwaggerOperation operation)
at NSwag.CodeGeneration.Models.OperationModelBase`2.get_UnwrappedResultType()
at NSwag.CodeGeneration.TypeScript.Models.TypeScriptOperationModel.get_ResultType()
--- End of inner exception stack trace ---
at DotLiquid.Context.HandleError(Exception ex)
at DotLiquid.Block.RenderAll(List`1 list, Context context, TextWriter result)
at DotLiquid.Document.Render(Context context, TextWriter result)
at DotLiquid.Template.RenderInternal(TextWriter result, RenderParameters parameters)
at DotLiquid.Template.Render(TextWriter writer, RenderParameters parameters)
at DotLiquid.Template.Render(RenderParameters parameters)
at NJsonSchema.CodeGeneration.DefaultTemplateFactory.LiquidTemplate.Render()
--- End of inner exception stack trace ---
at NJsonSchema.CodeGeneration.DefaultTemplateFactory.LiquidTemplate.Render()
at NSwag.CodeGeneration.ClientGeneratorBase`3.GenerateFile(SwaggerDocument document, ClientGeneratorOutputType type)
at NSwag.Commands.CodeGeneration.SwaggerToTypeScriptClientCommand.<<RunAsync>b__138_0>d.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at NSwag.Commands.CodeGeneration.SwaggerToTypeScriptClientCommand.<RunAsync>d__138.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at NSwag.Commands.CodeGeneration.SwaggerToTypeScriptClientCommand.<RunAsync>d__137.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at NSwag.Commands.NSwagDocument.<ExecuteAsync>d__8.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at NSwag.Commands.Document.ExecuteDocumentCommand.<ExecuteDocumentAsync>d__9.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at NSwag.Commands.Document.ExecuteDocumentCommand.<RunAsync>d__8.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at NConsole.CommandLineProcessor.<ProcessSingleAsync>d__12.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at NConsole.CommandLineProcessor.<ProcessAsync>d__11.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at NConsole.CommandLineProcessor.Process(String[] args, Object input)
at NSwag.Commands.NSwagCommandProcessor.Process(String[] args)
This spec references into "responses" which is not a documented property:
"basePath": "/api/v1", ... "responses": { "AccessToken": {
This is why it's not parsed correctly...
Not that easy to fix...
With OpenApi 3 you'd reference into /components/responses/... but with Swagger 2.0 this is probably not allowed...
I get this on an openapi3.0 spec:
https://app.swaggerhub.com/apiproxy/registry/Actimo/Actimo/1.2.2
CSharp Controller gets generated ok. But I get this error when trying to generated the CSharp Client.
Getting the same issue on https://austestr56.dayforcehcm.com/api/mhj/swagger/docs/v1
Getting issue with https://leksaker.starweb.se/api/v2/openapi.json as well.
Unhandled exception. System.AggregateException: One or more errors occurred. (Error while rendering Liquid template CSharp/Client.Class:
System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation.
---> System.InvalidOperationException: Sequence contains more than one matching element
at System.Linq.ThrowHelper.ThrowMoreThanOneMatchException()
at System.Linq.Enumerable.SingleOrDefault[TSource](IEnumerable1 source, Func
2 predicate)
at NSwag.OpenApiResponse.IsBinary(OpenApiOperation operation)
at NSwag.CodeGeneration.Models.ResponseModelBase.get_IsFile()
at NSwag.CodeGeneration.Models.ResponseModelBase.get_CheckChunkedStatusCode()
--- End of inner exception stack trace ---
at DotLiquid.Context.HandleError(Exception ex)
at DotLiquid.Block.RenderAll(List1 list, Context context, TextWriter result) at DotLiquid.Block.Render(Context context, TextWriter result) at DotLiquid.Document.Render(Context context, TextWriter result) at DotLiquid.Template.RenderInternal(TextWriter result, RenderParameters parameters) at DotLiquid.Template.Render(TextWriter writer, RenderParameters parameters) at DotLiquid.Template.Render(RenderParameters parameters) at NJsonSchema.CodeGeneration.DefaultTemplateFactory.LiquidTemplate.Render()) ---> System.InvalidOperationException: Error while rendering Liquid template CSharp/Client.Class: System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. ---> System.InvalidOperationException: Sequence contains more than one matching element at System.Linq.ThrowHelper.ThrowMoreThanOneMatchException() at System.Linq.Enumerable.SingleOrDefault[TSource](IEnumerable
1 source, Func`2 predicate)
at NSwag.OpenApiResponse.IsBinary(OpenApiOperation operation)
at NSwag.CodeGeneration.Models.ResponseModelBase.get_IsFile()
at NSwag.CodeGeneration.Models.ResponseModelBase.get_CheckChunkedStatusCode()
--- End of inner exception stack trace ---
Same issue on this spec (3.0.1):
https://service.inaras.be/octopus-rest-api/v1/openapi.json
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: NetCore31 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 122 at NSwag.CodeGeneration.CSharp.Models.CSharpOperationModel.get_ResultType() in C:\projects\nswag\src\NSwag.CodeGeneration.CSharp\Models\CSharpOperationModel.cs:line 131 --- End of inner exception stack trace --- at DotLiquid.Context.HandleError(Exception ex) at DotLiquid.Block.RenderAll(List
1 list, Context context, TextWriter result) at DotLiquid.Block.Render(Context context, TextWriter result) at DotLiquid.Document.Render(Context context, TextWriter result) at DotLiquid.Template.RenderInternal(TextWriter result, RenderParameters parameters) at DotLiquid.Template.Render(TextWriter writer, RenderParameters parameters) at DotLiquid.Template.Render(RenderParameters parameters) 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. at NSwag.CodeGeneration.Models.OperationModelBase2.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 122 at NSwag.CodeGeneration.CSharp.Models.CSharpOperationModel.get_ResultType() in C:\projects\nswag\src\NSwag.CodeGeneration.CSharp\Models\CSharpOperationModel.cs:line 131 --- End of inner exception stack trace --- at DotLiquid.Context.HandleError(Exception ex) at DotLiquid.Block.RenderAll(List
1 list, Context context, TextWriter result) at DotLiquid.Block.Render(Context context, TextWriter result) at DotLiquid.Document.Render(Context context, TextWriter result) at DotLiquid.Template.RenderInternal(TextWriter result, RenderParameters parameters) at DotLiquid.Template.Render(TextWriter writer, RenderParameters parameters) at DotLiquid.Template.Render(RenderParameters parameters) at NJsonSchema.CodeGeneration.DefaultTemplateFactory.LiquidTemplate.Render() --- End of inner exception stack trace --- at NJsonSchema.CodeGeneration.DefaultTemplateFactory.LiquidTemplate.Render() at NJsonSchema.CodeGeneration.CodeArtifact..ctor(String typeName, CodeArtifactType type, CodeArtifactLanguage language, CodeArtifactCategory category, ITemplate template) at NSwag.CodeGeneration.CSharp.CSharpClientGenerator.GenerateClientTypes(String controllerName, String controllerClassName, IEnumerable1 operations)+MoveNext() in C:\projects\nswag\src\NSwag.CodeGeneration.CSharp\CSharpClientGenerator.cs:line 68 at System.Collections.Generic.List
1.InsertRange(Int32 index, IEnumerable1 collection) at NSwag.CodeGeneration.ClientGeneratorBase
3.GenerateAllClientTypes() in C:\projects\nswag\src\NSwag.CodeGeneration\ClientGeneratorBase.cs:line 112 at NSwag.CodeGeneration.ClientGeneratorBase`3.GenerateFile(ClientGeneratorOutputType outputType) in C:\projects\nswag\src\NSwag.CodeGeneration\ClientGeneratorBase.cs:line 73 at NSwag.Commands.CodeGeneration.SwaggerToCSharpClientCommand.<RunAsync>b__89_0() in C:\projects\nswag\src\NSwag.Commands\Commands\CodeGeneration\OpenApiToCSharpClientCommand.cs:line 258 at NSwag.Commands.CodeGeneration.SwaggerToCSharpClientCommand.RunAsync() in C:\projects\nswag\src\NSwag.Commands\Commands\CodeGeneration\OpenApiToCSharpClientCommand.cs:line 244 at NSwag.Commands.CodeGeneration.SwaggerToCSharpClientCommand.RunAsync(CommandLineProcessor processor, IConsoleHost host) in C:\projects\nswag\src\NSwag.Commands\Commands\CodeGeneration\OpenApiToCSharpClientCommand.cs:line 233 at NSwag.Commands.NSwagDocument.ExecuteAsync() in C:\projects\nswag\src\NSwag.Commands\NSwagDocument.cs:line 85 at NSwag.Commands.Document.ExecuteDocumentCommand.ExecuteDocumentAsync(IConsoleHost host, String filePath) in C:\projects\nswag\src\NSwag.Commands\Commands\Document\ExecuteDocumentCommand.cs:line 86 at NSwag.Commands.Document.ExecuteDocumentCommand.RunAsync(CommandLineProcessor processor, IConsoleHost host) in C:\projects\nswag\src\NSwag.Commands\Commands\Document\ExecuteDocumentCommand.cs:line 32 at NConsole.CommandLineProcessor.ProcessSingleAsync(String[] args, Object input) at NConsole.CommandLineProcessor.ProcessAsync(String[] args, Object input) at NConsole.CommandLineProcessor.Process(String[] args, Object input) at NSwag.Commands.NSwagCommandProcessor.Process(String[] args) in C:\projects\nswag\src\NSwag.Commands\NSwagCommandProcessor.cs:line 56
Likewise. Any fix?
Cannot reproduce with latest version:
I'm getting this same error when trying to generate a C# controller with the Amazon Seller API. Link to swagger.json here https://github.com/amzn/selling-partner-api-models/blob/main/models/catalog-items-api-model/catalogItems_2022-04-01.json