Skip to content

OptionsValidator source generator does not escape keyword namespace segments #130460

Description

@UditDewan

Description

Follow-up from the review of #130415 (#130415 (comment)).

That PR escapes keyword identifiers in member-access position and in model type FQNs, but the validator's namespace still flows into the generated code unescaped. ValidatorType.Namespace is built from ContainingNamespace.ToString() (Parser.cs, both for declared validators and in AddSynthesizedValidator), which does not escape keyword segments.

So an [OptionsValidator] type declared in a namespace containing a keyword segment, e.g.:

namespace @class.Options
{
    [OptionsValidator]
    public partial class MyOptionsValidator : IValidateOptions<MyOptions>;
}

still generates namespace class.Options { ... }, which does not compile. The same unescaped string is also used to build references to synthesized validators (global:: + Namespace + . + Name), so those call sites are affected the same way when a model type lives in such a namespace.

Expected behavior

Generated code compiles; keyword namespace segments are emitted with @ escaping (e.g. namespace @class.Options), for both the namespace declaration and synthesized validator references.

Notes

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions