1

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,
}
Mr.H123
  • 81
  • 1
  • 1
  • 10
  • for omitting default value (I would describe it also as showing all possible values) take a look here https://stackoverflow.com/questions/7427909/how-to-tell-json-net-globally-to-apply-the-stringenumconverter-to-all-enums for the other part I think would be better using a separate enum for the API – Mohamed Aug 12 '22 at 10:58
  • Yeah, I was also considering a separate enum, but thought that there might be a solution. Concerning omitting the default value, how is it omitted based on the answer? I would would like to omit it from the spec, as it is redundant - Programmatically the stated default value is null in my code. – Mr.H123 Aug 12 '22 at 11:15

0 Answers0