Cysharp / Cysharp/MemoryPack

Required properties causes error

Open
#437 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

stale
Dominant language
C#
Stars
4.7k
Forks
313
PR merge metrics
No merged PRs in 30d

Description

Hi, when I have a class like:

[MemoryPackable(GenerateType.CircularReference)]
public partial class MyClass
{
    [MemoryPackOrder(0)]
    public MyOtherClass? Parent { get; set; }

    [MemoryPackOrder(1)]
    public required string Key {get; set;} = string.Empty;
}

It gives me an error:
Required member 'MyClass.Key' must be set in the object initializer or attribute constructor.

The error is inside generated file: MyClass.MemoryPackFormatter.g.cs inside Deserialize method:

if (value == null)
{
    value = new MyClass();
}

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Reproduce the example and inspect MyClass.MemoryPackFormatter.g.cs, focusing on the Deserialize method and the shown new MyClass() construction. Trace which generator entry point emits that code, then verify that the generated formatter compiles for a required property and deserialization completes without the reported error.

Written by the indexing model from the issue text.

Assessment

Tech stack
csharp
Domain
tooling
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Active
Clarity
Mostly clear
Newbie friendliness
66/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.