Skip to content

[http-client-csharp] TypeProviderWriter.WriteEnumContent does not render field attributes on enum members #9946

@ArcturusZhang

Description

@ArcturusZhang

Bug Description

TypeProviderWriter.WriteEnumContent() iterates over _provider.Fields to write enum members, but it only writes XmlDocs, Name, and InitializationValue. It never writes field.Attributes, even though FieldProvider supports them and WriteField() (used for class/struct fields) does render attributes.

This means any attributes added to enum fields via BuildFields() in a custom EnumProvider are silently dropped during code generation.

Reproduction

  1. Create a custom EnumProvider that overrides BuildFields() and adds attributes to enum field providers (e.g., [DataMember(Name = "...")]).
  2. Run code generation.
  3. The generated .cs file will not contain the attributes on enum members.

Root Cause

In TypeProviderWriter.cs line 112-132:

private void WriteEnumContent(CodeWriter writer)
{
    using (writer.Scope())
    {
        for (int i = 0; i < _provider.Fields.Count; i++)
        {
            writer.WriteXmlDocsNoScope(_provider.Fields[i].XmlDocs);
            //  Missing: field.Attributes are never written here
            writer.Append($"{_provider.Fields[i].Name}");
            ...
        }
    }
}

Compare with WriteField() in CodeWriter.cs which does render attributes:

public CodeWriter WriteField(FieldProvider field)
{
    WriteXmlDocsNoScope(field.XmlDocs);
    if (field.Attributes.Count > 0)
    {
        foreach (var attr in field.Attributes)
        {
            attr.Write(this);
        }
    }
    ...
}

Suggested Fix

Add attribute rendering in WriteEnumContent before writing the field name:

writer.WriteXmlDocsNoScope(_provider.Fields[i].XmlDocs);
foreach (var attr in _provider.Fields[i].Attributes)
{
    attr.Write(writer);
}
writer.Append($"{_provider.Fields[i].Name}");

Impact

This blocks downstream generators (e.g., Azure Provisioning generator) from adding [DataMember(Name = "...")] or other custom attributes to enum members for serialization purposes.

Metadata

Metadata

Labels

emitter:client:csharpIssue for the C# client emitter: @typespec/http-client-csharp

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions