Skip to content

Commit 3b31249

Browse files
committed
Add details on enums in OpenAPI section
1 parent 67d95d4 commit 3b31249

File tree

1 file changed

+20
-1
lines changed

1 file changed

+20
-1
lines changed

aspnetcore/fundamentals/openapi/include-metadata.md

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -504,9 +504,28 @@ 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 enums 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.
525+
507526
An enum type without a [`[JsonConverter]`](xref:System.Text.Json.Serialization.JsonConverterAttribute) will be defined as `type: integer` in the generated schema.
508527
509-
**Note:** The [`[AllowedValues]`](xref:System.ComponentModel.DataAnnotations.AllowedValuesAttribute) does not set the `enum` values of a property.
528+
**Note:** The [`[AllowedValues]`](xref:System.ComponentModel.DataAnnotations.AllowedValuesAttribute) attribute does not set the `enum` values of a property.
510529
511530
#### nullable
512531

0 commit comments

Comments
 (0)