Skip to content

Commit 4280167

Browse files
authored
Add details on enums in OpenAPI section (#33995)
* Add details on enums in OpenAPI section * Address PR Review comments
1 parent 67d95d4 commit 4280167

File tree

1 file changed

+27
-1
lines changed

1 file changed

+27
-1
lines changed

aspnetcore/fundamentals/openapi/include-metadata.md

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -504,9 +504,35 @@ Properties can also be marked as `required` with the [required](/dotnet/csharp/l
504504
#### enum
505505
506506
Enum types in C# are integer-based, but can be represented as strings in JSON with a [`[JsonConverter]`](xref:System.Text.Json.Serialization.JsonConverterAttribute) and a <xref:System.Text.Json.Serialization.JsonStringEnumConverter>. When an enum type is represented as a string in JSON, the generated schema will have an `enum` property with the string values of the enum.
507+
508+
The following example demonstrates how to use a `JsonStringEnumConverter` to represent an enum as a string in JSON:
509+
510+
```csharp
511+
[JsonConverter(typeof(JsonStringEnumConverter<DayOfTheWeekAsString>))]
512+
public enum DayOfTheWeekAsString
513+
{
514+
Sunday,
515+
Monday,
516+
Tuesday,
517+
Wednesday,
518+
Thursday,
519+
Friday,
520+
Saturday
521+
}
522+
```
523+
524+
A special case is when an enum type has the [Flags] attribute, which indicates that the enum can be treated as a bit field; that is, a set of flags. A flags enum with a [JsonConverterAttribute] will be defined as `type: string` in the generated schema with no `enum` property, since the value could be any combination of the enum values. For example, the following enum:
525+
526+
```csharp
527+
[Flags, JsonConverter(typeof(JsonStringEnumConverter<PizzaToppings>))]
528+
public enum PizzaToppings { Pepperoni = 1, Sausage = 2, Mushrooms = 4, Anchovies = 8 }
529+
```
530+
531+
could have values such as `"Pepperoni, Sausage"` or `"Sausage, Mushrooms, Anchovies"`.
532+
507533
An enum type without a [`[JsonConverter]`](xref:System.Text.Json.Serialization.JsonConverterAttribute) will be defined as `type: integer` in the generated schema.
508534
509-
**Note:** The [`[AllowedValues]`](xref:System.ComponentModel.DataAnnotations.AllowedValuesAttribute) does not set the `enum` values of a property.
535+
**Note:** The [`[AllowedValues]`](xref:System.ComponentModel.DataAnnotations.AllowedValuesAttribute) attribute does not set the `enum` values of a property.
510536
511537
#### nullable
512538

0 commit comments

Comments
 (0)