chenxin's blog
[原创]-asp.net core swagger api post 请求参数实体移除实体导航属性,响应保留的解决方案
    /// <summary>
    /// 移除swagger api 请求参数中的导航属性。
    /// </summary>
    public class RemoveNavigatePropsOperationFilter : IOperationFilter
    {

        public void Apply(OpenApiOperation operation, OperationFilterContext context)
        {
            if (operation.RequestBody == null) return;

            foreach (var content in operation.RequestBody.Content.Values)
            {
                var schema = content.Schema;
                if (schema?.Reference == null) continue; // 非引用情况忽略

                var refId = schema.Reference.Id; // 比如 "UserDto"
                if (!context.SchemaRepository.Schemas.TryGetValue(refId, out var originalSchema))
                    continue;

                // 克隆 schema(深拷贝)
                var clonedSchema = CloneSchema(originalSchema);

                // 找到有 [Navigate] 的属性并移除
                Type[] parameterTypes = context.MethodInfo.GetParameters()
                    .Select(p => p.ParameterType).ToArray();
                var type = parameterTypes
                    .FirstOrDefault(t => t.FullName == refId); // 简单匹配,也可以用更复杂逻辑
                if (type == null) continue;

                var navigateProps = type.GetProperties()
                    .Where(p => p.GetCustomAttributes(typeof(SqlSugar.Navigate), true).Any())
                    .Select(p => JsonNamingPolicy.CamelCase.ConvertName(p.Name))
                    .ToList();

                foreach (var prop in navigateProps)
                {
                    if (clonedSchema.Properties.ContainsKey(prop))
                    {
                        clonedSchema.Properties.Remove(prop);
                    }
                }

                // 替换 requestBody 的 schema(不能直接动全局的)
                content.Schema = clonedSchema;
            }
        }

        private OpenApiSchema CloneSchema(OpenApiSchema original)
        {
            var clone = new OpenApiSchema
            {
                Type = original.Type,
                Format = original.Format,
                Properties = new Dictionary<string, OpenApiSchema>(original.Properties),
                Required = new SortedSet<string>(original.Required),
                AdditionalPropertiesAllowed = original.AdditionalPropertiesAllowed,
                AdditionalProperties = original.AdditionalProperties,
                Nullable = original.Nullable
            };

            return clone;
        }


    }
非特殊说明,本文版权归 陈新 所有,转载请注明出处.
本文标题:asp.net core swagger api post 请求参数实体移除实体导航属性,响应保留的解决方案
(0)
(0)
微信扫一扫
支付宝扫一扫
写作不易,如果本文对你所有帮助,扫码请我喝杯饮料可以吗?
评论列表(0条)
还没有任何评论,快来发表你的看法吧!
{{item.userInfo.nickName}}{{item.userInfo.isBlogger?"(博主)":""}}
{{getUserType(item.userInfo.userType)}} {{formatCommentTime(item.commentDate)}}
回复
{{replyItem.userInfo.nickName}}{{replyItem.userInfo.isBlogger?"(博主)":""}}
@{{replyItem.reply.userInfo.nickName+":"}}
{{getUserType(replyItem.userInfo.userType)}} {{formatCommentTime(replyItem.commentDate)}}
回复
正在加载评论列表... 已经到底啦~~~
文章归档 网站地图 闽ICP备2020021271号-1 百度统计