【Bug】Swagger:Failed to generate Operation for action之Failed to generate Operation for action

文章目录

    • 问题
    • 原因
    • 解决
    • 另一原因

问题

.NET 6 action接收参数特性使用 FromBody swagger报错不知道为啥(swagger报错被后来中间件处理了并没有提示出来,一开始没想到,疯狂猜测原因)

//报错
 [HttpPost, Route("CapacityComplianceRate")] public async Task<IActionResult> CapacityComplianceRate([FromBody] ApiCommonInput input)//改成 FromBody swagger报错不知道为啥 {}
//没报错
 [HttpPost, Route("CapacityComplianceRate")]
 public async Task<IActionResult> CapacityComplianceRate([FromQuery] ApiCommonInput input)//改成  FromBody swagger报错不知道为啥
 {}

除了FromBody 还有FromHerder会其余的都不会 不写特性也不会
【Bug】Swagger:Failed to generate Operation for action之Failed to generate Operation for action_第1张图片
理论上只是接口能用,只是Swagger UI生成不了

原因

存在swagger处理不了的情况

本文的问题具体来说是接口参数的model在不同命名空间(引用)中存在同名,.Net可通过命名空间区别但是swagger区别不了

报错日志节选:

2023-03-28 17:23:43:Failed to generate Operation for action - QL.Board.Controllers.ProductionController.CapacityComplianceRate (QL.Board.WebApi). See inner exceptionCan’t use schemaId “ A p i C o m m o n I n p u t " f o r t y p e " ApiCommonInput" for type " ApiCommonInput"fortype"QL.Platform.Utility.Entity.ApiModels.Input.ApiCommonInput”. The same schemaId is already used for type “ Q L . B o a r d . C o n t r o l l e r s . B i z B o a r d L i s t C o n t r o l l e r + A p i C o m m o n I n p u t " S w a s h b u c k l e . A s p N e t C o r e . S w a g g e r G e n . S w a g g e r G e n e r a t o r E x c e p t i o n : F a i l e d t o g e n e r a t e s c h e m a f o r t y p e − Q L . P l a t f o r m . U t i l i t y . E n t i t y . A p i M o d e l s . I n p u t . A p i C o m m o n I n p u t . S e e i n n e r e x c e p t i o n − − − > S y s t e m . I n v a l i d O p e r a t i o n E x c e p t i o n : C a n ′ t u s e s c h e m a I d " QL.Board.Controllers.BizBoardListController+ApiCommonInput"Swashbuckle.AspNetCore.SwaggerGen.SwaggerGeneratorException: Failed to generate schema for type - QL.Platform.Utility.Entity.ApiModels.Input.ApiCommonInput. See inner exception ---> System.InvalidOperationException: Can't use schemaId " QL.Board.Controllers.BizBoardListController+ApiCommonInput"Swashbuckle.AspNetCore.SwaggerGen.SwaggerGeneratorException:FailedtogenerateschemafortypeQL.Platform.Utility.Entity.ApiModels.Input.ApiCommonInput.Seeinnerexception>System.InvalidOperationException:CantuseschemaId"ApiCommonInput” for type “ Q L . P l a t f o r m . U t i l i t y . E n t i t y . A p i M o d e l s . I n p u t . A p i C o m m o n I n p u t " . T h e s a m e s c h e m a I d i s a l r e a d y u s e d f o r t y p e " QL.Platform.Utility.Entity.ApiModels.Input.ApiCommonInput". The same schemaId is already used for type " QL.Platform.Utility.Entity.ApiModels.Input.ApiCommonInput".ThesameschemaIdisalreadyusedfortype"QL.Board.Controllers.BizBoardListController+ApiCommonInput”
at Swashbuckle.AspNetCore.SwaggerGen.SchemaRepository.RegisterType(Type type, String schemaId)
at Swashbuckle.AspNetCore.SwaggerGen.SchemaGenerator.GenerateReferencedSchema(DataContract dataContract, SchemaRepository schemaRepository, Func`1 definitionFactory)
at Swashbuckle.AspNetCore.SwaggerGen.SchemaGenerator.GenerateConcreteSchema(DataContract dataContract, SchemaRepository schemaRepository)

解决

更改其中一个同名model的命名,并处理对应的引用(挑引用少的改)

	//类1
    public class ApiUtilityCommonInput
    {
        /// 
        /// 类型
        /// 
        public int type { get; set; }
    }
    //类2
    public class ApiCommonInput
    {
        /// 
        /// 类型
        /// 
        public int type { get; set; }
        /// 
        /// Id
        /// 
        public Guid Id{ get; set; }
    }

   [HttpPost, Route("CapacityComplianceRate")]
    public async Task<IActionResult> CapacityComplianceRate([FromBody] ApiUtilityCommonInput input)
    {
    }

另一原因

存在同名路由

参考本人另一博文

你可能感兴趣的:(#,WEB_C#,bug,swagger)