[英]Swagger Swashbuckle Asp.NET Core: show details about every enum is used
I have the following enum:我有以下枚举:
public enum TicketQuestionType
{
General = 1,
Billing = 2,
TMS = 3,
HOS = 4,
DeviceManagement = 5
}
and model class:和 model class:
public class TicketCreateApi
{
public string Subject { get; set; }
public TicketQuestionType QuestionType { get; set; } = TicketQuestionType.General;
public TicketType Type { get; set; } = TicketType.Problem;
public TicketStatus Status { get; set; } = TicketStatus.New;
public TicketPriority Priority { get; set; } = TicketPriority.Normal;
public string Description { get; set; }
public List<string> Attachments { get; set; }
public int? DeviceId { get; set; }
public int? DriverId { get; set; }
}
my API method uses it:我的 API 方法使用它:
Task<IActionResult> Create(TicketCreateApi model);
Swagger generates the following: Swagger 生成以下内容:
and this:和这个:
so, we can see only default value and no way to see available list of enum (names and values).因此,我们只能看到默认值,而无法看到可用的枚举列表(名称和值)。 I would like to show it.
我想展示它。 How to do it?
怎么做?
we can see only default value and no way to see available list of enum (names and values).
我们只能看到默认值,无法看到可用的枚举列表(名称和值)。 I would like to show it.
我想展示它。 How to do it?
怎么做?
To display the enums as strings in swagger, you configure the JsonStringEnumConverter, adding the following line in ConfigureServices:要在 swagger 中将枚举显示为字符串,请配置 JsonStringEnumConverter,在 ConfigureServices 中添加以下行:
services.AddControllers().AddJsonOptions(options =>
options.JsonSerializerOptions.Converters.Add(new JsonStringEnumConverter()));
The output as below: output如下:
If you want to display the enums as stings and int values, you could try to create a EnumSchemaFilter to change the schema.如果您想将枚举显示为 stings 和 int 值,您可以尝试创建一个 EnumSchemaFilter 来更改架构。 code as below:
代码如下:
public class EnumSchemaFilter : ISchemaFilter
{
public void Apply(OpenApiSchema model, SchemaFilterContext context)
{
if (context.Type.IsEnum)
{
model.Enum.Clear();
Enum.GetNames(context.Type)
.ToList()
.ForEach(name => model.Enum.Add(new OpenApiString($"{Convert.ToInt64(Enum.Parse(context.Type, name))} = {name}")));
}
}
}
Configure the SwaggerGen to use above ShemaFilter.配置 SwaggerGen 以使用上面的 ShemaFilter。
services.AddSwaggerGen(c =>
{
c.SwaggerDoc("v1", new OpenApiInfo
{
Version = "v1",
Title = "ToDo API",
Description = "A simple example ASP.NET Core Web API",
TermsOfService = new Uri("https://example.com/terms"),
Contact = new OpenApiContact
{
Name = "Shayne Boyer",
Email = string.Empty,
Url = new Uri("https://twitter.com/spboyer"),
},
License = new OpenApiLicense
{
Name = "Use under LICX",
Url = new Uri("https://example.com/license"),
}
});
c.SchemaFilter<EnumSchemaFilter>();
});
The result like this:结果是这样的:
I tried EnumSchemaFilter.我试过 EnumSchemaFilter。 I got some error every time i submited the request.
每次提交请求时我都会遇到一些错误。 Because the serialize enum string to int
因为将枚举字符串序列化为 int
I used this code block and i hope it works我使用了这个代码块,我希望它有效
services.AddControllersWithViews()
.AddJsonOptions(
opts =>
{
var enumConverter = new JsonStringEnumConverter();
opts.JsonSerializerOptions.Converters.Add(enumConverter);
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.