c# -在Swagger API文档中从抽象类中排序所需的参数



在问这个问题之前我已经搜索了很多,但我发现的所有内容都与其他主题有关,比如在swagger文档中排序路径。

我可以用这个简单的例子来说明我的问题是什么:

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

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

最新更新