Generating Typescript Enums correctly with NSwag from Swagger docs created with Swashbuckle.

We have been generating typescript classes and data clients for our angular application with NSwag by by pointing it at our AspNetCore api swagger documentation which was generated using swashbuckle.

When generating typescript enums this caused a problem that the enum option names were generated at numbers.

// How it was by default 
// This is not what we want
exports enum SomeEnum {
        _1 = 1
        _2  = 2

For obvious reasons we wanted the option names to be valid names indicating what they referred to. Something like:

 // This is how we want it
 exports enum SomeEnum {
    OptionOne = 1
    AnotherCoolOption  = 2

After some searching we realised this was a limitation of NSwag generating from swashbuckle. If we used NSwag server side to generate the swagger docs this wouldn’t happen.

What was required was a swashbuckle filter in the pipeline to add additional metadata to the enum schema.

 public class XEnumNamesSchemaFilter : ISchemaFilter
        public void Apply(OpenApiSchema schema, SchemaFilterContext context)
            var type = context.Type;
            if (type.IsEnum)
                // Add enum type information once
                if (schema.Extensions.ContainsKey("x-enumNames")) return;

                var valuesArr = new OpenApiArray();
                                                .Select(value => new OpenApiString(value)));


To register it the following needed to be added to startup.cs

services.AddSwaggerGen(c =>