-
Notifications
You must be signed in to change notification settings - Fork 339
Description
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
- Create a custom
EnumProviderthat overridesBuildFields()and adds attributes to enum field providers (e.g.,[DataMember(Name = "...")]). - Run code generation.
- The generated
.csfile 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.