微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

C# - 从 Swagger API 文档中的抽象类订购所需的参数

如何解决C# - 从 Swagger API 文档中的抽象类订购所需的参数

在问这个问题之前我已经搜索了很多,但我发现的所有内容都与其他主题有关,例如在 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>();

                    ...
                }
         }

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

解决方法

尝试解决这个问题很长时间后,我终于想出了一个解决方案:

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,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;
        }
    }

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

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。