-
Notifications
You must be signed in to change notification settings - Fork 10.4k
[Validation] Use [JsonPropertyName] for display name #63291
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
[Validation] Use [JsonPropertyName] for display name #63291
Conversation
Use `[JsonPropertyName("{value}")]` for the display name, if `[Display(Name = "{value}")` is not specified, instead of the property's name. Contributes to dotnet#63290.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull Request Overview
This PR enhances the validation generator to use JsonPropertyName attributes for display names when Display attributes are not present. This improves the user experience by providing more meaningful display names in validation error messages when JSON property names are specified.
- Modifies the type parser to check for JsonPropertyName attributes as a fallback when Display attributes are not present
- Updates the ISymbolExtensions to support extracting display names from JsonPropertyName attributes with both named arguments and constructor arguments
- Adds the JsonPropertyNameAttribute to the well-known types for proper attribute detection
Reviewed Changes
Copilot reviewed 5 out of 5 changed files in this pull request and generated 1 comment.
Show a summary per file
File | Description |
---|---|
ValidationsGenerator.TypesParser.cs | Updates display name resolution logic to use JsonPropertyName as fallback after Display attribute |
ISymbolExtensions.cs | Enhances GetDisplayName method to handle JsonPropertyName attribute extraction with both named and constructor arguments |
WellKnownTypeData.cs | Adds JsonPropertyNameAttribute to the well-known types enumeration and names array |
ValidationsGenerator.ComplexType.cs | Adds test case with ComplexTypeWithJsonPropertyName class to verify the new functionality |
ValidatableInfoResolver.g.verified.cs | Updates test snapshot to include expected generated code for the JsonPropertyName test case |
Add own method for getting `[JsonPropertyName]` instead of mingling with `[Display]`.
Give better names.
- Check for `[Display]` before any `[JsonPropertyName]`. - Add test case for records.
[Validation] Use [JsonPropertyName] for display
Use
[JsonPropertyName("{value}")]
for the display name.Description
Use
[JsonPropertyName("{value}")]
for the display name if[Display(Name = "{value}")
is not specified instead of the property's name.I didn't make any changes to use it for the name due to #62341 (comment).
Contributes to #63290.