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

System.InvalidOperationException: Error while rendering Liquid template CSharp/Class: System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. ---> System.OverflowException: Value was either too large or too small for an Int32. #3778

Open stevendonline opened 2 years ago

stevendonline commented 2 years ago

when generating c# client with a large swagger.json file (>10000 lines)

System.InvalidOperationException: Error while rendering Liquid template CSharp/Class: System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. ---> System.OverflowException: Value was either too large or too small for an Int32.

Runtime: Net50 at System.Decimal.ToInt32(Decimal d) at System.Decimal.System.IConvertible.ToInt32(IFormatProvider provider) at System.Convert.ToInt32(Object value) at NJsonSchema.CodeGeneration.CSharp.CSharpValueGenerator.GetNumericValue(JsonObjectType type, Object value, String format) at NJsonSchema.CodeGeneration.CSharp.Models.PropertyModel.get_RangeMaximumValue() --- End of inner exception stack trace --- at System.RuntimeMethodHandle.InvokeMethod(Object target, Object[] arguments, Signature sig, Boolean constructor, Boolean wrapExceptions) at System.Reflection.RuntimePropertyInfo.GetValue(Object obj, Object[] index) at NJsonSchema.CodeGeneration.LiquidProxyHash.GetValue(String key) at DotLiquid.Context.LookupAndEvaluate(Object obj, Object key) at DotLiquid.Context.Variable(String markup, Boolean notifyNotFound) at DotLiquid.Context.Resolve(String key, Boolean notifyNotFound) at DotLiquid.Variable.RenderInternal(Context context) at DotLiquid.Variable.Render(Context context, TextWriter result) at DotLiquid.Block.RenderAll(List1 list, Context context, TextWriter result) --- End of stack trace from previous location --- at DotLiquid.Context.HandleError(Exception ex) at DotLiquid.Block.RenderAll(List1 list, Context context, TextWriter result) at DotLiquid.Tags.If.<>cDisplayClass11_0.b0() at DotLiquid.Context.Stack(Hash newScope, Action callback) at DotLiquid.Block.RenderAll(List1 list, Context context, TextWriter result) --- End of stack trace from previous location --- at DotLiquid.Context.HandleError(Exception ex) at DotLiquid.Block.RenderAll(List1 list, Context context, TextWriter result) at DotLiquid.Tags.For.<>cDisplayClass8_0.b0() at DotLiquid.Context.Stack(Hash newScope, Action callback) at DotLiquid.Tags.For.Render(Context context, TextWriter result) at DotLiquid.Block.RenderAll(List1 list, Context context, TextWriter result) --- End of stack trace from previous location --- at DotLiquid.Context.HandleError(Exception ex) at DotLiquid.Block.RenderAll(List1 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() ---> System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. ---> System.OverflowException: Value was either too large or too small for an Int32. at System.Decimal.ToInt32(Decimal d) at System.Decimal.System.IConvertible.ToInt32(IFormatProvider provider) at System.Convert.ToInt32(Object value) at NJsonSchema.CodeGeneration.CSharp.CSharpValueGenerator.GetNumericValue(JsonObjectType type, Object value, String format) at NJsonSchema.CodeGeneration.CSharp.Models.PropertyModel.get_RangeMaximumValue() --- End of inner exception stack trace --- at System.RuntimeMethodHandle.InvokeMethod(Object target, Object[] arguments, Signature sig, Boolean constructor, Boolean wrapExceptions) at System.Reflection.RuntimePropertyInfo.GetValue(Object obj, Object[] index) at NJsonSchema.CodeGeneration.LiquidProxyHash.GetValue(String key) at DotLiquid.Context.LookupAndEvaluate(Object obj, Object key) at DotLiquid.Context.Variable(String markup, Boolean notifyNotFound) at DotLiquid.Context.Resolve(String key, Boolean notifyNotFound) at DotLiquid.Variable.RenderInternal(Context context) at DotLiquid.Variable.Render(Context context, TextWriter result) at DotLiquid.Block.RenderAll(List1 list, Context context, TextWriter result) --- End of stack trace from previous location --- at DotLiquid.Context.HandleError(Exception ex) at DotLiquid.Block.RenderAll(List1 list, Context context, TextWriter result) at DotLiquid.Tags.If.<>cDisplayClass11_0.b0() at DotLiquid.Context.Stack(Hash newScope, Action callback) at DotLiquid.Block.RenderAll(List1 list, Context context, TextWriter result) --- End of stack trace from previous location --- at DotLiquid.Context.HandleError(Exception ex) at DotLiquid.Block.RenderAll(List1 list, Context context, TextWriter result) at DotLiquid.Tags.For.<>cDisplayClass8_0.b0() at DotLiquid.Context.Stack(Hash newScope, Action callback) at DotLiquid.Tags.For.Render(Context context, TextWriter result) at DotLiquid.Block.RenderAll(List1 list, Context context, TextWriter result) --- End of stack trace from previous location --- at DotLiquid.Context.HandleError(Exception ex) at DotLiquid.Block.RenderAll(List1 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 NJsonSchema.CodeGeneration.CodeArtifact..ctor(String typeName, String baseTypeName, CodeArtifactType type, CodeArtifactLanguage language, CodeArtifactCategory category, ITemplate template) at NJsonSchema.CodeGeneration.CSharp.CSharpGenerator.GenerateClass(JsonSchema schema, String typeName) at NJsonSchema.CodeGeneration.CSharp.CSharpGenerator.GenerateType(JsonSchema schema, String typeNameHint) at NJsonSchema.CodeGeneration.GeneratorBase.GenerateTypes() at NJsonSchema.CodeGeneration.CSharp.CSharpGenerator.GenerateTypes() at NSwag.CodeGeneration.CSharp.CSharpGeneratorBase.GenerateDtoTypes() in D:\a\NSwag\NSwag\src\NSwag.CodeGeneration.CSharp\CSharpGeneratorBase.cs:line 106 at NSwag.CodeGeneration.ClientGeneratorBase`3.GenerateFile(ClientGeneratorOutputType outputType) in D:\a\NSwag\NSwag\src\NSwag.CodeGeneration\ClientGeneratorBase.cs:line 82 at NSwag.Commands.CodeGeneration.SwaggerToCSharpClientCommand.b__95_0() in D:\a\NSwag\NSwag\src\NSwag.Commands\Commands\CodeGeneration\OpenApiToCSharpClientCommand.cs:line 275 at NSwag.Commands.CodeGeneration.SwaggerToCSharpClientCommand.RunAsync(CommandLineProcessor processor, IConsoleHost host) in D:\a\NSwag\NSwag\src\NSwag.Commands\Commands\CodeGeneration\OpenApiToCSharpClientCommand.cs:line 248 at NSwag.Commands.NSwagDocument.ExecuteAsync() in D:\a\NSwag\NSwag\src\NSwag.Commands\NSwagDocument.cs:line 85 at NSwag.Commands.Document.ExecuteDocumentCommand.ExecuteDocumentAsync(IConsoleHost host, String filePath) in D:\a\NSwag\NSwag\src\NSwag.Commands\Commands\Document\ExecuteDocumentCommand.cs:line 85 at NSwag.Commands.Document.ExecuteDocumentCommand.RunAsync(CommandLineProcessor processor, IConsoleHost host) in D:\a\NSwag\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 D:\a\NSwag\NSwag\src\NSwag.Commands\NSwagCommandProcessor.cs:line 55

RicoSuter commented 2 years ago

Did this work with a previous version?

RicoSuter commented 2 years ago

It seems that one of your "maximum" values is "integer" but bigger than int32 can support...

stevendonline commented 2 years ago

@RicoSuter , Thanks for the clue. The problem is with the time: "timestamp": { "description": "Epoch timestamp of interest for non-revocation proof", "example": 1640995199, "format": "int32", "maximum": 18446744073709551615, "minimum": 0, "type": "integer" } It would need long or Int64. Is there anything the tool can do to cope with the issue? the API is a third party API with limited support

RicoSuter commented 2 years ago

I'd say this spec is essentially invalid as the maximum does not fit into the type... We could for example ignore the maximum in this case etc... but this is all hacky. For me the best solution for now is that you preprocess this spec (eg remove this maximum) before you pass it into the code gen...

Znadek commented 1 year ago

I have a similar issue and i have fixed unflagging "Generate data annotation attributes"

PeaStew commented 1 year ago

Hi, the spec here actually says that integer can be a signed int32 or int64 https://swagger.io/docs/specification/data-models/data-types/

So actually the system really should be able to adapt.

image