Skip to content

[http-client-csharp] Render field attributes on enum members in TypeProviderWriter.WriteEnumContent#9947

Merged
jorgerangel-msft merged 4 commits intomainfrom
copilot/fix-enum-member-attributes
Mar 10, 2026
Merged

[http-client-csharp] Render field attributes on enum members in TypeProviderWriter.WriteEnumContent#9947
jorgerangel-msft merged 4 commits intomainfrom
copilot/fix-enum-member-attributes

Conversation

Copy link
Contributor

Copilot AI commented Mar 6, 2026

  • Add attribute rendering for enum fields in TypeProviderWriter.WriteEnumContent()
  • Add test to validate enum field attributes are written correctly
  • Add test data file for the new test
  • Run tests to validate changes (all 4 TypeProviderWriter tests pass)
  • Revert unrelated formatting/header changes from dotnet format
  • Run code review and security scanning
Original prompt

This section details on the original issue you should resolve

<issue_title>[http-client-csharp] TypeProviderWriter.WriteEnumContent does not render field attributes on enum members</issue_title>
<issue_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

  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.</issue_description>

Comments on the Issue (you are @copilot in this section)


🔒 GitHub Advanced Security automatically protects Copilot coding agent pull requests. You can protect all pull requests by enabling Advanced Security for your repositories. Learn more about Advanced Security.

…iteEnumContent

Co-authored-by: ArcturusZhang <10554446+ArcturusZhang@users.noreply.github.com>
@microsoft-github-policy-service microsoft-github-policy-service bot added the emitter:client:csharp Issue for the C# client emitter: @typespec/http-client-csharp label Mar 6, 2026
Copilot AI changed the title [WIP] Fix enum member attribute rendering in TypeProviderWriter [http-client-csharp] Render field attributes on enum members in TypeProviderWriter.WriteEnumContent Mar 6, 2026
@pkg-pr-new
Copy link

pkg-pr-new bot commented Mar 6, 2026

Open in StackBlitz

npm i https://pkg.pr.new/@typespec/http-client-csharp@9947

commit: e3a4176

Copy link
Contributor

@jorgerangel-msft jorgerangel-msft left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The core changes look good, but there's a lot of other unrelated files that are being edited. If it wasn't intentional, can we please revert those?

Co-authored-by: ArcturusZhang <10554446+ArcturusZhang@users.noreply.github.com>
Copy link
Contributor Author

Copilot AI commented Mar 10, 2026

The core changes look good, but there's a lot of other unrelated files that are being edited. If it wasn't intentional, can we please revert those?

Reverted all unrelated files in 249231b. They were unintended dotnet format changes. The PR now only touches 3 files.

@github-actions
Copy link
Contributor

No changes needing a change description found.

@jorgerangel-msft jorgerangel-msft added this pull request to the merge queue Mar 10, 2026
Merged via the queue into main with commit 3859f57 Mar 10, 2026
24 checks passed
@jorgerangel-msft jorgerangel-msft deleted the copilot/fix-enum-member-attributes branch March 10, 2026 16:34
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

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

3 participants