I'm building a REST API and a lot of the objects it returns are designed with a number of Enum
s for various settings.
Having read up on it, and in particular using advice from this SO Question:
Swagger UI Web Api documentation Present enums as strings?
I have set up my Global.asax to enable Swagger with the DescribeAllEnumsAsStrings
option:
(It should be noted here that the parent project which provides the API is a .NET Framework application, written in VB which I do plan to convert to a .NET Core application but being a big project, migration will take time, so code above is for .NET Framework VB, whereas code below, the consuming Client is a .NET Core website application using C#)
The client code (adding using Visual Studio > Add Rest Client (Swagger)) generates all the objects correctly, and Intellisense knows the string
options for the Enum
s:
Yet, when I dump
the JSON object fetched by the API, the Enum
properties have integer values represented as strings:
In this instance, the API is returning some image data; images have 4 size variants and the Image.Size class gives the user information about the sizes. SizeType
is an Enum
used to determine which image to use for different purposes.
For the convenience of the clients using this API I would like the Enum
type to be reflected and produced by the generated API code, or at least, the Enum
properties be string values that correspond to the Enum
name, thus a bit of code like:
i.Sizes.FirstOrDefault(s => s.SizeType.Equals("Thumb"))
or
i.Sizes.FirstOrDefault(s => s.SizeType = SizeType.Thumb)
Will be more intuitive for the user. Currently, neither of the above find the Thumbnail image size that it should, the code to fetch this Thumbnail needs to be:
i.Sizes.FirstOrDefault(s => s.SizeType = "3")
Obviously that isn't as intuitive as the above examples, and I am not sure why, or what, if any, Swagger
option would change it.