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

GenerateSerializer should be compatible with the required keyword. #8367

Closed
jamescarter-le opened this issue Apr 4, 2023 · 5 comments · Fixed by #8595
Closed

GenerateSerializer should be compatible with the required keyword. #8367

jamescarter-le opened this issue Apr 4, 2023 · 5 comments · Fixed by #8595

Comments

@jamescarter-le
Copy link
Contributor

Currently the compiler throws an error when trying to use the required keyword with GenerateSerializer

[GenerateSerializer]
public class Foo
{
    [Id(0)] public required string Bar{ get; set; }

Gives error:

Required member 'Foo.Bar' must be set in the object initializer or attribute constructor.

@ghost ghost added the Needs: triage 🔍 label Apr 4, 2023
@ReubenBond ReubenBond self-assigned this Apr 4, 2023
@ReubenBond
Copy link
Member

We will need to fix this ASAP. Thank you for reporting it, @jamescarter-le.
I think the only viable fix is to avoid using new for types which have required properties, even if they have an accessible zero-parameter constructor.

For now, as a workaround, I can think of a couple of options:

  • Do not use required
  • Add the [UseActivator] attribute to your types which have required properties.
  • Avoid having an accessible (public or internal) zero-parameter constructor

@jamescarter-le
Copy link
Contributor Author

jamescarter-le commented Apr 4, 2023 via email

@onionhammer
Copy link
Contributor

Any update on this?

@ReubenBond
Copy link
Member

I am looking into this now. For types with required members, we will need to not call the constructor, since the error cannot be suppressed and it is emitted at the constructor call site.

@ReubenBond
Copy link
Member

ReubenBond commented Aug 30, 2023

Fix PR opened: #8595. Thank you for bumping the issue

@ghost ghost added the Status: Fixed label Aug 30, 2023
@ghost ghost locked as resolved and limited conversation to collaborators Sep 29, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants