Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions src/Orleans.CodeGenerator/FieldIdAssignmentHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,8 @@ private bool ExtractFieldIdAnnotations()
}
else if (PropertyUtility.GetMatchingPrimaryConstructorParameter(prop, _constructorParameters) is { } prm)
{
id = CodeGenerator.GetId(_libraryTypes, prop);
// Check for [Id] attribute on the primary constructor parameter
id = CodeGenerator.GetId(_libraryTypes, prm);
if (id.HasValue)
{
_symbols[member] = (id.Value, true);
Expand Down Expand Up @@ -108,7 +109,12 @@ private bool ExtractFieldIdAnnotations()
var constructorParameter = _constructorParameters.FirstOrDefault(x => x.Name.Equals(property.Name, StringComparison.OrdinalIgnoreCase));
if (constructorParameter is not null)
{
id = (uint)_constructorParameters.IndexOf(constructorParameter);
// Check for [Id] attribute on the constructor parameter
id = CodeGenerator.GetId(_libraryTypes, constructorParameter);
if (!id.HasValue)
{
id = (uint)_constructorParameters.IndexOf(constructorParameter);
}
isConstructorParameter = true;
}
}
Expand Down
1 change: 1 addition & 0 deletions src/Orleans.Serialization.Abstractions/Annotations.cs
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,7 @@ public GetCompletionSourceMethodNameAttribute(string methodName)
[AttributeUsage(
AttributeTargets.Field
| AttributeTargets.Property
| AttributeTargets.Parameter
| AttributeTargets.Class
| AttributeTargets.Struct
| AttributeTargets.Enum
Expand Down
27 changes: 27 additions & 0 deletions test/Orleans.CodeGenerator.Tests/OrleansSourceGeneratorTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,33 @@ public record class DemoDataRecordClass([property: Id(0)] string Value);
[GenerateSerializer]
public record DemoDataRecord([property: Id(0)] string Value);");

/// <summary>
/// Tests serializer generation for records with [Id] attributes on primary constructor parameters.
/// This is the new, more concise syntax that avoids the need for [property: Id(0)].
/// Verifies that:
/// - [Id] on constructor parameters is properly detected and used
/// - Generated serializers correctly handle the property-parameter relationship
/// - Both simple records and records with additional properties work correctly
/// </summary>
[Fact]
public Task TestRecordsWithParameterIdAttributes() => AssertSuccessfulSourceGeneration(
@"using Orleans;

namespace TestProject;

[GenerateSerializer]
public record SimpleRecord([Id(0)] int Value, [Id(1)] string Name);

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

[GenerateSerializer]
public record struct RecordStructWithParameterId([Id(0)] string Value);");

/// <summary>
/// Tests serializer generation for generic types.
/// Generic types require:
Expand Down
Loading
Loading