swagger swashbuckle 不支持嵌套类作为操作方法参数

问题描述 投票:0回答:2

我正在使用 asp.net 5

我有两个嵌套的模型类,两个内部类都被命名为

Command

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

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

在我的控制器类中有两个方法

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

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

由于 Put 和 Post 的查询我都使用嵌套类的名称

Command
,Swagger 将返回以下错误

执行请求时发生未处理的异常。 Swashbuckle.AspNetCore.SwaggerGen.SwaggerGeneratorException:操作的冲突方法/路径组合“PUT 测试” - TestSwagger.Controllers.TestController.PutData (TestSwagger),TestSwagger.Controllers.TestController.PostData (测试Swagger)。操作需要独特的方法/路径组合 Swagger/OpenAPI 3.0。使用 ConflictingActionsResolver 作为解决方法 在 Swashbuckle.AspNetCore.SwaggerGen.SwaggerGenerator.GenerateOperations(IEnumerable

1 apiDescriptions, SchemaRepository schemaRepository) at Swashbuckle.AspNetCore.SwaggerGen.SwaggerGenerator.GeneratePaths(IEnumerable
1 apiDescriptions、SchemaRepository schemaRepository) 在 Swashbuckle.AspNetCore.SwaggerGen.SwaggerGenerator.GetSwagger(String 文档名称、字符串主机、字符串基本路径) 在 Swashbuckle.AspNetCore.Swagger.SwaggerMiddleware.Invoke(HttpContext httpContext、ISwaggerProvider swaggerProvider) 在 Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddleware.Invoke(HttpContext 上下文)

如果我将命令模型名称之一更改为不同的名称,Swagger 将起作用。 然而,我相信这个嵌套类模型名称是合法的,并且也应该与 swagger 一起使用。如果有办法解决这个问题。谢谢

asp.net-web-api swagger swashbuckle
2个回答
10
投票

通过添加

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

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

解决了schemaId冲突。感谢这个问题


0
投票

@HExit 的回答使我的 swagger 页面失败并出现以下错误:

paths./api/v1/{sheetId}.get.requestBody.content.application/json.schema.$ref 处的解析器错误 无法解析引用:无法解析指针:/components/schemas/XL.API.Features.Sheets.GetSheet+Command 在文档中不存在

通过将其更改为

已修复
c.CustomSchemaIds(s => s.FullName.Replace("+", "."));
© www.soinside.com 2019 - 2024. All rights reserved.