简体   繁体   English

使用 Swashbuckle 对 Swagger 页面的架构部分进行排序

[英]Sorting the Schemas portion of a Swagger page using Swashbuckle

On my Swagger page, I am (mostly) able to order the operations as described on the Swashbuckle page.在我的 Swagger 页面上,我(大部分)能够按照 Swashbuckle 页面上的说明进行操作。

Below the operations is a "Schemas" section showing the data structures used by the actions.操作下方是“模式”部分,显示操作使用的数据结构。 These data structures appear in a arbitrary order.这些数据结构以任意顺序出现。 I would like to sort them.我想对它们进行排序。

The question Swagger sort Schema Defintions superficially looks like the same question, but in that question "sort" is used in the sense of "sorting the items into different bins", not "ordering a list" which is what I want.问题Swagger sort Schema Defintions表面上看起来像同一个问题,但在那个问题中,“排序”的含义是“将项目分类到不同的箱子”,而不是我想要的“排序列表”。

I have made a document filter that "works", but when I look at the code I wrote, I die a little inside.我已经制作了一个“有效”的文档过滤器,但是当我查看我编写的代码时,我有点死心了。

Is there a more correct way to do this?有没有更正确的方法来做到这一点?

Edit: To be specific, what I object to about this code is that it is "working" by sorting the entries in a Dictionary, which is just bad ( see this question ).编辑:具体来说,我 object 对这段代码的看法是,它通过对字典中的条目进行排序来“工作”,这很糟糕(见这个问题)。

Turns out the answer was simply to use a SortedDictionary:原来答案只是使用 SortedDictionary:

        openApiDoc.Components.Schemas = new System.Collections.Generic.SortedDictionary<string, OpenApiSchema>(openApiDoc.Components.Schemas);

Actually, you were on the right track with the document filter!实际上,您在使用文档过滤器时走在了正确的轨道上!

In Startup.cs, ConfigureServices method:-在 Startup.cs 中, ConfigureServices方法:-

services.AddSwaggerGen(c =>
{
    // ...

    // For our document filtering needs.
    c.DocumentFilter<DocumentFilter>();
});

And here is the document filter implementation:-这是文档过滤器的实现:-

using System.Linq;

public class DocumentFilter : IDocumentFilter
{
    public DocumentFilter()
    {
    }

    // Implements IDocumentFilter.Apply().
    public void Apply(OpenApiDocument swaggerDoc, DocumentFilterContext context)
    {
        if (swaggerDoc == null)
            return;

        // Re-order the schemas alphabetically.
        swaggerDoc.Components.Schemas = swaggerDoc.Components.Schemas.OrderBy(kvp => kvp.Key, StringComparer.InvariantCulture)
            .ToDictionary(kvp => kvp.Key, kvp => kvp.Value);
    }
}

when I look at the code I wrote, I die a little inside - embrace the glory of LINQ, and you will be proud of your code! when I look at the code I wrote, I die a little inside死心了——拥抱 LINQ 的荣耀,你会为你的代码感到骄傲!

This wouldn't change the final display order for me, but MikeBeaton's suggestion whilst reporting a Sort Schema Issue on GitHub worked like a charm..这不会改变我的最终显示顺序,但MikeBeaton 在报告 GitHub 上的排序模式问题时的建议就像一个魅力..

app.UseSwagger(c =>
{
    c.PreSerializeFilters.Add((swagger, _) =>
    {
        if (swagger.Components != null && swagger.Components.Schemas != null)
        {
            var replacement = new Dictionary<string, OpenApiSchema>();
            foreach (var kv in swagger.Components.Schemas.OrderBy(p => p.Key))
            {
                replacement.Add(kv.Key, kv.Value);
            }
            swagger.Components.Schemas = replacement;
        }
    });
})

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

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