RicoSuter / NSwag

The Swagger/OpenAPI toolchain for .NET, ASP.NET Core and TypeScript.
http://NSwag.org
MIT License
6.61k stars 1.22k forks source link

Multiple Controllers - NSwag.MSBuild.CodeGeneration code generation #1896

Open grahamehorner opened 5 years ago

grahamehorner commented 5 years ago

When the csproj when multiple controllers the NSwag.MSBuild.CodeGeneration fails to generate the client cs code; only the json file is created.

RicoSuter commented 5 years ago

Is there an error in the build output?

grahamehorner commented 5 years ago

@RSuter please see below the build output

> 1>------ Build started: Project: BigChange.JobWatch.WebGUI, Configuration: Debug Any CPU ------
> 2>------ Build started: Project: BigChange.JobWatch.WebAPI, Configuration: Debug Any CPU ------
> 2>BigChange.JobWatch.WebAPI -> C:\Users\GrahameHorner\source\repos\BigChange.JobWatch\src\BigChange.JobWatch.WebAPI\bin\Debug\netcoreapp2.2\BigChange.JobWatch.WebAPI.dll
> 1>
> 1>GenerateNSwagCSharp:
> 1>  "C:\Users\GrahameHorner\.nuget\packages\nswag.msbuild\12.0.0\build\../tools/Win/NSwag.exe" swagger2csclient /className:WebClient /namespace:BigChange.JobWatch.WebGUI /input:C:\Users\GrahameHorner\source\repos\BigChange.JobWatch\src\BigChange.JobWatch.WebGUI\BigChange.JobWatch.WebAPI.v1.json /output:WebClient.cs
> 1>NSwag command line tool for .NET 4.6.1+ WinX64, toolchain v12.0.0.0 (NJsonSchema v9.12.2.0 (Newtonsoft.Json v11.0.0.0))
> 1>Visit http://NSwag.org for more information.
> 1>NSwag bin directory: C:\Users\GrahameHorner\.nuget\packages\nswag.msbuild\12.0.0\tools\Win
> 1>System.NullReferenceException: Object reference not set to an instance of an object.
> 1>   at NSwag.SwaggerDocument.<>c__DisplayClass57_0.<FromJsonAsync>b__0(SwaggerDocument document)
> 1>   at NJsonSchema.Infrastructure.JsonSchemaSerialization.<FromJsonAsync>d__10`1.MoveNext()
> 1>--- End of stack trace from previous location where exception was thrown ---
> 1>   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
> 1>   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
> 1>   at NSwag.SwaggerDocument.<FromJsonAsync>d__57.MoveNext()
> 1>--- End of stack trace from previous location where exception was thrown ---
> 1>   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
> 1>   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
> 1>   at NSwag.SwaggerDocument.<FromFileAsync>d__58.MoveNext()
> 1>--- End of stack trace from previous location where exception was thrown ---
> 1>   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
> 1>   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
> 1>   at NSwag.Commands.OutputCommandBase.<ReadSwaggerDocumentAsync>d__5.MoveNext()
> 1>--- End of stack trace from previous location where exception was thrown ---
> 1>   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
> 1>   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
> 1>   at NSwag.Commands.InputOutputCommandBase.<GetInputSwaggerDocument>d__12.MoveNext()
> 1>--- End of stack trace from previous location where exception was thrown ---
> 1>   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
> 1>   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
> 1>   at NSwag.Commands.CodeGeneration.SwaggerToCSharpClientCommand.<<RunAsync>b__86_0>d.MoveNext()
> 1>--- End of stack trace from previous location where exception was thrown ---
> 1>   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
> 1>   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
> 1>   at NSwag.Commands.CodeGeneration.SwaggerToCSharpClientCommand.<RunAsync>d__86.MoveNext()
> 1>--- End of stack trace from previous location where exception was thrown ---
> 1>   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
> 1>   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
> 1>   at NSwag.Commands.CodeGeneration.SwaggerToCSharpClientCommand.<RunAsync>d__85.MoveNext()
> 1>--- End of stack trace from previous location where exception was thrown ---
> 1>   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
> 1>   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
> 1>   at NConsole.CommandLineProcessor.<ProcessSingleAsync>d__12.MoveNext()
> 1>--- End of stack trace from previous location where exception was thrown ---
> 1>   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
> 1>   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
> 1>   at NConsole.CommandLineProcessor.<ProcessAsync>d__11.MoveNext()
> 1>--- End of stack trace from previous location where exception was thrown ---
> 1>   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
> 1>   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
> 1>   at NConsole.CommandLineProcessor.Process(String[] args, Object input)
> 1>   at NSwag.Commands.NSwagCommandProcessor.Process(String[] args)
> 1>C:\Users\GrahameHorner\.nuget\packages\nswag.msbuild.codegeneration\12.0.12\build\NSwag.MSBuild.CodeGeneration.targets(71,5): error MSB3073: The command ""C:\Users\GrahameHorner\.nuget\packages\nswag.msbuild\12.0.0\build\../tools/Win/NSwag.exe" swagger2csclient /className:WebClient /namespace:BigChange.JobWatch.WebGUI /input:C:\Users\GrahameHorner\source\repos\BigChange.JobWatch\src\BigChange.JobWatch.WebGUI\BigChange.JobWatch.WebAPI.v1.json /output:WebClient.cs " exited with code -1.
> 1>Done building project "BigChange.JobWatch.WebGUI.csproj" -- FAILED.
> ========== Build: 1 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
> 
RicoSuter commented 5 years ago

Can you provide the swagger.json? It seems that there is something wrong in it...

grahamehorner commented 5 years ago

@RSuter the aspnetcore server application works as expected; and the swagger document generated by the server application results in the correct document (see below)

image

the json file within the client application that is generated however is empty

grahamehorner commented 5 years ago

@RSuter this error aside, my reading and understanding of the generation of code would when a project has multiple controllers would result in invalid code that wouldn't complete the code would be a partial class in a single file with multiple duplicated attributes, private/public members and methods that conflict, IMHO each method should be in a seperate file {classname}.{method}.cs and the common elements should be within the {classname}.cs

RicoSuter commented 5 years ago

It should generate one cs file with multiple classes in it, but if swagger.json is empty then it cannot work...

Multiple file output: https://github.com/RSuter/NSwag/issues/1398

grahamehorner commented 5 years ago

@RSuter if the json file is invalid then the build output shouldn’t indicate this instead of throwing an exception, parsing of the json should narrow down and/or identify the error/area of issue.

RicoSuter commented 5 years ago

I agree - but keep in mind that this feature is in preview...