In order to create an OpenAPI spec for the Azure Functions I have been developing, I have used a library, which enables Azure Functions to render OpenAPI document and Swagger UI.
Generally, it works pretty well, but when it comes down to handling an enum within [OpenApiParameter]
, I haven't been able to figure out how to customize it for the type of the parameter.
[OpenApiParameter(
name: "levels",
In = ParameterLocation.Query,
Required = false,
Type = typeof(List<LevelEnum>),
Description = "List of levels"
)]
By default, when converting an enum, the first index will be considered as the default enum property. However, in some cases, the enum properties are optional meaning that there should not be a default value.
Moreover, while coding I noticed that I was looking for a way to customize what should be included in the OpenApi Spec, but did not succeed in my research - I have tried using the annotations [JsonIgnore]
and [IgnoreDataMember]
, but that did not affect anything. I have read the documentation about how to show the string value of the enum properties, but unfortunately, it does always include everything and it is (seemingly) not possible to hand-pick, which should be included/excluded from the spec.
[JsonConverter(typeof(StringEnumConverter))]
public enum LevelEnum
{
[JsonIgnore]
Bronze = 0, // This will always be the default value, but providing a level is optional
Silver= 1,
Gold = 2,
Premium= 3,
}