Repository navigation
Rename argument in DisplayNameInfo.GetDisplayName - #67159
Conversation
|
Hey @dotnet/aspnet-build, looks like this PR is something you want to take a look at. |
There was a problem hiding this comment.
Pull request overview
Updates the Validation display-name localization API to better represent that the “declaring type” value can be either the declaring type (property validation), the validated type (type-level validation), or null (parameter validation), and carries that rename through implementations, tests, generator output, and consumers.
Changes:
- Renames
DisplayNameInfo.GetDisplayName(..., Type? declaringType)to...Type? typeand updates related call sites. - Renames
DisplayNameLocalizationContext.DeclaringTypetoDisplayNameLocalizationContext.Typeand updates localizer/pipeline usage. - Updates source generator emitter + verified snapshots and adjusts affected tests/benchmarks/Components client validation integration.
Reviewed changes
Copilot reviewed 55 out of 55 changed files in this pull request and generated 3 comments.
Show a summary per file
| File | Description |
|---|---|
| src/Validation/src/DisplayNameInfo.cs | Renames GetDisplayName parameter to type and updates XML docs. |
| src/Validation/src/DisplayNameLocalizationContext.cs | Renames context property to Type and updates doc comment semantics. |
| src/Validation/src/ValidatableParameterInfo.cs | Updates parameter validation to call GetDisplayName(..., type: null). |
| src/Validation/src/RuntimeValidatableParameterInfoResolver.cs | Updates runtime display-name strategies to new signature (and context usage). |
| src/Validation/src/PublicAPI.Unshipped.txt | Reflects the renamed API surface in unshipped public API listings. |
| src/Validation/Localization/src/DefaultValidationLocalizer.cs | Switches display-name localization to use DisplayNameLocalizationContext.Type. |
| src/Validation/Localization/test/DefaultValidationLocalizerTests.cs | Updates localization tests to assert against the renamed Type property. |
| src/Validation/Localization/test/ValidationLocalizationPipelineTests.cs | Updates pipeline tests to populate the renamed Type property. |
| src/Validation/gen/Emitters/ValidationsGenerator.Emitter.cs | Updates emitted generator code to the renamed parameter/property. |
| src/Validation/test/Microsoft.Extensions.Validation.Tests/TestDisplayNameInfo.cs | Updates test doubles to populate DisplayNameLocalizationContext.Type. |
| src/Validation/test/Microsoft.Extensions.Validation.Tests/RuntimeValidatableParameterInfoResolverTests.cs | Updates tests for renamed property/argument usage. |
| src/Validation/test/Microsoft.Extensions.Validation.Tests/ValidationLocalizationIntegrationTests.cs | Updates integration assertions to use displayCall.Type. |
| src/Validation/test/Microsoft.Extensions.Validation.GeneratorTests/snapshots/ValidationsGeneratorTests.ValidatesPropertiesWithJsonIgnoreWhenWritingConditions#ValidatableInfoResolver.g.verified.cs | Updates verified generator output snapshot for renamed API. |
| src/Validation/test/Microsoft.Extensions.Validation.GeneratorTests/snapshots/ValidationsGeneratorTests.SkipsNonReadableAndStaticProperties#ValidatableInfoResolver.g.verified.cs | Updates verified generator output snapshot for renamed API. |
| src/Validation/test/Microsoft.Extensions.Validation.GeneratorTests/snapshots/ValidationsGeneratorTests.SkipsIndexerPropertiesOnTypes#ValidatableInfoResolver.g.verified.cs | Updates verified generator output snapshot for renamed API. |
| src/Validation/test/Microsoft.Extensions.Validation.GeneratorTests/snapshots/ValidationsGeneratorTests.SkipsClassesWithNonAccessibleTypes#ValidatableInfoResolver.g.verified.cs | Updates verified generator output snapshot for renamed API. |
| src/Validation/test/Microsoft.Extensions.Validation.GeneratorTests/snapshots/ValidationsGeneratorTests.DoesNotEmitForExemptTypes#ValidatableInfoResolver.g.verified.cs | Updates verified generator output snapshot for renamed API. |
| src/Validation/test/Microsoft.Extensions.Validation.GeneratorTests/snapshots/ValidationsGeneratorTests.DoesNotEmit_ForSkipValidationAttribute_OnRecordProperties#ValidatableInfoResolver.g.verified.cs | Updates verified generator output snapshot for renamed API. |
| src/Validation/test/Microsoft.Extensions.Validation.GeneratorTests/snapshots/ValidationsGeneratorTests.DoesNotEmit_ForSkipValidationAttribute_OnEndpointParameters#ValidatableInfoResolver.g.verified.cs | Updates verified generator output snapshot for renamed API. |
| src/Validation/test/Microsoft.Extensions.Validation.GeneratorTests/snapshots/ValidationsGeneratorTests.DoesNotEmit_ForSkipValidationAttribute_OnClassProperties#ValidatableInfoResolver.g.verified.cs | Updates verified generator output snapshot for renamed API. |
| src/Validation/test/Microsoft.Extensions.Validation.GeneratorTests/snapshots/ValidationsGeneratorTests.CanValidateValidationAttributesOnClasses#ValidatableInfoResolver.g.verified.cs | Updates verified generator output snapshot for renamed API. |
| src/Validation/test/Microsoft.Extensions.Validation.GeneratorTests/snapshots/ValidationsGeneratorTests.CanValidateTypeWithParsableProperties#ValidatableInfoResolver.g.verified.cs | Updates verified generator output snapshot for renamed API. |
| src/Validation/test/Microsoft.Extensions.Validation.GeneratorTests/snapshots/ValidationsGeneratorTests.CanValidateRecursiveTypes#ValidatableInfoResolver.g.verified.cs | Updates verified generator output snapshot for renamed API. |
| src/Validation/test/Microsoft.Extensions.Validation.GeneratorTests/snapshots/ValidationsGeneratorTests.CanValidateRecordTypesWithAttribute#ValidatableInfoResolver.g.verified.cs | Updates verified generator output snapshot for renamed API. |
| src/Validation/test/Microsoft.Extensions.Validation.GeneratorTests/snapshots/ValidationsGeneratorTests.CanValidateRecordTypes#ValidatableInfoResolver.g.verified.cs | Updates verified generator output snapshot for renamed API. |
| src/Validation/test/Microsoft.Extensions.Validation.GeneratorTests/snapshots/ValidationsGeneratorTests.CanValidateRecordStructTypes#ValidatableInfoResolver.g.verified.cs | Updates verified generator output snapshot for renamed API. |
| src/Validation/test/Microsoft.Extensions.Validation.GeneratorTests/snapshots/ValidationsGeneratorTests.CanValidatePolymorphicTypes#ValidatableInfoResolver.g.verified.cs | Updates verified generator output snapshot for renamed API. |
| src/Validation/test/Microsoft.Extensions.Validation.GeneratorTests/snapshots/ValidationsGeneratorTests.CanValidateParameters#ValidatableInfoResolver.g.verified.cs | Updates verified generator output snapshot for renamed API. |
| src/Validation/test/Microsoft.Extensions.Validation.GeneratorTests/snapshots/ValidationsGeneratorTests.CanValidateMultipleNamespaces#ValidatableInfoResolver.g.verified.cs | Updates verified generator output snapshot for renamed API. |
| src/Validation/test/Microsoft.Extensions.Validation.GeneratorTests/snapshots/ValidationsGeneratorTests.CanValidateIValidatableObject#ValidatableInfoResolver.g.verified.cs | Updates verified generator output snapshot for renamed API. |
| src/Validation/test/Microsoft.Extensions.Validation.GeneratorTests/snapshots/ValidationsGeneratorTests.CanValidateIValidatableObject_WithoutPropertyValidations#ValidatableInfoResolver.g.verified.cs | Updates verified generator output snapshot for renamed API. |
| src/Validation/test/Microsoft.Extensions.Validation.GeneratorTests/snapshots/ValidationsGeneratorTests.CanValidateComplexTypesWithJsonIgnore#ValidatableInfoResolver.g.verified.cs | Updates verified generator output snapshot for renamed API. |
| src/Validation/test/Microsoft.Extensions.Validation.GeneratorTests/snapshots/ValidationsGeneratorTests.CanValidateComplexTypes#ValidatableInfoResolver.g.verified.cs | Updates verified generator output snapshot for renamed API. |
| src/Validation/test/Microsoft.Extensions.Validation.GeneratorTests/snapshots/ValidationsGeneratorTests.CanValidateClassTypesWithAttribute#ValidatableInfoResolver.g.verified.cs | Updates verified generator output snapshot for renamed API. |
| src/Validation/test/Microsoft.Extensions.Validation.GeneratorTests/snapshots/ValidationsGeneratorTests.CanUseBothFrameworkAndGeneratedValidatableTypeAttributes#ValidatableInfoResolver.g.verified.cs | Updates verified generator output snapshot for renamed API. |
| src/Validation/test/Microsoft.Extensions.Validation.GeneratorTests/snapshots/ValidationsGeneratorTests.CanGenerateWhenAddValidationCalledMultipleTimes#ValidatableInfoResolver.g.verified.cs | Updates verified generator output snapshot for renamed API. |
| src/Validation/test/Microsoft.Extensions.Validation.GeneratorTests/snapshots/ValidationsGeneratorTests.CanDiscoverGeneratedValidatableTypeAttribute#ValidatableInfoResolver.g.verified.cs | Updates verified generator output snapshot for renamed API. |
| src/Validation/test/Microsoft.Extensions.Validation.GeneratorTests/snapshots/ValidationsGeneratorDisplayNameTests.TypeDisplayName_WithResourceType#ValidatableInfoResolver.g.verified.cs | Updates verified generator output snapshot for renamed API. |
| src/Validation/test/Microsoft.Extensions.Validation.GeneratorTests/snapshots/ValidationsGeneratorDisplayNameTests.TypeDisplayName_WithNameOnly#ValidatableInfoResolver.g.verified.cs | Updates verified generator output snapshot for renamed API. |
| src/Validation/test/Microsoft.Extensions.Validation.GeneratorTests/snapshots/ValidationsGeneratorDisplayNameTests.TypeDisplayName_WithDisplayNameAttribute#ValidatableInfoResolver.g.verified.cs | Updates verified generator output snapshot for renamed API. |
| src/Validation/test/Microsoft.Extensions.Validation.GeneratorTests/snapshots/ValidationsGeneratorDisplayNameTests.RecordPropertyDisplayName_WithResourceType#ValidatableInfoResolver.g.verified.cs | Updates verified generator output snapshot for renamed API. |
| src/Validation/test/Microsoft.Extensions.Validation.GeneratorTests/snapshots/ValidationsGeneratorDisplayNameTests.RecordPropertyDisplayName_WithNameOnly#ValidatableInfoResolver.g.verified.cs | Updates verified generator output snapshot for renamed API. |
| src/Validation/test/Microsoft.Extensions.Validation.GeneratorTests/snapshots/ValidationsGeneratorDisplayNameTests.RecordPropertyDisplayName_LiteralOnConstructorParameter#ValidatableInfoResolver.g.verified.cs | Updates verified generator output snapshot for renamed API. |
| src/Validation/test/Microsoft.Extensions.Validation.GeneratorTests/snapshots/ValidationsGeneratorDisplayNameTests.PropertyDisplayName_WithResourceType#ValidatableInfoResolver.g.verified.cs | Updates verified generator output snapshot for renamed API. |
| src/Validation/test/Microsoft.Extensions.Validation.GeneratorTests/snapshots/ValidationsGeneratorDisplayNameTests.PropertyDisplayName_WithoutDisplayAttribute_UsesPropertyName#ValidatableInfoResolver.g.verified.cs | Updates verified generator output snapshot for renamed API. |
| src/Validation/test/Microsoft.Extensions.Validation.GeneratorTests/snapshots/ValidationsGeneratorDisplayNameTests.PropertyDisplayName_WithNameOnly#ValidatableInfoResolver.g.verified.cs | Updates verified generator output snapshot for renamed API. |
| src/Validation/test/Microsoft.Extensions.Validation.GeneratorTests/snapshots/ValidationsGeneratorDisplayNameTests.PropertyDisplayName_WithDisplayNameAttribute#ValidatableInfoResolver.g.verified.cs | Updates verified generator output snapshot for renamed API. |
| src/Validation/test/Microsoft.Extensions.Validation.GeneratorTests/snapshots/ValidationsGeneratorDisplayNameTests.PropertyDisplayName_WithControlCharacters_EmitsValidLiteral#ValidatableInfoResolver.g.verified.cs | Updates verified generator output snapshot for renamed API. |
| src/Validation/test/Microsoft.Extensions.Validation.GeneratorTests/snapshots/ValidationsGeneratorDisplayNameTests.PropertyDisplayName_DisplayAttributeTakesPrecedenceOverDisplayNameAttribute#ValidatableInfoResolver.g.verified.cs | Updates verified generator output snapshot for renamed API. |
| src/Validation/test/Microsoft.Extensions.Validation.GeneratorTests/snapshots/ValidationsGeneratorDisplayNameTests.PropertyDisplayName_DisplayAttributeResourceTypeTakesPrecedenceOverDisplayNameAttribute#ValidatableInfoResolver.g.verified.cs | Updates verified generator output snapshot for renamed API. |
| src/Validation/test/Microsoft.Extensions.Validation.GeneratorTests/snapshots/ValidationsGeneratorDisplayNameTests.ParameterDisplayName_WithResourceType#ValidatableInfoResolver.g.verified.cs | Updates verified generator output snapshot for renamed API. |
| src/Validation/test/Microsoft.Extensions.Validation.GeneratorTests/snapshots/ValidationsGeneratorDisplayNameTests.ParameterDisplayName_WithNameOnly#ValidatableInfoResolver.g.verified.cs | Updates verified generator output snapshot for renamed API. |
| src/Http/Http/perf/Microbenchmarks/ValidatableTypesBenchmark.cs | Updates benchmark mock DisplayNameInfo implementation signature. |
| src/Components/Forms/src/ClientValidation/DefaultClientValidationService.cs | Updates Blazor client validation display-name localization context to Type. |
| src/Components/Forms/test/DefaultClientValidationServiceTest.cs | Updates Blazor client validation tests to assert against Type. |
| return localizer.ResolveDisplayName(new DisplayNameLocalizationContext | ||
| { | ||
| DeclaringType = declaringType, | ||
| Type = null, | ||
| DisplayName = literal, | ||
| MemberName = memberName, | ||
| }) ?? literal; |
| return localizer.ResolveDisplayName(new DisplayNameLocalizationContext | ||
| { | ||
| DeclaringType = declaringType, | ||
| Type = null, |
There was a problem hiding this comment.
| Type = null, | |
| Type = type, |
Are we missing test coverage?
There was a problem hiding this comment.
I thought it was intentional that Type is:
- containing type for the case of ValidatablePropertyInfo.
- the actual type being validated in case of ValidatableTypeInfo.
- null in case of ValidatableParameterInfo.
That was why the signatures needed Type? (nullable).
If we think we want to put the parameter type there as well, I think all relevant signatures should then be changed from Type? to Type?
There was a problem hiding this comment.
@halter73 As Youssef is saying, the null here is because parameters (so far) do not use the type in localization contexts and this implementation of DisplayNameInfo is private to RuntimeValidatableParameterInfoResolver which (so far) only produces ValidatableInfo for parameters. That said, I immediately wanted to revert this change after leaving the office because I realized it will only trip someone up if they complete the resolver implementation to support properties/types as well.
@Youssef1313 We could pass in the parameter type here and in ErrorMessageLocalizationContext. The reason why this distinction was made (here and elsewhere historically), is that when type is used to resolve/scope resources, it makes sense only for non-primitive types. That is why properties use the containing type - because it would typically not make sense to scope the resource based on e.g. System.String. The problem is that for Minimal API endpoint parameters, there is no such containing type. Note that MVC could avoid this problem because endpoints where defined in controller classes so the controller could be use to scope the resources.
However, to get back to your question, there are multiple ways we could approach this (both in the display name and error message context), for example:
- a) Keep the logic as is.
- b) Keep the logic as is for types and for properties, but pass the parameter type for parameters (either unconditionally or only for non-primitive parameter types).
- c) Include separate
Type typeandType? containingTypeto remove the ambiguity from whattyperefers to, and also allow resolving resources per property type, not the containing type.
I am not against any of these options. A typical Minimal API app that wants to use RESX files for localization data would still need to configure localization with a shared resource file (or some other custom strategy) because the default convention does not work for primitive parameters (even if we pass the parameter type as type here).
There was a problem hiding this comment.
Regardless of the course we take, I would do it in a separate issue/PR and merge this just with the naming change. (I rolled back the null change flagged by copilot and Stephen.)
There was a problem hiding this comment.
(I rolled back the null change flagged by copilot and Stephen.)
Last commit has change from null to use the type, so either it's not rolled back or I misunderstand your comment.
There was a problem hiding this comment.
Oh I see it was originally using type. My bad
Co-authored-by: Stephen Halter <[email protected]>
Following API review, renames the
Type? declaringTypeargument inDisplayNameInfo.GetDisplayNameto justType? typeto better reflect that it can hold the validated type itself in case of type-level validation.For consistency also renames the
Type? DeclaringTypeproperty inDisplayNameLocalizationContexttoType? Type. (This property is populated from the renamed argument inDisplayNameInfo.GetDisplayName.)Fixes #66378