NSwag将x-enum-varnames标记添加到所有枚举

问题描述

是否可以向NSwag中的所有枚举添加 x-enum-varnames ?我有这个例子,但是您必须指定所有枚举。有更通用的解决方案吗?另外,此示例创建了我不理解的第二个实例 ResultCode2

https://github.com/RicoSuter/NSwag/issues/1993

public class NSwagProcessor : IOperationProcessor
{
    public bool Process( OperationProcessorContext context )
    {
        JsonSchema schema = JsonSchema.FromType<ResultCode>();
        if( schema.ExtensionData == null )
        {
            schema.ExtensionData = new Dictionary<string,object>();
        }

        string[] enumerationNames = new string[ schema.EnumerationNames.Count ];
        schema.EnumerationNames.CopyTo( enumerationNames,0 );
        schema.ExtensionData.Add( "x-enum-varnames",enumerationNames );

        if( context.Settings.TypeMappers.Any( t => t.MappedType == typeof( ResultCode ) ) == false )
        {
            context.Settings.TypeMappers.Add( new ObjectTypeMapper( typeof( ResultCode ),schema ) );
        }

        return true;
    }
}

...

services.AddOpenApiDocument( config => {
    config.OperationProcessors.Add( new NSwagProcessor() );
} );

这将创建:

"ResultCode": {
    "type": "integer","description": "","x-enumNames": [
      "Error","Success"
    ],"enum": [
      0,1
    ]
  },...

"ResultCode2": {
    "title": "ResultCode","type": "integer",1
    ],"x-enum-varnames": [
      "Error","Success"
    ]
  },

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)