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

jexiocij  于 12个月前  发布在  其他
关注(0)|答案(2)|浏览(157)

我正在使用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; }
    }
}

在我的Controller类中有两个方法

[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 Test”- TestSwagger.Controllers.TestController.PutData(TestSwagger),TestSwagger.Controllers.TestController.PostData(TestSwagger)。操作需要Swagger/OpenAPI 3.0的唯一方法/路径组合。在Swashbuckle.AspNetCore.SwaggerGen.SwaggerGenerator.GenerateOperations(IEEEx 1 m2n1x 1 apiDescriptions,SchemaRepository schemaRepository)中使用Swashbuckle.AspNetCore.SwaggerGen.SwaggerGenerator.GetSwagger(String documentName,String host,String basePath)在Swashbuckle.AspNetCore.Swagger.SwaggerMiddleware. Swacke(HttpContext httpContext,ISwaggerProvider swaggerProvider)在Microsoft.AspNetCore.Diagnostics. SwackerPageExceptionMiddleware. Swacke(HttpContext context)中使用SwaggerResolver作为变通方法
Swagger将工作,如果我改变了命令模型的名称之一,以不同的东西。然而,我相信这个嵌套类模型名称是合法的,应该也可以与swagger一起使用。如果有办法解决的话。谢谢

cig3rfwq

cig3rfwq1#

通过添加c.CustomSchemaIds(x => x.FullName);

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

解决了schemaId冲突。感谢this question

9rnv2umw

9rnv2umw2#

来自@HExit的回答导致我的swagger页面失败,并出现以下错误:
路径./API/v1/{sheetId}.get.requestBody.content.application/json.schema.$ref处的解析程序错误无法解析引用:无法解析指针:文档中不存在/components/schemas/XL.API.Features.Sheets.GetSheet+Command
通过将其更改为

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

相关问题