NSwag enum
https://github.com/RSuter/NJsonSchema/wiki/JsonSchemaGenerator#integer-vs-string-enumerations
Integer vs string enumerations
JSON Schema supports integer and string enumerations. In Json.NET, enums are serialized as integer by default. However you can mark a property with the JsonConverterAttribute
so that it is serialized as string. NJsonSchema looks for these attributes and generates the enum JSON Schema respecting this attribute:
// Only this property is serialized as string
[JsonConverter(typeof(StringEnumConverter))]
public Gender Gender { get; set; }
// Always serialized as string
[JsonConverter(typeof(StringEnumConverter))]
public enum Gender
{
...
}
Also see Enums
So I think I have a similar problem. I'm looking for swagger to generate enums along with the int -> string mapping. The API must accept the int. The swagger-ui matters less, what I really want is code generation with a "real" enum on the other side (android apps using retrofit in this case).
So from my research this ultimately seems to be a limit of the OpenAPI specification which Swagger uses. It's not possible to specify names and numbers for enums.
The best issue I've found to follow is https://github.com/OAI/OpenAPI-Specification/issues/681 which looks like a "maybe soon" but then Swagger would have to be updated, and in my case Swashbuckle as well.
For now my workaround has been to implement a document filter that looks for enums and populates the relevant description with the contents of the enum.
GlobalConfiguration.Configuration .EnableSwagger(c => { c.DocumentFilter<SwaggerAddEnumDescriptions>(); //disable this //c.DescribeAllEnumsAsStrings()
SwaggerAddEnumDescriptions.cs:
using System; using System.Web.Http.Description; using Swashbuckle.Swagger; using System.Collections.Generic; public class SwaggerAddEnumDescriptions : IDocumentFilter { public void Apply(SwaggerDocument swaggerDoc, SchemaRegistry schemaRegistry, IApiExplorer apiExplorer) { // add enum descriptions to result models foreach (KeyValuePair<string, Schema> schemaDictionaryItem in swaggerDoc.definitions) { Schema schema = schemaDictionaryItem.Value; foreach (KeyValuePair<string, Schema> propertyDictionaryItem in schema.properties) { Schema property = propertyDictionaryItem.Value; IList<object> propertyEnums = property.@enum; if (propertyEnums != null && propertyEnums.Count > 0) { property.description += DescribeEnum(propertyEnums); } } } // add enum descriptions to input parameters if (swaggerDoc.paths.Count > 0) { foreach (PathItem pathItem in swaggerDoc.paths.Values) { DescribeEnumParameters(pathItem.parameters); // head, patch, options, delete left out List<Operation> possibleParameterisedOperations = new List<Operation> { pathItem.get, pathItem.post, pathItem.put }; possibleParameterisedOperations.FindAll(x => x != null).ForEach(x => DescribeEnumParameters(x.parameters)); } } } private void DescribeEnumParameters(IList<Parameter> parameters) { if (parameters != null) { foreach (Parameter param in parameters) { IList<object> paramEnums = param.@enum; if (paramEnums != null && paramEnums.Count > 0) { param.description += DescribeEnum(paramEnums); } } } } private string DescribeEnum(IList<object> enums) { List<string> enumDescriptions = new List<string>(); foreach (object enumOption in enums) { enumDescriptions.Add(string.Format("{0} = {1}", (int)enumOption, Enum.GetName(enumOption.GetType(), enumOption))); } return string.Join(", ", enumDescriptions.ToArray()); } }
This results in something like the following on your swagger-ui so at least you can "see what you're doing":
作者:Chuck Lu GitHub |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了