简体   繁体   中英

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. I want to upload the swagger.json file to power automate but there are two rules. Max 4 Mb and Max 256 functions per file. 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. But I don't know how to configure(Swashbuckle) or should I do it with documentFilters?

I already use ApiVersioning to decrease a littel bit of functions and size, but it is not enough. And I can´t chnage the complete url endpoint. 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.

  1. In each controller you can add set the Apiversion [ApiVersion("2.0")] and then set the controllername ie [ApiVersion("2.0.order")] . And there will be a version for each controller. This solution will change the urls and are not approachable for an existing 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

And a good example to check is https://github.com/cbruen1/SwaggerFilter . Hope this helps anyone.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM