Skip to content

Conversation

Copy link
Contributor

Copilot AI commented Nov 18, 2025

Enables [Id] attribute to be applied directly to record primary constructor parameters, eliminating the verbose [property: Id(0)] syntax.

Changes

  • IdAttribute: Added AttributeTargets.Parameter to supported targets
  • FieldIdAssignmentHelper: Enhanced ExtractFieldIdAnnotations() to check primary constructor parameters for [Id] attributes when processing properties that match those parameters
  • Tests: Added serialization round-trip tests and code generator snapshot tests for the new syntax

Usage

// Before
[GenerateSerializer]
public record Deposit([property: Id(0)] decimal Amount)

// After
[GenerateSerializer]
public record Deposit([Id(0)] decimal Amount)

Both syntaxes remain supported. The new syntax works for simple records and records with additional properties:

[GenerateSerializer]
public record Employee([Id(0)] int Id, [Id(1)] string Name)
{
    [Id(2)]
    public string Department { get; init; }
}

Resolves #9092

Original prompt

This section details on the original issue you should resolve

<issue_title>Add support for [Id] annotations on record primary constructor parameters</issue_title>
<issue_description>Now that proper support for records (in referenced assemblies too) is implemented, it would be great to be able to annotate the ctor parameter itself, rather than the more verbose (and harder to discover) syntax for annotating the backing property generated for it. This would allow to go from this:

public record Deposit([property: Id(0)] decimal Amount)

to this:

public record Deposit([Id(0)] decimal Amount)

The generator could just treat an Id attribute on a same-name ctor parameter as a property of the type as if was applied to the property, simplifying its usage in this particular scenario and saving non-trivial amount of keystrokes (for even just a few parameters, having to add property: to every property can quickly become annoying).

Related: #9092</issue_description>

<agent_instructions>Make sure to add tests which exercise this path! Add both serialization tests and codegenerator output verification tests</agent_instructions>

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

@ReubenBond This would be great, if someone can make it work. It might involve finding the primary ctor for a record and matching the attributes on the ctor params with the resulting properties.

💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.

Microsoft Reviewers: Open in CodeFlow

…ters

Co-authored-by: ReubenBond <203839+ReubenBond@users.noreply.github.com>
Copilot AI changed the title [WIP] Add support for Id annotations on record parameters Add support for [Id] attribute on record primary constructor parameters Nov 18, 2025
Copilot AI requested a review from ReubenBond November 18, 2025 16:50
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add support for [Id] annotations on record primary constructor parameters Issue with generated codec for referenced project record message type

2 participants