swagger swashbuckle does not support nested class as action method parameter

Viewed 1407

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

1 Answers

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

Related