RicoSuter / NSwag

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

Failed to generate openapi since 14.0.4 #4832

Closed cmenzi closed 7 months ago

cmenzi commented 7 months ago

Hi Rico

Since version 14.0.4 I'm not able to generate my openapi.json anymore with the following exception.

Everything worked in 14.0.3.

It somehow detects multiple body parameters in all my POST and PUT operations, like The operation 'CreateUser' has more than one body parameter.

This is how my MapPost looks like:

As you can see, there is only one DTO CreateUserCommand in the delegate.

Any ideas?

//...

groupV1.MapPost(
    CreateUserV1
)
.Produces<CreateUserResponse>(statusCode: StatusCodes.Status201Created)

//...

public Task<IResult> CreateUserV1(CreateUserCommand command, ISender sender, CancellationToken cancellationToken) =>
    Result
        .Create(command, UnprocessableRequest)
        .Bind(command => sender.Send(command, cancellationToken))
        .Created(x => $"/users/{x.Id}");
Launcher directory: C:\Program Files (x86)\Rico Suter\NSwagStudio\Net80
System.InvalidOperationException: Swagger generation failed with non-zero exit code '1'.

Runtime: Net80
   at NSwag.Commands.Generation.AspNetCore.AspNetCoreToOpenApiCommand.RunAsync(CommandLineProcessor processor, IConsoleHost host) in /_/src/NSwag.Commands/Commands/Generation/AspNetCore/AspNetCoreToOpenApiCommand.cs:line 195
   at NSwag.Commands.NSwagDocumentBase.GenerateSwaggerDocumentAsync() in /_/src/NSwag.Commands/NSwagDocumentBase.cs:line 270
   at NSwag.Commands.NSwagDocument.ExecuteAsync() in /_/src/NSwag.Commands/NSwagDocument.cs:line 67
   at NSwag.Commands.Document.ExecuteDocumentCommand.ExecuteDocumentAsync(IConsoleHost host, String filePath) in /_/src/NSwag.Commands/Commands/Document/ExecuteDocumentCommand.cs:line 76
   at NSwag.Commands.Document.ExecuteDocumentCommand.RunAsync(CommandLineProcessor processor, IConsoleHost host) in /_/src/NSwag.Commands/Commands/Document/ExecuteDocumentCommand.cs:line 33
   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

System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation.
 ---> System.InvalidOperationException: The operation 'CreateUser' has more than one body parameter.
   at NSwag.Generation.AspNetCore.Processors.OperationParameterProcessor.EnsureSingleBodyParameter(OpenApiOperationDescription operationDescription)
   at NSwag.Generation.AspNetCore.Processors.OperationParameterProcessor.Process(OperationProcessorContext operationProcessorContext)
   at NSwag.Generation.AspNetCore.AspNetCoreOpenApiDocumentGenerator.RunOperationProcessors(OpenApiDocument document, ApiDescription apiDescription, Type controllerType, MethodInfo methodInfo, OpenApiOperationDescription operationDescription, List`1 allOperations, OpenApiDocumentGenerator generator, OpenApiSchemaResolver schemaResolver)
   at NSwag.Generation.AspNetCore.AspNetCoreOpenApiDocumentGenerator.AddOperationDescriptionsToDocument(OpenApiDocument document, Type controllerType, List`1 operations, OpenApiDocumentGenerator swaggerGenerator, OpenApiSchemaResolver schemaResolver)
   at NSwag.Generation.AspNetCore.AspNetCoreOpenApiDocumentGenerator.GenerateApiGroups(OpenApiDocumentGenerator generator, OpenApiDocument document, IGrouping`2[] apiGroups, OpenApiSchemaResolver schemaResolver)
   at NSwag.Generation.AspNetCore.AspNetCoreOpenApiDocumentGenerator.GenerateAsync(ApiDescriptionGroupCollection apiDescriptionGroups)
   at NSwag.Commands.Generation.AspNetCore.AspNetCoreToOpenApiCommand.GenerateDocumentWithDocumentProviderAsync(IServiceProvider serviceProvider) in /_/src/NSwag.Commands/Commands/Generation/AspNetCore/AspNetCoreToOpenApiCommand.cs:line 245
   at NSwag.Commands.Generation.AspNetCore.AspNetCoreToOpenApiCommand.GenerateDocumentAsync(IServiceProvider serviceProvider, String currentWorkingDirectory) in /_/src/NSwag.Commands/Commands/Generation/AspNetCore/AspNetCoreToOpenApiCommand.cs:line 239
   at NSwag.Commands.Generation.AspNetCore.AspNetCoreToOpenApiGeneratorCommandEntryPoint.Process(String commandContent, String outputFile, String applicationName) in /_/src/NSwag.Commands/Commands/Generation/AspNetCore/AspNetCoreToOpenApiGeneratorCommandEntryPoint.cs:line 29
   at System.RuntimeMethodHandle.InvokeMethod(Object target, Void** arguments, Signature sig, Boolean isConstructor)
   at System.Reflection.MethodBaseInvoker.InvokeDirectByRefWithFewArgs(Object obj, Span`1 copyOfArgs, BindingFlags invokeAttr)
   --- End of inner exception stack trace ---
   at System.Reflection.MethodBaseInvoker.InvokeDirectByRefWithFewArgs(Object obj, Span`1 copyOfArgs, BindingFlags invokeAttr)
   at System.Reflection.MethodBaseInvoker.InvokeWithFewArgs(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
   at System.Reflection.MethodBase.Invoke(Object obj, Object[] parameters)
   at NSwag.AspNetCore.Launcher.Program.Main(String[] args) in /_/src/NSwag.AspNetCore.Launcher/Program.cs:line 132
roddone commented 7 months ago

Same error with a simple Post Route :

var api = app.MapGroup("api");
api.MapPost("/{user}/{entityName}", ( string user, string entityName, Entity entity) => { /*my code*/ })
   .WithOpenApi();
An unhandled exception has occurred while executing the request.
      System.InvalidOperationException: The operation 'PostApi' has more than one body parameter.
         at NSwag.Generation.AspNetCore.Processors.OperationParameterProcessor.EnsureSingleBodyParameter(OpenApiOperationDescription operationDescription)
         at NSwag.Generation.AspNetCore.Processors.OperationParameterProcessor.Process(OperationProcessorContext operationProcessorContext)
         at NSwag.Generation.AspNetCore.AspNetCoreOpenApiDocumentGenerator.RunOperationProcessors(OpenApiDocument document, ApiDescription apiDescription, Type controllerType, MethodInfo methodInfo, OpenApiOperationDescription operationDescription, List`1 allOperations, OpenApiDocumentGenerator generator, OpenApiSchemaResolver schemaResolver)
         at NSwag.Generation.AspNetCore.AspNetCoreOpenApiDocumentGenerator.AddOperationDescriptionsToDocument(OpenApiDocument document, Type controllerType, List`1 operations, OpenApiDocumentGenerator swaggerGenerator, OpenApiSchemaResolver schemaResolver)
         at NSwag.Generation.AspNetCore.AspNetCoreOpenApiDocumentGenerator.GenerateApiGroups(OpenApiDocumentGenerator generator, OpenApiDocument document, IGrouping`2[] apiGroups, OpenApiSchemaResolver schemaResolver)
         at NSwag.Generation.AspNetCore.AspNetCoreOpenApiDocumentGenerator.GenerateAsync(ApiDescriptionGroupCollection apiDescriptionGroups)
         at NSwag.AspNetCore.Middlewares.OpenApiDocumentMiddleware.GenerateDocumentAsync(HttpContext context)
         at NSwag.AspNetCore.Middlewares.OpenApiDocumentMiddleware.GetDocumentAsync(HttpContext context)
         at NSwag.AspNetCore.Middlewares.OpenApiDocumentMiddleware.Invoke(HttpContext context)
         at Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddlewareImpl.Invoke(HttpContext context)

I tried to add FromRoute / FromBody decorators on parameters, but error still occurs

smcgettrick commented 7 months ago

Same issue when trying to migrate from Swashbuckle to NSwag with the below code:

app.MapPost("/test", async (CreateTestRequest request, [FromServices] ISender sender) =>
{
  // Code Here
}
.WithName("CreateTest")
.Produces<CreateTestResponse>(StatusCodes.Status201Created)
.ProducesProblem(StatusCodes.Status201Created)
.WithOpenApi();
jamesburton commented 7 months ago

I hit the same problem across multiple methods ... appears fundamentally broken and needs to be pulled!

RicoSuter commented 7 months ago

Should be fixed in v14.0.5, please open issue if problem persists.

jamesburton commented 7 months ago

Just pulled 14.0.5 and it is resolved for me, thanks.

smcgettrick commented 7 months ago

That fixed it, thank you.

roddone commented 7 months ago

That fixed it for me too, thanks ;)

cmenzi commented 7 months ago

Fixed for me as well. @RicoSuter Thx