6

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

HExit
  • 696
  • 7
  • 17
  • Yes that is strange, the type of the arguments should not mater, and the error is `Conflicting method/path combination` do you have custom paths defined? – Helder Sepulveda Dec 08 '20 at 17:00
  • @HelderSepulveda I do not have a custom path defined. So, both Put and Post have the same route. I just created a new project from Visual Studio and selected the Web Api .Net 5 Template. Then added the two model classes and add the Put and Post action in the controller. – HExit Dec 08 '20 at 22:51
  • I just created new project with the same models and actions with my home computer. something must be different I believe. swagger give me an different error message. System.InvalidOperationException: Can't use schemaId "$Command" for type "$TestSwagger.EditModel+Command". The same schemaId is already used for type "$TestSwagger.CreateModel+Command" And this error message make more sense. Yet, swagger should still allow nested parameter for two different actions with same inner class name. – HExit Dec 08 '20 at 23:10
  • There was an issue in 2015, but was just closed https://github.com/domaindrivendev/Swashbuckle.WebApi/pull/240 – HExit Dec 08 '20 at 23:18

1 Answers1

10

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

HExit
  • 696
  • 7
  • 17