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

Failure trying to generate C# classes from Json file #591

Open galvesribeiro opened 7 years ago

galvesribeiro commented 7 years ago

I'm trying to use NSwag (both UI and CLI) to generate C# classes from https://docs.docker.com/engine/api/v1.26/swagger.yaml

I converted it to Json using http://editor.swagger.io and the converted file is attached (renamed to txt). swagger.txt

The error I have is as follow:

System.ArgumentOutOfRangeException: Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index
   at System.ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument argument, ExceptionResource resource)
   at System.Collections.Generic.List`1.get_Item(Int32 index)
   at NJsonSchema.CodeGeneration.DefaultValueGenerator.GetDefaultValue(JsonSchema4 schema, Boolean allowsNull, String targetType, String typeNameHint)
   at NJsonSchema.CodeGeneration.TypeScript.TypeScriptDefaultValueGenerator.GetDefaultValue(JsonSchema4 schema, Boolean allowsNull, String targetType, String typeNameHint)
   at NJsonSchema.CodeGeneration.Models.PropertyModelBase.get_DefaultValue()
   at CallSite.Target(Closure , CallSite , Object )
   at NJsonSchema.CodeGeneration.TypeScript.Templates.ClassTemplate.TransformText()
   at NJsonSchema.CodeGeneration.TypeScript.Templates.ClassTemplate.Render()
   at NJsonSchema.CodeGeneration.TypeScript.TypeScriptGenerator.GenerateType(String typeNameHint)
   at NJsonSchema.CodeGeneration.TypeResolverBase`1.GenerateTypes(ExtensionCode extensionCode)
   at NSwag.CodeGeneration.TypeScript.Models.TypeScriptFileTemplateModel..ctor(String clientCode, IEnumerable`1 clientClasses, SwaggerDocument document, TypeScriptExtensionCode extensionCode, SwaggerToTypeScriptClientGeneratorSettings settings, TypeScriptTypeResolver resolver) in C:\projects\nswag\src\NSwag.CodeGeneration.TypeScript\Models\TypeScriptFileTemplateModel.cs:line 45
   at NSwag.CodeGeneration.TypeScript.SwaggerToTypeScriptClientGenerator.GenerateFile(String clientCode, IEnumerable`1 clientClasses, ClientGeneratorOutputType outputType) in C:\projects\nswag\src\NSwag.CodeGeneration.TypeScript\SwaggerToTypeScriptClientGenerator.cs:line 96
   at NSwag.CodeGeneration.ClientGeneratorBase`3.GenerateFile(SwaggerDocument document, ClientGeneratorOutputType type) in C:\projects\nswag\src\NSwag.CodeGeneration\ClientGeneratorBase.cs:line 102
   at NSwag.Commands.SwaggerToTypeScriptClientCommand.<<RunAsync>b__79_0>d.MoveNext() in C:\projects\nswag\src\NSwag.Commands\Commands\SwaggerToTypeScriptClientCommand.cs:line 215
--- 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.SwaggerToTypeScriptClientCommand.<RunAsync>d__79.MoveNext() in C:\projects\nswag\src\NSwag.Commands\Commands\SwaggerToTypeScriptClientCommand.cs:line 206
--- 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 NSwagStudio.ViewModels.CodeGenerators.SwaggerToTypeScriptClientGeneratorViewModel.<>c__DisplayClass31_1.<<GenerateClientAsync>b__1>d.MoveNext() in C:\projects\nswag\src\NSwagStudio\ViewModels\CodeGenerators\SwaggerToTypeScriptClientGeneratorViewModel.cs:line 143
--- 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 NSwagStudio.ViewModels.CodeGenerators.SwaggerToTypeScriptClientGeneratorViewModel.<>c__DisplayClass31_0.<<GenerateClientAsync>b__0>d.MoveNext() in C:\projects\nswag\src\NSwagStudio\ViewModels\CodeGenerators\SwaggerToTypeScriptClientGeneratorViewModel.cs:line 138
--- 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 MyToolkit.Mvvm.ViewModelBase.<>c__DisplayClass14_0.<<RunTaskAsync>b__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 MyToolkit.Mvvm.ViewModelBase.<RunTaskAsync>d__12`1.MoveNext()

What is wrong? Appreciate any help.

Thanks!

RicoSuter commented 7 years ago

https://github.com/NJsonSchema/NJsonSchema/commit/898be5d3ac33eae2d27549285dc024e44b76d0b3

galvesribeiro commented 7 years ago

Hello @rsuter any idea when it will be merged?

RicoSuter commented 7 years ago

There are some more problems...

RicoSuter commented 7 years ago

https://github.com/NJsonSchema/NJsonSchema/commit/bb408a45640cc5749baa9149a1e13f332ae66847

RicoSuter commented 7 years ago

Ok, with the latest build it works for me. Please try it yourself:

https://ci.appveyor.com/project/rsuter/nswag-25x6o/build/artifacts

galvesribeiro commented 7 years ago

@rsuter thank you very much for promptly reply. The error was fixed. Now I have the code generated properly. Thanks!

Side Note: The .msi from the build artifacts crash while installing. I took the .zip.

galvesribeiro commented 7 years ago

@rsuter I found a bug...

Types that comes from a $ref tag on json, are not being re-utilized... They are being duplicated for example look at Version. The C# code is havin Version1, 2, 3, X... One for each $ref...

RicoSuter commented 7 years ago

I couldn't find "Version1" in the generated code... I think the problem is that these enums are not referenced/reused with $ref.

https://github.com/NJsonSchema/NJsonSchema/issues/17