RicoSuter/NSwag

Unhandled exception generating code with openapi2csclient for PagerDuty OpenAPI v3

LazaroOnline opened this issue · 2 comments

Running the command to generate the C# client code from the open-API v3 json file from the official PagerDuty url results in an unhandled exception by the nswag command and thus doesn't get to generate any code.

nswag openapi2csclient  /output:PagerDutyClient.cs  /namespace:MyApp /input:https://raw.githubusercontent.com/PagerDuty/api-schema/main/reference/REST/openapiv3.json

Error:
System.InvalidOperationException: Could not resolve the path '#/components/responses/OrchestrationPathServiceActiveResponse/content/application~1json/schema'.
at NJsonSchema.JsonReferenceResolver.ResolveDocumentReference(Object rootObject, String jsonPath, Type targetType, IContractResolver contractResolver)
at NJsonSchema.JsonReferenceResolver.ResolveReferenceAsync(Object rootObject, String jsonPath, Type targetType, IContractResolver contractResolver, Boolean append, CancellationToken cancellationToken)
at NJsonSchema.JsonReferenceResolver.ResolveReferenceAsync(Object rootObject, String jsonPath, Type targetType, IContractResolver contractResolver, CancellationToken cancellationToken)
at NJsonSchema.JsonSchemaReferenceUtilities.JsonReferenceUpdater.VisitJsonReferenceAsync(IJsonReference reference, String path, String typeNameHint, CancellationToken cancellationToken)
at NJsonSchema.Visitors.AsyncJsonReferenceVisitorBase.VisitAsync(Object obj, String path, String typeNameHint, ISet1 checkedObjects, Action1 replacer, CancellationToken cancellationToken)
at NJsonSchema.Visitors.AsyncJsonReferenceVisitorBase.VisitAsync(Object obj, String path, String typeNameHint, ISet1 checkedObjects, Action1 replacer, CancellationToken cancellationToken)
at NJsonSchema.Visitors.AsyncJsonReferenceVisitorBase.VisitAsync(Object obj, String path, String typeNameHint, ISet1 checkedObjects, Action1 replacer, CancellationToken cancellationToken)
at NJsonSchema.Visitors.AsyncJsonReferenceVisitorBase.VisitAsync(Object obj, String path, String typeNameHint, ISet1 checkedObjects, Action1 replacer, CancellationToken cancellationToken)
at NJsonSchema.Visitors.AsyncJsonReferenceVisitorBase.VisitAsync(Object obj, String path, String typeNameHint, ISet1 checkedObjects, Action1 replacer, CancellationToken cancellationToken)
at NJsonSchema.Visitors.AsyncJsonReferenceVisitorBase.VisitAsync(Object obj, String path, String typeNameHint, ISet1 checkedObjects, Action1 replacer, CancellationToken cancellationToken)
at NJsonSchema.Visitors.AsyncJsonReferenceVisitorBase.VisitAsync(Object obj, String path, String typeNameHint, ISet1 checkedObjects, Action1 replacer, CancellationToken cancellationToken)
at NJsonSchema.Visitors.AsyncJsonReferenceVisitorBase.VisitAsync(Object obj, CancellationToken cancellationToken)
at NJsonSchema.JsonSchemaReferenceUtilities.JsonReferenceUpdater.VisitAsync(Object obj, CancellationToken cancellationToken)
at NJsonSchema.JsonSchemaReferenceUtilities.UpdateSchemaReferencesAsync(Object rootObject, JsonReferenceResolver referenceResolver, IContractResolver contractResolver, CancellationToken cancellationToken)
at NJsonSchema.Infrastructure.JsonSchemaSerialization.FromJsonWithLoaderAsync[T](Func1 loader, SchemaType schemaType, String documentPath, Func2 referenceResolverFactory, IContractResolver contractResolver, CancellationToken cancellationToken)
at NSwag.OpenApiDocument.FromJsonAsync(String data, String documentPath, SchemaType expectedSchemaType, Func`2 referenceResolverFactory, CancellationToken cancellationToken) in //src/NSwag.Core/OpenApiDocument.cs:line 204
at NSwag.OpenApiDocument.FromUrlAsync(String url, CancellationToken cancellationToken) in /
/src/NSwag.Core/OpenApiDocument.cs:line 236
at NSwag.Commands.InputOutputCommandBase.GetInputSwaggerDocument() in //src/NSwag.Commands/Commands/InputOutputCommandBase.cs:line 47
at NSwag.Commands.CodeGeneration.OpenApiToCSharpClientCommand.RunAsync() in /
/src/NSwag.Commands/Commands/CodeGeneration/OpenApiToCSharpClientCommand.cs:line 281
at NSwag.Commands.CodeGeneration.OpenApiToCSharpClientCommand.RunAsync(CommandLineProcessor processor, IConsoleHost host) in //src/NSwag.Commands/Commands/CodeGeneration/OpenApiToCSharpClientCommand.cs:line 270
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 65

Image screenshot:
image

Running windows 11, PowerShell 7.4.4,
NSwag version: 14.1.0.0
NJsonSchema version: 11.0.2.0 (Newtonsoft.Json v13.0.0.0)

It seems Nswag is not happy with the "$ref" found in the json:

image

that is referencing this other json property:

image

Maybe there is some part of the OpenAPI v3 standar that Nswag is not yet supporting?

NSwag should be able to generate most of the code and skip through the schema validation errors whenever possible, but at the moment the error results in no code at all in the output.
For example, the openapi-generator tool is able to generate the code for this case:

npm install -g @openapitools/openapi-generator-cli
openapi-generator-cli generate -g csharp -o "./PagerDutyClient" --skip-validate-spec -i https://raw.githubusercontent.com/PagerDuty/api-schema/main/reference/REST/openapiv3.json

Even if the generated code has some errors, we can manually fix those latter after the tool generates the base for it, the tool should be able to generate 99% of the code.