Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Required members with circular reference causes CS9035 #370

Open
ardabada opened this issue Feb 7, 2025 · 0 comments · May be fixed by #374
Open

Required members with circular reference causes CS9035 #370

ardabada opened this issue Feb 7, 2025 · 0 comments · May be fixed by #374

Comments

@ardabada
Copy link

ardabada commented Feb 7, 2025

Having required members together with circular reference serialization causes CS9035 (required member must be set in the object initializer or attribute constructor) compilation error in generated file. The issue present only if GenerateType is CircularReference.

Reproduction Steps

[MemoryPackable(GenerateType.CircularReference, SerializeLayout.Explicit)]
public partial class Category
{
    [MemoryPackOrder(0)]
    public required string Id { get; init; }

    [MemoryPackOrder(1)]
    public required string Name { get; init; }

    [MemoryPackOrder(3)]
    public required Category[] Subcategories { get; init; }
}

Expected behavior
Source generated file compiles as expected.

Workaround
Remove required keyword and use null-forgiving operator where needed:

[MemoryPackable(GenerateType.CircularReference, SerializeLayout.Explicit)]
public partial class Category
{
    [MemoryPackOrder(0)]
    public string Id { get; init; } = null!;

    [MemoryPackOrder(1)]
    public string Name { get; init; } = null!;

    [MemoryPackOrder(3)]
    public Category[] Subcategories { get; init; } = null!;
}
@ardabada ardabada changed the title Having required members with circular reference causes CS9035 Required members with circular reference causes CS9035 Feb 7, 2025
@ardabada ardabada linked a pull request Feb 13, 2025 that will close this issue
2 tasks
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 a pull request may close this issue.

1 participant