[英]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 的情况下做到这一点。
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。
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.