在 Swashbuckle 中将字符串字段转换为枚举字段

2024-01-03

我们正在使用 Swashbuckle 来记录我们的 WebAPI 项目(使用 Owin),并尝试修改 Swashbuckle 生成的 Swagger 文件。 随着DescribeAllEnumsAsStrings()和如下所示的枚举属性,我们得到预期的结果:

class MyResponseClass {
    public Color color;
}

enum Color {
    LightBlue,
    LightRed,
    DarkBlue,
    DarkRed
}

Swagger 生成的结果:

"color": {
  "enum": [
    "LightBlue",
    "LightRed",
    "DarkBlue",
    "DarkRed"
  ],
  "type": "string"
},

我们面临的挑战是我们有一些类型的属性string但我们实际上将它们视为enum类型。例如:

class MyResponseClass {
    public string color;
}

该属性唯一可能的值为dark-blue, dark-red, light-blue, light-red.

因此,我们想要如下所示的结果:

"color": {
  "enum": [
    "light-blue",
    "light-red",
    "dark-blue",
    "dark-red"
  ],
  "type": "string"
},

我们有很多这样的属性,在不同的类中具有不同的值。如果有一个如下所示的自定义属性以使其通用,那就太好了。我不知道如何创建这样的属性并在 Swashbuckle 中使用它DocumentFilters or OperationFilters:

public MyEndpointResponseClass {

    [StringEnum("booked", "confirmed", "reserved")]
    public string status;

    // Other properties
}

public MyEndpointRequestClass {

    [StringEnum("dark-blue", "dark-red", "light-blue", "light-red")]
    public string color;

    // Other properties
}

使用 swagger 已经知道的属性,而不是自定义属性(StringEnum),使用一点了解属性(我以前从未使用过它):

[RegularExpression("^(dark-blue|dark-red|light-blue|light-red)")]

这将注入到parameter.pattern中,然后我们可以从IDocumentSchema中读取它并将其转换为枚举,这是我的代码:

private class StringEnumDocumentFilter : IDocumentFilter
{
    public void Apply(SwaggerDocument swaggerDoc, SchemaRegistry s, IApiExplorer a)
    {                
        if (swaggerDoc.paths != null)
        {
            foreach (var path in swaggerDoc.paths)
            {
                ProcessOperation(path.Value.get);
                ProcessOperation(path.Value.put);
                ProcessOperation(path.Value.post);
                ProcessOperation(path.Value.delete);
                ProcessOperation(path.Value.options);
                ProcessOperation(path.Value.head);
                ProcessOperation(path.Value.patch);
            }
        }
    }

    private void ProcessOperation(Operation op)
    {
        if (op != null)
        {
            foreach (var param in op.parameters)
            {
                if (param.pattern != null)
                {
                    param.@enum = param.pattern
                        .Replace("^", "")
                        .Replace("(", "")
                        .Replace(")", "")
                        .Split('|');
                }
            }
        }                
    }
}

这是一个工作示例:
http://swashbuckletest.azurewebsites.net/swagger/ui/index?filter=TestStringEnum#/TestStringEnum/TestStringEnum_Post http://swashbuckletest.azurewebsites.net/swagger/ui/index?filter=TestStringEnum#/TestStringEnum/TestStringEnum_Post

其背后的代码位于 GitHub 上:
TestStringEnumController.cs https://github.com/heldersepu/SwashbuckleTest/blob/master/Swagger_Test/Controllers/TestStringEnumController.cs
SwaggerConfig.cs#L389 https://github.com/heldersepu/SwashbuckleTest/blob/master/Swagger_Test/App_Start/SwaggerConfig.cs#L389

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

在 Swashbuckle 中将字符串字段转换为枚举字段 的相关文章

随机推荐