Skip to content

Conversation

martincostello
Copy link
Member

[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.

Use `[JsonPropertyName("{value}")]` for the display name, if `[Display(Name = "{value}")` is not specified, instead of the property's name.

Contributes to dotnet#63290.
@Copilot Copilot AI review requested due to automatic review settings August 16, 2025 12:21
Copy link
Contributor

@Copilot Copilot AI left a 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

@github-actions github-actions bot added the needs-area-label Used by the dotnet-issue-labeler to label those issues which couldn't be triaged automatically label Aug 16, 2025
@martincostello martincostello added area-minimal Includes minimal APIs, endpoint filters, parameter binding, request delegate generator etc feature-validation Issues related to model validation in minimal and controller-based APIs and removed needs-area-label Used by the dotnet-issue-labeler to label those issues which couldn't be triaged automatically labels Aug 16, 2025
@dotnet-policy-service dotnet-policy-service bot added the community-contribution Indicates that the PR has been added by a community member label Aug 16, 2025
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.
@dotnet-policy-service dotnet-policy-service bot added the pending-ci-rerun When assigned to a PR indicates that the CI checks should be rerun label Aug 26, 2025
@dotnet-policy-service dotnet-policy-service bot added this to the 11.0-preview1 milestone Aug 26, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-minimal Includes minimal APIs, endpoint filters, parameter binding, request delegate generator etc community-contribution Indicates that the PR has been added by a community member feature-validation Issues related to model validation in minimal and controller-based APIs pending-ci-rerun When assigned to a PR indicates that the CI checks should be rerun
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant