[OptionsValidator] CompareAttribute.OtherProperty is interpreted as C# member-access syntax without symbol resolution
Description
The [OptionsValidator] source generator records CompareAttribute.OtherProperty as an unresolved string and later inserts it into generated C# source.
The value is used in two contexts:
- an unescaped generated string literal;
- an unquoted member-access expression.
The member-access expression passes through EscapeIdentifier, but that helper only prefixes @ for exact C# keywords. It does not resolve the name to an IPropertySymbol or ensure that the value is one valid property identifier.
Consequently, [Compare("Password.Length")] is generated as .Password.Length. Reflection-based CompareAttribute treats "Password.Length" as one literal property name and reports that it does not exist, while the generated validator treats it as nested member access and may successfully validate against Password.Length.
Reproduction Steps
Use this model and validator:
using System.ComponentModel.DataAnnotations;
using Microsoft.Extensions.Options;
namespace CompareAttributeGeneratorRepro;
public sealed class RegistrationOptions
{
public string Password { get; set; } = string.Empty;
[Compare("Password.Length")]
public int ConfirmPasswordLength { get; set; }
}
[OptionsValidator]
public sealed partial class RegistrationOptionsValidator
: IValidateOptions<RegistrationOptions>
{
}
The generated Validators.g.cs contains:
if (validationContext.ObjectInstance
is global::CompareAttributeGeneratorRepro.RegistrationOptions
&& OtherProperty == "Password.Length")
{
result = Equals(
value,
((global::CompareAttributeGeneratorRepro.RegistrationOptions)
validationContext.ObjectInstance)
.Password.Length);
}
Run both generated and reflection-based validation:
var options = new RegistrationOptions
{
Password = "abcde",
ConfirmPasswordLength = 5,
};
ValidateOptionsResult generatedResult =
new RegistrationOptionsValidator().Validate(
name: null,
options);
Console.WriteLine(
$"Generated options validator succeeded: {generatedResult.Succeeded}");
var validationResults = new List<ValidationResult>();
bool reflectionResult = Validator.TryValidateObject(
options,
new ValidationContext(options),
validationResults,
validateAllProperties: true);
Console.WriteLine(
$"Reflection-based DataAnnotations succeeded: {reflectionResult}");
foreach (ValidationResult result in validationResults)
{
Console.WriteLine(
$"Reflection-based error: {result.ErrorMessage}");
}
Observed output:
Generated options validator succeeded: True
Reflection-based DataAnnotations succeeded: False
Reflection-based error: Could not find a property named Password.Length.
Expected behavior
The generator should resolve CompareAttribute.OtherProperty to an actual IPropertySymbol on the validated model before generating a member-access expression.
For "Password.Length", no single property with that name exists. The generator should report a clear diagnostic at the attribute and must not emit .Password.Length from the raw string.
Generated validation should not silently implement different semantics from reflection-based CompareAttribute.
Actual behavior
For [Compare("Password.Length")], the generated options validator succeeds when ConfirmPasswordLength equals Password.Length, while reflection-based DataAnnotations validation fails with:
Could not find a property named Password.Length.
The generated validator therefore behaves differently from reflection-based CompareAttribute.
Regression?
This appears to be longstanding rather than a recent regression.
git blame shows that the raw tuple storage and most Compare-specific emission were introduced in October 2023. The member-access line was updated by #130415 in July 2026 to add keyword escaping, but property-symbol resolution was not added.
Known Workarounds
Use nameof with a real direct property:
[Compare(nameof(Password))]
public string ConfirmPassword { get; set; } = string.Empty;
Avoid dotted paths or arbitrary strings in CompareAttribute.
For comparisons involving nested values or computed values, expose a direct comparison property or use a custom validation mechanism.
Configuration
SDK: 11.0.100-rc.1.26420.103
Target: net11.0
OS: Windows 10.0.26200
Architecture: x64
RID: win-x64
Other information
The generator already performs real property-symbol lookup for length attributes through:
ParserUtilities.TypeHasProperty
called from:
Parser.LengthBasedAttributeIsTrackedForSubstitution
A Compare-specific helper could similarly resolve and return an IPropertySymbol, including inherited-property handling, before the emitter constructs source.
The existing test at Main.cs#L1277-L1335 uses [Compare("val")] while the property is named Val, but only verifies that the generator reports no diagnostics; it does not verify that the emitted source compiles or that the property resolves.
The closest related item is #130318, fixed by #130415, but that issue covers C# keyword escaping rather than property-symbol resolution or dotted-path semantics.
[!NOTE] This issue was generated with the assistance of GitHub Copilot.
Tagging subscribers to this area: @dotnet/area-extensions-options See info in area-owners.md if you want to be subscribed.
Hi, I can work on this issue. I propose resolving CompareAttribute.OtherProperty to a single property symbol on the validated options type, and reporting a generator diagnostic when the name does not identify such a property. This would keep source-generated validation aligned with reflection semantics and avoid emitting member access from the raw attribute string. I would add coverage for a direct property name, a dotted name, and a missing name. Does this direction match the area owners' expectations? I will wait for confirmation before implementing, per the contribution guide.