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 generating code for AppCenter API (System.InvalidOperationException: The properties 'id', 'name' are defined multiple times.) #3369

Open mamift opened 3 years ago

mamift commented 3 years ago

Am trying to generate a C# rest client for the Visual Studio AppCenter API (the API itself can be browsed at: https://openapi.appcenter.ms/#/) as there's no official C#/.NET client.

Command line (this is called in Visual Studio when the build command is invoked): C:\Users\buildrunner\.nuget\packages\nswag.msbuild\13.0.5\build\../tools/Win/NSwag.exe openapi2csclient /className:AppCenterRestApi /namespace:AppCenterApi /input:C:\Users\buildrunner\source\repos\AppCenterRestClient\swagger.json /output:C:\Users\buildrunner\source\repos\AppCenterRestClient\obj\swaggerClient.cs

JSON input file is from https://openapi.appcenter.ms/#/, or viewed directly via https://api.appcenter.ms/preview/swagger.json

Stack trace:

System.InvalidOperationException: The properties 'id', 'name' are defined multiple times.
   at NJsonSchema.JsonSchema.get_ActualProperties()
   at NJsonSchema.CodeGeneration.CSharp.Models.ClassTemplateModel..ctor(String typeName, CSharpGeneratorSettings settings, CSharpTypeResolver resolver, JsonSchema schema, Object rootObject)
   at NJsonSchema.CodeGeneration.CSharp.CSharpGenerator.GenerateClass(JsonSchema schema, String typeName)
   at NJsonSchema.CodeGeneration.GeneratorBase.GenerateTypes()
   at NJsonSchema.CodeGeneration.CSharp.CSharpGenerator.GenerateTypes()
   at NSwag.CodeGeneration.ClientGeneratorBase`3.GenerateFile(ClientGeneratorOutputType outputType)
   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 NConsole.CommandLineProcessor.<ProcessSingleAsync>d__12.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   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)
vppetrov commented 3 years ago

I had a similar problem when I had a class hierarchy: A -> B -> C. B was the class defining a property and I was getting the same error. I was able to fix it by using [JsonSchemaFlatten] according to https://github.com/RicoSuter/NJsonSchema/wiki/Inheritance (note that class hierarchy is lost for the client).

mamift commented 3 years ago

I had a similar problem when I had a class hierarchy: A -> B -> C. B was the class defining a property and I was getting the same error. I was able to fix it by using [JsonSchemaFlatten] according to https://github.com/RicoSuter/NJsonSchema/wiki/Inheritance (note that class hierarchy is lost for the client).

Interesting. You wouldn't happen to know how to pass that same param to the NSwag.exe tool would you?

RicoSuter commented 3 years ago

You have to put it on the class in your code and NSwag.exe picks this up... there is a global configuration where you can flatten everything without touching the code - not sure this is what you want though

mamift commented 3 years ago

You have to put it on the class in your code and NSwag.exe picks this up... there is a global configuration where you can flatten everything without touching the code - not sure this is what you want though

OK but my input is a JSON schema, as I'm trying to generate C# code for the REST client. The command I'm trying to run is NSwag.exe openapi2csclient /className:AppCenterRestApi /namespace:AppCenterApi /input:C:\Users\mamift\source\repos\AppCenterRestClient\swagger.json /output:C:\Users\mamift\source\repos\AppCenterRestClient\swaggerClient.cs

Is there any interface in the command line that can be used to pass that flag to flatten the classes? When I go NSwag.exe help openapi2csclient I cannot find a flag that refers to class flattening.

PatrickMatthiesen commented 1 year ago

I'm having a similar issue trying to make a client using the dotnet-openapi tool, it complains about "fireRate" existing multiple times. Though loading the spec using the swagger editor I find no issues with the file.

OpenApi spec Url: https://docs.warframestat.us/openapi.json

System.InvalidOperationException: The properties 'fireRate' are defined multiple times.
     at NJsonSchema.JsonSchema.get_ActualProperties()
     at NJsonSchema.CodeGeneration.CSharp.Models.ClassTemplateModel..ctor(String typeName, CSharpGeneratorSettings sett
  ings, CSharpTypeResolver resolver, JsonSchema schema, Object rootObject)
     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 /_/src/NSwag.CodeGeneration.CSharp/CSharp
  GeneratorBase.cs:line 106
     at NSwag.CodeGeneration.ClientGeneratorBase`3.GenerateFile(ClientGeneratorOutputType outputType) in /_/src/NSwag.C
  odeGeneration/ClientGeneratorBase.cs:line 75
     at NSwag.Commands.CodeGeneration.SwaggerToCSharpClientCommand.RunAsync() in /_/src/NSwag.Commands/Commands/CodeGen
  eration/OpenApiToCSharpClientCommand.cs:line 270
     at NSwag.Commands.CodeGeneration.SwaggerToCSharpClientCommand.RunAsync(CommandLineProcessor processor, IConsoleHos
  t host) in /_/src/NSwag.Commands/Commands/CodeGeneration/OpenApiToCSharpClientCommand.cs:line 248
     at NConsole.CommandLineProcessor.ProcessSingleAsync(String[] args, Object input)
     at NConsole.CommandLineProcessor.ProcessAsync(String[] args, Object input)
     at NSwag.Commands.NSwagCommandProcessor.ProcessAsync(String[] args) in /_/src/NSwag.Commands/NSwagCommandProcessor
  .cs:line 61