Is it possible to add x-enum-varnames to all enums in NSwag? I have this example but then you have to specify all the enums. Is there a more generic solution? Also, this example creates a second instance ResultCode2 which I don't understand.
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() );
} );
This creates:
"ResultCode": {
"type": "integer",
"description": "",
"x-enumNames": [
"Error",
"Success"
],
"enum": [
0,
1
]
},
...
"ResultCode2": {
"title": "ResultCode",
"type": "integer",
"description": "",
"x-enumNames": [
"Error",
"Success"
],
"enum": [
0,
1
],
"x-enum-varnames": [
"Error",
"Success"
]
},
I think the best solution for me was to set json serialization of enums to string:
services.AddControllers().AddJsonOptions( options =>
{
options.JsonSerializerOptions.Converters.Add( new JsonStringEnumConverter() );
});