简体   繁体   English

Swashbuckle swagger.json 大于 4 mb 净核心

[英]Swashbuckle swagger.json larger than 4 mb net core

My api endpoint have grown too large and I need to minimize it or divide it to multiple swagger.json files.我的 api 端点变得太大,我需要将其最小化或将其分成多个 swagger.json 文件。 I want to upload the swagger.json file to power automate but there are two rules.我想上传 swagger.json 文件以实现自动化,但有两条规则。 Max 4 Mb and Max 256 functions per file.每个文件最多 4 Mb 和最多 256 个函数。 I don´t meet these requirement.我不符合这些要求。

I want to have a swagger file per controller/group this will minimize number of functions and decrease the size of file.我希望每个控制器/组有一个 swagger 文件,这将最大限度地减少功能数量并减小文件大小。 But I don't know how to configure(Swashbuckle) or should I do it with documentFilters?但我不知道如何配置(Swashbuckle)还是应该使用 documentFilters 来配置?

I already use ApiVersioning to decrease a littel bit of functions and size, but it is not enough.我已经使用 ApiVersioning 来减少一些功能和大小,但这还不够。 And I can´t chnage the complete url endpoint.而且我无法更改完整的 url 端点。 I just want multiple files more than just versions.我只想要多个文件而不仅仅是版本。

There are two options to make this possible.有两种选择可以使这成为可能。 But I want to make it without changing any urls to the existing api.但我想在不更改现有 api 的任何 url 的情况下做到这一点。

  1. In each controller you can add set the Apiversion [ApiVersion("2.0")] and then set the controllername ie [ApiVersion("2.0.order")] .在每个 controller 中,您可以添加设置 Apiversion [ApiVersion("2.0")]然后设置控制器名称,即[ApiVersion("2.0.order")] And there will be a version for each controller.每个 controller 都会有一个版本。 This solution will change the urls and are not approachable for an existing api.此解决方案将更改 url,并且不适用于现有的 api。

  2. Another solution is to create tags for each operation with a filter and now we can create a filter for each endpoint另一种解决方案是使用过滤器为每个操作创建标签,现在我们可以为每个端点创建一个过滤器

    public class ApplySwaggerOperationTags : IOperationFilter
        {
            public void Apply(OpenApiOperation operation, OperationFilterContext context)
            {
                var tag = new OpenApiTag();
                context.ApiDescription.ActionDescriptor.RouteValues.TryGetValue("controller",out string tagname);
                tag.Name = tagname;
                operation.Tags.Add(tag);
    
                var tagGroupName = new OpenApiTag();
                tagGroupName.Name = context.ApiDescription.GroupName;
                operation.Tags.Add(tagGroupName);
            }
        }

And then apply a document filter然后应用文档过滤器

 public class SwaggerDocumentFilter : IDocumentFilter
    {
        public void Apply(OpenApiDocument swaggerDoc, DocumentFilterContext context)
        {
            // Key is read-only so make a copy of the Paths property
            var pathsFiltered = new OpenApiPaths();
            var array = context.DocumentName.Split("-");
            string version = array[0];
            string tag = string.Empty;
            if (array.Count() > 1)
            {
                tag = array[1];
            }

            foreach (var path in swaggerDoc.Paths)
            {
                if (path.Value.Operations.Values.First().Tags.FirstOrDefault(c => c.Name == version) != null)
                {
                    if (path.Value.Operations.Values.First().Tags.FirstOrDefault(c => c.Name.ToLower() == tag.ToLower()) != null ||
                        tag == string.Empty)
                    {
                        // Add the path to the filtered collection
                        pathsFiltered.Add(path.Key, path.Value);
                    }
                }
            }
            swaggerDoc.Paths = pathsFiltered;
        }
    }

The key is to have c.SwaggerEndpoint(in UseSwaggerUI) and options.SwaggerDoc(in SwaggerGenOptions) that match关键是要让 c.SwaggerEndpoint(in UseSwaggerUI) 和 options.SwaggerDoc(in SwaggerGenOptions) 匹配

And a good example to check is https://github.com/cbruen1/SwaggerFilter .一个很好的检查例子是https://github.com/cbruen1/SwaggerFilter Hope this helps anyone.希望这对任何人都有帮助。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 ASP.NET Core - Swashbuckle 未创建 swagger.json 文件 - ASP.NET Core - Swashbuckle not creating swagger.json file 如何修复 .net core 2.2 应用程序中未找到的 swagger.json - How to fix swagger.json not found in .net core 2.2 app 使用 Swashbuckle Aspnetcore 将 `host`、`basePath` 和 `schemes` 添加到 swagger.json - Add `host`, `basePath` and `schemes` to swagger.json using Swashbuckle Aspnetcore .net core/swagger - 生成 swagger.json 文件时如何绕过控制器文件? - .net core/swagger - How can I around a controller file when generating the swagger.json file? SWAGGER UI 不显示 - Swagger.json 在 .NET Core 上显示 - SWAGGER UI does not show up - Swagger.json does on .NET Core Swashbuckle/Swagger .NET Core 字典<int, MyModel> - Swashbuckle/Swagger .NET Core Dictionary<int, MyModel> ASP.NET Core Web API - Fetch error undefined /swagger/MyApp API v1/swagger.json - ASP.NET Core Web API - Fetch error undefined /swagger/MyApp API v1/swagger.json 使用在 Swashbuckle 的后期构建操作中生成的 swagger.json 文件,而不是在运行时生成的文件 - Use swagger.json file generated in post build actions in Swashbuckle instead of the file generated at runtime Swagger 不生成 swagger.json - Swagger is not generating swagger.json Swashbuckle C#swagger插件默认情况下如何使用我自己的个人swagger.json而不是依靠它生成的一个? - Swashbuckle C# swagger plugin how to use my own personal swagger.json by default instead of relying on one it generates?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM