【问题标题】:C# - Ordering required params from abstract class in Swagger API documentationC# - 从 Swagger API 文档中的抽象类中订购所需的参数
【发布时间】:2021-01-30 19:35:37
【问题描述】:

在问这个问题之前我已经搜索了很多,但我发现的都是与其他主题相关的,比如在 swagger doc 中排序路径。

我可以通过这个简单的例子来说明我的问题:

abstract class Animal
{
   [Required]
   public string Name { get; set; }
}

class Dog : Animal 
{
   [Required]
   public bool CanBark { get; set; }
   
   public Color DogColor { get; set; }
}

但在我生成的 Swagger API 文档中,参数按以下顺序出现:

  1. CanBark(必填)
  2. 狗颜色
  3. 姓名(必填)

基本上,我想更改它们在生成的文档中出现的顺序。所以,为了做到这一点,我在 SwashBuckleConfig.cs

中创建了一个类
    internal class SortBodyParams : IDocumentFilter
    {
        public void Apply(SwaggerDocument swaggerDoc, SchemaRegistry schemaRegistry, IApiExplorer apiExplorer)
        {
            var properties = schemaRegistry.Definitions.Values;
            foreach (var prop in properties) // Maybe something like this
            {
                // Order params by Required type and by name

                // This code doesn't change anything, 
                // although I thought it would help to order params by name
                prop.properties
                    .OrderBy(x => x.Key)
                    .ToList()
                    .ToDictionary(x => x.Key, y => y.Value);
            }
        }
    }

并从Register方法调用类:

        public static void Register(HttpConfiguration config)
        {
            config
                .EnableSwagger(sdc =>
                {
                    ...

                    sdc.DocumentFilter<SortBodyParams>();

                    ...
                }
         }

有人可以帮我先订购所需的参数并按名称订购吗?

【问题讨论】:

  • 上述 sn-p 的一个问题是您丢弃了排序和字典转换的结果。你必须做 prop.properties = ....
  • @arynaq 你是对的,但我仍然必须订购我所需的参数,所以我认为下面的答案是最好的

标签: c# api swagger swashbuckle


【解决方案1】:

解决这个问题想了很久,终于想出了一个解决办法:

internal class SortBodyParams : IDocumentFilter
    {
        private Schema _schema = new Schema();

        public void Apply(
            SwaggerDocument swaggerDoc, 
            SchemaRegistry schemaRegistry, 
            IApiExplorer apiExplorer)
        {
            foreach (var schema in schemaRegistry.Definitions.Values)
            {
                _schema = schema;

                var allFields = GetAllFieldsInSchema();
                var reqFields = GetAllRequiredFieldsInSchema();

                _schema.properties.Clear();

                AddAllRequiredFieldsInSchema(allFields, reqFields);
                AddAllOptionalFieldsInSchema(allFields, reqFields);
            }
        }

        public Dictionary<string, Schema> GetAllFieldsInSchema()
        {
            return _schema.properties
                    .OrderBy(x => x.Key)
                    .ToList()
                    .ToDictionary(x => x.Key, y => y.Value);
        }

        public IList<string> GetAllRequiredFieldsInSchema()
        {
            var requiredFields = _schema.required;
            if (ThereAreRequiredFields())
            {
                return requiredFields
                        .OrderBy(x => x)
                        .ToList();
            }
            return requiredFields;
        }

        public void AddAllRequiredFieldsInSchema(
            Dictionary<string, Schema> allFields,
            IList<string> reqFields)
        {
            if (ThereAreRequiredFields())
            {
                int index = 0;
                int notAllowedIndex = reqFields.Count;
                foreach (var field in allFields)
                {
                    if (field.Key == reqFields[index])
                    {
                        _schema.properties.Add(field.Key, field.Value);
                        index++;

                        if (index == notAllowedIndex) break;
                    }
                }
            }
        }

        public void AddAllOptionalFieldsInSchema(
            Dictionary<string, Schema> allFields, 
            IList<string> reqFields)
        {
            foreach (var field in allFields)
            {
                if (ThereAreRequiredFields())
                {
                    if (!reqFields.Contains(field.Key))
                    {
                        _schema.properties.Add(field.Key, field.Value);
                    }
                }
                else
                {
                    _schema.properties.Add(field.Key, field.Value);
                }
            }
        }

        public bool ThereAreRequiredFields()
        {
            if (_schema.required != null)
            {
                return true;
            }

            return false;
        }
    }

为了解决我的问题,我只需要删除所有字段并按照我想要的顺序添加它们。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-05-09
    • 2018-10-24
    • 1970-01-01
    • 1970-01-01
    • 2021-12-17
    • 2017-01-19
    • 2015-10-24
    • 2019-05-05
    相关资源
    最近更新 更多