Skip to content

Commit 360db23

Browse files
Describe attribute on record parameters and explain required (#34023)
* Describe attribute on record parameters and explain required * Apply suggestions from PR review Co-authored-by: Tom Dykstra <[email protected]>
1 parent abf7c15 commit 360db23

File tree

1 file changed

+21
-1
lines changed

1 file changed

+21
-1
lines changed

aspnetcore/fundamentals/openapi/include-metadata.md

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -495,11 +495,31 @@ The following table summarizes attributes from the `System.ComponentModel` names
495495
496496
Note that in controller-based apps, these attributes add filters to the operation to validate that any incoming data satisfies the constraints. In Minimal APIs, these attributes set the metadata in the generated schema but validation must be performed explicitly via an endpoint filter, in the route handler's logic, or via a third-party package.
497497
498+
Attributes can also be placed on parameters in the parameter list of a record definition but must include the `property` modifier. For example:
499+
500+
```csharp
501+
public record Todo(
502+
[property: Required]
503+
[property: Description("The unique identifier for the todo")]
504+
int Id,
505+
[property: Description("The title of the todo")]
506+
[property: MaxLength(120)]
507+
string Title,
508+
[property: Description("Whether the todo has been completed")]
509+
bool Completed
510+
) {}
511+
```
512+
498513
### Other sources of metadata for generated schemas
499514
500515
#### required
501516
502-
Properties can also be marked as `required` with the [required](/dotnet/csharp/language-reference/proposals/csharp-11.0/required-members#required-modifier) modifier.
517+
In a class, struct, or record, properties with the [`[Required]`](xref:System.ComponentModel.DataAnnotations.RequiredAttribute) attribute or [required](/dotnet/csharp/language-reference/proposals/csharp-11.0/required-members#required-modifier) modifier are always `required` in the corresponding schema.
518+
519+
Other properties may also be required based on the constructors (implicit and explicit) for the class, struct, or record.
520+
- For a class or record class with a single public constructor, any property with the same type and name (case-insensitive match) as a parameter to the constructor is required in the corresponding schema.
521+
- For a class or record class with multiple public constructors, no other properties are required.
522+
- For a struct or record struct, no other properties are required since C# always defines an implicit parameterless constructor for a struct.
503523
504524
#### enum
505525

0 commit comments

Comments
 (0)