Repository navigation
Description
Description
The [OptionsValidator] source generator emits property and type names verbatim into identifier position without escaping C# keywords. If a validated options model has a property whose name is a keyword (declared with @, e.g. @class, @event, @namespace), the generated validator emits e.g. options.class instead of options.@class, which does not compile.
Repro (self-contained library)
MyOptions.cs:
using System.ComponentModel.DataAnnotations;
using Microsoft.Extensions.Options;
public class MyOptions
{
// Property whose name is a C# keyword; in source it must be written as @class.
[Required]
public string? @class { get; set; }
}
[OptionsValidator]
public partial class MyOptionsValidator : IValidateOptions<MyOptions>;KeywordRepro.csproj:
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net10.0</TargetFramework>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Options" Version="10.*" />
</ItemGroup>
</Project>Actual behavior
Build fails. The generated Validators.g.cs contains (note the unescaped options.class):
context.MemberName = "class"; // string literal — OK
context.DisplayName = string.IsNullOrEmpty(name) ? "MyOptions.class" : $"{name}.class";
validationAttributes.Add(global::__OptionValidationStaticInstances.__Attributes.A1);
if (!global::System.ComponentModel.DataAnnotations.Validator.TryValidateValue(options.class, context, validationResults, validationAttributes))
// ^^^^^ should be options.@classThis causes confusing compiler errors (cascading from the unescaped keyword):
error CS1520: Method must have a return type
error CS0501: 'MyOptionsValidator.MyOptionsValidator(validationResults)' must declare a body because it is not marked abstract, extern, or partial
error CS0246: The type or namespace name 'validationResults' could not be found
Reproduced with Microsoft.Extensions.Options 10.0.9.
Expected behavior
The generated code compiles, emitting options.@class (or otherwise escaping keyword identifiers).
Root cause
Two independent spots in src/libraries/Microsoft.Extensions.Options/gen:
-
Member names (the one this repro hits).
ValidatedMember.Nameis set to the rawmember.NameinParser.cs(GetMemberInfo), andEmitter.csinterpolates it directly into identifier position in ~15 places (e.g.options.{vm.Name}at the syncTryValidateValuecall and the asyncTryValidateValueAsynccall). No@-escaping is applied. -
Type/namespace FQNs.
GetFQN(Parser.cs) usesSymbolDisplayFormat.FullyQualifiedFormat.WithMiscellaneousOptions(SymbolDisplayMiscellaneousOptions.IncludeNullableReferenceTypeModifier)..WithMiscellaneousOptions(...)replaces the option set, dropping the defaultEscapeKeywordIdentifiers. So a model type or namespace segment that is a keyword would also emit unescaped. (By contrast,GetMinimalFQN/GetMinimalFQNWithoutGenericsuse.AddGenericsOptions/.WithGenericsOptionsand therefore keepEscapeKeywordIdentifiers— an inconsistency.)
Suggested fix
- For member access, escape the leaf identifier before emission (e.g. prefix
@whenSyntaxFacts.GetKeywordKind/GetContextualKeywordKindindicates a keyword, or render via aSymbolDisplayFormatthat hasEscapeKeywordIdentifiers). - For
GetFQN, use.AddMiscellaneousOptions(SymbolDisplayMiscellaneousOptions.IncludeNullableReferenceTypeModifier)instead of.WithMiscellaneousOptions(...)soEscapeKeywordIdentifiersis retained.
Notes
The in-flight async validation work (ValidateAsync emission) in #130263 uses the same options.{vm.Name} pattern, so it is affected identically and should be fixed together.
Note
This issue was generated with the assistance of AI (GitHub Copilot).
Activity
dotnet-policy-service commentedon Jul 7, 2026
Tagging subscribers to this area: @dotnet/area-extensions-options
See info in area-owners.md if you want to be subscribed.
JuanZamudioGBM commentedon Jul 15, 2026
I just wanted to add that __OptionValidationStaticInstances is not CLS-compliant and fails to compile with TreatWarningsAsErrors
Escape C# keyword identifiers in OptionsValidator generated code (#13…
svick commentedon Jul 20, 2026
@JuanZamudioGBM If that is something that matters to you, can you open a new issue for it? Thanks.