Skip to content

[C# Generator] Populate attributes from custom code properties and fields in NamedTypeSymbolProvider#10322

Merged
jorgerangel-msft merged 7 commits intomainfrom
copilot/fix-custom-code-properties-attributes
Apr 10, 2026
Merged

[C# Generator] Populate attributes from custom code properties and fields in NamedTypeSymbolProvider#10322
jorgerangel-msft merged 7 commits intomainfrom
copilot/fix-custom-code-properties-attributes

Conversation

Copy link
Copy Markdown
Contributor

Copilot AI commented Apr 9, 2026

NamedTypeSymbolProvider.BuildProperties() and BuildFields() never passed symbol attributes to the PropertyProvider/FieldProvider constructors, so CustomCodeView.Properties[*].Attributes and CustomCodeView.Fields[*].Attributes were always empty. This blocked downstream consumers (e.g., mgmt generator) from detecting attributes like [Obsolete] on custom code properties and fields.

Changes

  • NamedTypeSymbolProvider.cs: Extract AttributeData from each IPropertySymbol and IFieldSymbol and pass as attributes: parameter when constructing PropertyProvider and FieldProvider. All attributes (including internal CodeGen attributes) are passed through without filtering. Uses .ToArray() because the constructor's (attributes as IReadOnlyList<AttributeStatement>) ?? [] silently drops unmaterialized IEnumerable.
new PropertyProvider(
    ...,
    this,
    attributes: propertySymbol.GetAttributes().Select(a => new AttributeStatement(a)).ToArray())

new FieldProvider(
    ...,
    attributes: fieldSymbol.GetAttributes().Select(a => new AttributeStatement(a)).ToArray())
  • FieldProvider.cs: Updated InitializeParameter() to filter out codegen-related attributes (from the Microsoft.TypeSpec.Generator.Customizations namespace) when propagating field attributes to constructor parameters via AsParameter. This ensures user-facing attributes like [Obsolete] propagate to generated constructor parameters, while internal codegen attributes like [CodeGenMember] do not appear in generated code. The field itself retains all attributes for downstream consumers.
var paramAttributes = Attributes.Where(a => a.Type.Namespace != CodeModelGenerator.CustomizationAttributeNamespace).ToArray();
_parameter = new(() => new ParameterProvider(
    ..., attributes: paramAttributes));
  • Test: Added CanReadPropertyAttributes with a custom code file declaring [Obsolete] (with a named argument DiagnosticId), [EditorBrowsable], and a custom non-system attribute on a property, plus [Obsolete] on a field. The test validates attribute types using CSharpType.Equals, checks argument counts, validates PositionalArguments, and ensures the generator does not throw for custom non-system attributes.

…der.BuildProperties

Agent-Logs-Url: https://github.com/microsoft/typespec/sessions/24b2dbde-3903-4684-830e-2b03e1d84dd2

Co-authored-by: jorgerangel-msft <102122018+jorgerangel-msft@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 Apr 9, 2026
Copilot AI changed the title [WIP] Fix missing attributes for CustomCodeView properties [C# Generator] Populate attributes from custom code properties in NamedTypeSymbolProvider Apr 9, 2026
Copilot AI requested a review from jorgerangel-msft April 9, 2026 15:44
…pe.Equals, argument validation, and custom attribute

Agent-Logs-Url: https://github.com/microsoft/typespec/sessions/1c0103ad-e7d0-4e76-b18f-12805d30824b

Co-authored-by: jorgerangel-msft <102122018+jorgerangel-msft@users.noreply.github.com>
Copilot AI changed the title [C# Generator] Populate attributes from custom code properties in NamedTypeSymbolProvider [C# Generator] Populate attributes from custom code properties and fields in NamedTypeSymbolProvider Apr 9, 2026
@pkg-pr-new
Copy link
Copy Markdown

pkg-pr-new bot commented Apr 9, 2026

Open in StackBlitz

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

commit: ecf9570

…, update expected output

Agent-Logs-Url: https://github.com/microsoft/typespec/sessions/96747ddb-cde5-4ed0-a8b9-27a2beaca6d3

Co-authored-by: jorgerangel-msft <102122018+jorgerangel-msft@users.noreply.github.com>
…tes to constructor parameters

Agent-Logs-Url: https://github.com/microsoft/typespec/sessions/2aa57a53-e9f5-4f62-b4a3-eafbe19db041

Co-authored-by: jorgerangel-msft <102122018+jorgerangel-msft@users.noreply.github.com>
… to constructor parameters

Agent-Logs-Url: https://github.com/microsoft/typespec/sessions/52c392ca-eff3-412f-ad7b-8142cb7871b1

Co-authored-by: jorgerangel-msft <102122018+jorgerangel-msft@users.noreply.github.com>
… FieldProvider

Agent-Logs-Url: https://github.com/microsoft/typespec/sessions/c117587e-318d-4500-a614-6f4388418265

Co-authored-by: jorgerangel-msft <102122018+jorgerangel-msft@users.noreply.github.com>
@jorgerangel-msft jorgerangel-msft added this pull request to the merge queue Apr 9, 2026
@github-merge-queue github-merge-queue bot removed this pull request from the merge queue due to failed status checks Apr 9, 2026
@jorgerangel-msft jorgerangel-msft added this pull request to the merge queue Apr 10, 2026
Merged via the queue into main with commit 76466bf Apr 10, 2026
29 checks passed
@jorgerangel-msft jorgerangel-msft deleted the copilot/fix-custom-code-properties-attributes branch April 10, 2026 19:15
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.

[C# Generator] Custom code (CustomCodeView) properties missing attributes from Roslyn parser

3 participants