Skip to content

Code generated by Json source generator results in warnings in projects with nullable enabled #52227

Closed

Description

@pranavkm

I think it's an issue with the nullability annotations on the getters and setters for JsonMetadataServices.CreatePropertyInfo

These should be:

- Func<object, T>? getter,
+ Func<object, T?>? getter,

etc

Activity

  1. ghost added
    untriagedNew issue has not been triaged by the area owner
    on May 4, 2021
  2. self-assigned this
    on May 4, 2021
  3. removed
    untriagedNew issue has not been triaged by the area owner
    on May 4, 2021
  4. added this to the 6.0.0 milestone on May 4, 2021
  5. Trolldemorted commented on Jun 22, 2021

    @Trolldemorted

    Aren't the warnings correct in this case, because System.Text.Json ignores the non-nullability?

  6. ghost added
    in-prThere is an active PR which will close this issue when it is merged
    on Aug 10, 2021
  7. layomia commented on Aug 10, 2021

    @layomia
    Contributor

    Aren't the warnings correct in this case, because System.Text.Json ignores the non-nullability?

    There's robust logic in STJ to handle null on serialization and deserialization, including to support features like ignoring null on serialization. Because of this we don't leak NullReferenceException and related bugs. However, it would be most correct to accept T? on deserialization and return T? on serialization, since property values could indeed be null. Unfortunately, I couldn't find a way to express this in a prototype to have the correct semantics, due error CS0453 given this code.

    In the fix for the issue #57178, I left the parameter type as Func<object, T>? getter and just banged the return value of the getter call in the generated code.

  8. ghost removed
    in-prThere is an active PR which will close this issue when it is merged
    on Aug 12, 2021
  9. ghost locked as resolved and limited conversation to collaborators on Sep 11, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions