HExit
HExit

Reputation: 696

swagger swashbuckle does not support nested class as action method parameter

I am using asp.net 5

I have two model class, which are nested, both of the inner class are named Command

public class EditModel
{
   public class Command
   {
      public int Id { get; set; }
      public string Info { get; set; }
   }
}

and

public class CreateModel
{
    public class Command
    {
        public int Id { get; set; }
        public string Info { get; set; }
    }
}

In my Controller class has two methods

    [HttpPost]
    public IActionResult PutData(CreateModel.Command model)
    {
        return Ok();
    }

    [HttpPut]
    public IActionResult PostData(EditModel.Command model)
    {
        return Ok();
    }

Since for both Put and Post's query I am using nested class both name Command, Swagger will return the following error

An unhandled exception has occurred while executing the request. Swashbuckle.AspNetCore.SwaggerGen.SwaggerGeneratorException: Conflicting method/path combination "PUT Test" for actions - TestSwagger.Controllers.TestController.PutData (TestSwagger),TestSwagger.Controllers.TestController.PostData (TestSwagger). Actions require a unique method/path combination for Swagger/OpenAPI 3.0. Use ConflictingActionsResolver as a workaround at Swashbuckle.AspNetCore.SwaggerGen.SwaggerGenerator.GenerateOperations(IEnumerable1 apiDescriptions, SchemaRepository schemaRepository) at Swashbuckle.AspNetCore.SwaggerGen.SwaggerGenerator.GeneratePaths(IEnumerable1 apiDescriptions, SchemaRepository schemaRepository) at Swashbuckle.AspNetCore.SwaggerGen.SwaggerGenerator.GetSwagger(String documentName, String host, String basePath) at Swashbuckle.AspNetCore.Swagger.SwaggerMiddleware.Invoke(HttpContext httpContext, ISwaggerProvider swaggerProvider) at Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddleware.Invoke(HttpContext context)

Swagger will work, if I change one of the Command model name to something different. Yet, I believe this nested class model name is legit and should work with swagger also. If there a way to work around this. Thanks

Upvotes: 6

Views: 4235

Answers (2)

Yehor Androsov
Yehor Androsov

Reputation: 6162

Answer from @HExit made my swagger page fail with following error:

Resolver error at paths./api/v1/{sheetId}.get.requestBody.content.application/json.schema.$ref Could not resolve reference: Could not resolve pointer: /components/schemas/XL.API.Features.Sheets.GetSheet+Command does not exist in document

It was fixed by changing it to

c.CustomSchemaIds(s => s.FullName.Replace("+", "."));

Upvotes: 1

HExit
HExit

Reputation: 696

By adding c.CustomSchemaIds(x => x.FullName);

services.AddSwaggerGen(c =>
            {
                c.SwaggerDoc("v1", new OpenApiInfo { Title = "TestSwagger", Version = "v1" });
                c.CustomSchemaIds(x => x.FullName);
            });

solved the schemaId conflict. Thanks to this question

Upvotes: 10

Related Questions