Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
galvesribeiro opened this issue Feb 14, 2017 · 8 comments
Open

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

galvesribeiro opened this issue Feb 14, 2017 · 8 comments

Comments

@galvesribeiro
Copy link

galvesribeiro commented Feb 14, 2017

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
Copy link
Owner

@galvesribeiro
Copy link
Author

Hello @RSuter any idea when it will be merged?

@RicoSuter
Copy link
Owner

There are some more problems...

@RicoSuter
Copy link
Owner

@RicoSuter
Copy link
Owner

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

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

@galvesribeiro
Copy link
Author

@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
Copy link
Author

@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
Copy link
Owner

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

RicoSuter/NJsonSchema#17

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants