在Swashbuckle中将字符串字段转换为枚举字段 [英] Convert string fields to enum fields in Swashbuckle

查看:111
本文介绍了在Swashbuckle中将字符串字段转换为枚举字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

We are using Swashbuckle to document our WebAPI project (using Owin) and are trying to modify the generated Swagger file of Swashbuckle. With the DescribeAllEnumsAsStrings() and an enum property like below, we get an expected result:

class MyResponseClass {
    public Color color;
}

enum Color {
    LightBlue,
    LightRed,
    DarkBlue,
    DarkRed
}

挥舞生成的结果:

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

挑战对我们来说,我们有一些类型为 string 的属性,但实际上我们将它们视为 enum 类型。例如:

The challenge for us is that we have some properties that are of type string but we are actually treating them as enum types. For example:

class MyResponseClass {
    public string color;
}

此属性唯一可能的值是 dark-蓝色深红色浅蓝色浅-red

The only possible values for this property are dark-blue, dark-red, light-blue, light-red.

因此,我们想要以下结果:

So, we want something like below as the result:

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

我们有很多这样的属性,它们在不同的类中具有不同的值。拥有如下所示的自定义属性以使其具有通用性将是很棒的。我不知道如何创建这样的属性并在Swashbuckle DocumentFilters OperationFilters 中使用它:

We have lots of these properties with different values in different classes. It would be great to have a custom attribute like below to make it generic. I can't figure out how to create such an attribute and use it in 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
}


推荐答案

而不是自定义属性(StringEnum)使用大摇大摆已经知道的属性,而很少知道的属性(我以前从未使用过):

Instead of a custom attribute (StringEnum) use an attribute that swagger already knows about, a little know attribute (I've never used it before):

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

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

That will inject into the parameter.pattern and then we can read it from the IDocumentSchema and transform it into an enum, here is my code:

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

和代码后面是GitHub:

TestStringEnumController .cs

SwaggerConfig.cs#L389

And the code behind that is on GitHub:
TestStringEnumController.cs
SwaggerConfig.cs#L389

这篇关于在Swashbuckle中将字符串字段转换为枚举字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆