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

Could not find the JSON path of a referenced schema #4869

Closed albert-tan closed 2 months ago

albert-tan commented 2 months ago

We recently upgrading our entire solution from .NET 6 to .NET 8. One of the asp.net core web project has NSwag post-build event configured: Command="$(NSwagExe_Net60) run nswag.json /variables:Configuration=$(Configuration)" That gets upgraded to use $(NSwagExe_Net80) along with the NSwag library upgrade from 13.20.0 to 14.0.7 and NJsonSchema from 10.9.0 to 11.0.0.

The issue here is after the upgrade, the tool $(NSwagExe_Net80) is throwing an error "Could not find the JSON path of a referenced schema".

1>Executing file 'nswag.json' with variables 'Configuration=Debug'...
1>Launcher directory: C:\Users\Albert Tan\.nuget\packages\nswag.msbuild\14.0.7\tools\Net80
1>System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation.
1> ---> System.InvalidOperationException: Could not find the JSON path of a referenced schema: Manually referenced schemas must be added to the 'Definitions' of a parent schema.
1>   at NJsonSchema.JsonPathUtilities.GetJsonPaths(Object rootObject, IEnumerable`1 searchedObjects, IContractResolver contractResolver)
1>   at NJsonSchema.JsonSchemaReferenceUtilities.UpdateSchemaReferencePaths(Object rootObject, Boolean removeExternalReferences, IContractResolver contractResolver)
1>   at NJsonSchema.Infrastructure.JsonSchemaSerialization.ToJson(Object obj, SchemaType schemaType, IContractResolver contractResolver, Formatting formatting)
1>   at NSwag.OpenApiDocument.ToJson(SchemaType schemaType, Formatting formatting)
1>   at NSwag.OpenApiDocument.ToJson(SchemaType schemaType)
1>   at NSwag.OpenApiDocument.ToJson()
1>   at NSwag.Commands.Generation.AspNetCore.AspNetCoreToOpenApiGeneratorCommandEntryPoint.Process(String commandContent, String outputFile, String applicationName) in /_/src/NSwag.Commands/Commands/Generation/AspNetCore/AspNetCoreToOpenApiGeneratorCommandEntryPoint.cs:line 30
1>   at System.RuntimeMethodHandle.InvokeMethod(Object target, Void** arguments, Signature sig, Boolean isConstructor)
1>   at System.Reflection.MethodBaseInvoker.InvokeDirectByRefWithFewArgs(Object obj, Span`1 copyOfArgs, BindingFlags invokeAttr)
1>   --- End of inner exception stack trace ---
1>   at System.Reflection.MethodBaseInvoker.InvokeDirectByRefWithFewArgs(Object obj, Span`1 copyOfArgs, BindingFlags invokeAttr)
1>   at System.Reflection.MethodBaseInvoker.InvokeWithFewArgs(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
1>   at System.Reflection.MethodBase.Invoke(Object obj, Object[] parameters)
1>   at NSwag.AspNetCore.Launcher.Program.Main(String[] args) in /_/src/NSwag.AspNetCore.Launcher/Program.cs:line 132
1>System.InvalidOperationException: Swagger generation failed with non-zero exit code '1'.
1>   at NSwag.Commands.Generation.AspNetCore.AspNetCoreToOpenApiCommand.RunAsync(CommandLineProcessor processor, IConsoleHost host) in /_/src/NSwag.Commands/Commands/Generation/AspNetCore/AspNetCoreToOpenApiCommand.cs:line 195
1>   at NSwag.Commands.NSwagDocumentBase.GenerateSwaggerDocumentAsync() in /_/src/NSwag.Commands/NSwagDocumentBase.cs:line 270
1>   at NSwag.Commands.NSwagDocument.ExecuteAsync() in /_/src/NSwag.Commands/NSwagDocument.cs:line 67
1>   at NSwag.Commands.Document.ExecuteDocumentCommand.ExecuteDocumentAsync(IConsoleHost host, String filePath) in /_/src/NSwag.Commands/Commands/Document/ExecuteDocumentCommand.cs:line 76
1>   at NSwag.Commands.Document.ExecuteDocumentCommand.RunAsync(CommandLineProcessor processor, IConsoleHost host) in /_/src/NSwag.Commands/Commands/Document/ExecuteDocumentCommand.cs:line 33
1>   at NConsole.CommandLineProcessor.ProcessSingleAsync(String[] args, Object input)
1>   at NConsole.CommandLineProcessor.ProcessAsync(String[] args, Object input)
1>   at NSwag.Commands.NSwagCommandProcessor.ProcessAsync(String[] args) in /_/src/NSwag.Commands/NSwagCommandProcessor.cs:line 65
1>C:\Repos\definitiv-net8\Definitiv.Web\Definitiv.Web.csproj(59,5): error MSB3073: The command "dotnet "C:\Users\Albert Tan\.nuget\packages\nswag.msbuild\14.0.7\buildTransitive\../tools/Net80/dotnet-nswag.dll" run nswag.json /variables:Configuration=Debug" exited with code -1.

I have been tracing the source code for NSwag and NJssonSchema to know within the NJsonSchema.JsonPathUtilities.GetJsonPaths(...) method, the mappings dictionary contains 3 items with null value which is causing the error. image

Inspecting the JsonSchema dictionary key object for those 3 items only reveals they are empty schema without any clue for me to trace back to the source.

It was working fine for the .NET 6 project with $(NSwagExe_Net60) tool. So just wondering anyone knows any possible changes in between NSwag version 13.20.0 and 14.0.7 and/or NJsonSchema 10.9.0 and 11.00 that could potentially cause this behavior? Or any additional settings or configuration needed for the updated library to run correctly?

albert-tan commented 2 months ago

Found out that it is due to a few of our API endpoints are returning response of type JContainer that causes the error. After changing that to JObject then the error is fixed.