Azure / Azure/azure-functions-openapi-extension

Add [JsonConverter(typeof(StringEnumConverter))] to Enums outside of my Assembly

Open
#284 4 comments 7 reactions 0 assignees View on GitHub
Dominant language
C#
Stars
388
Forks
202
PR merge metrics
No merged PRs in 30d

Description

Hey,

I'm running into an issue with enums showing up as numbers in the swagger UI. According to the documentation I need to add `[JsonConverter(typeof(StringEnumConverter))]` to the enum file. However, for enums outside of my assembly or in Nuget packages I can not add that attribute. I tried doing it via reflection in the Azure Functions Startup.cs. see code below:
``` csharp
// add [JsonConverter(typeof(StringEnumConverter))]
var nugetAssembly = typeof(IBaseModel).Assembly;
var enumTypes = nugetAssembly.GetTypes().Where(t => t.IsEnum).ToList();
enumTypes.ForEach(e => TypeDescriptor.AddAttributes(e, new JsonConverterAttribute(e)));
```


I confirmed by printing out the Attributes that the JsonConverterAttribute is in the enum in that assembly, but the swagger documentation still shows them as ints instead of string.

If this is a known bug please let me know.

Thanks!

Contributor guide

Open the contributing guide

Research direction

Start in the Azure Functions Startup.cs example and reproduce the external-enum case using the supplied TypeDescriptor.AddAttributes code. Trace how enum schemas are generated for NuGet assemblies and compare the result with the Swagger UI output. Done means externally defined enums appear as strings rather than integers in the generated OpenAPI documentation.

Written by the indexing model from the issue text.

Assessment

Tech stack
azure, csharp, openapi
Domain
api
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.