Skip to content

System.Text.Json source generation producing invalid code on generic JsonSerializable types #61782

Closed

Description

@eiriktsarpalis

Description

Passing a generic type definition in a JsonSerializableAttribute results in invalid C# code being generated, namely type parameters are erroneously treated as named type definitions.

Reproduction Steps

[JsonSerializable(typeof(HashSet<>))]
internal partial class MyJsonSerializerContext : JsonSerializerContext { }

Expected behavior

Should fail with a relevant diagnostic message.

Actual behavior

Produces invalid C# code that fails to type check:

    internal partial class MyJsonSerializerContext
    {
        private global::System.Text.Json.Serialization.Metadata.JsonTypeInfo<global::System.Collections.Generic.HashSet<global::T>>? _HashSetT;
    }
error CS0400: The type or namespace name 'T' could not be found in the global namespace

Regression?

No response

Known Workarounds

No response

Configuration

No response

Other information

We could either emit an error diagnostic when users specify generic types or we could try supporting them. Note however that generic types would require generating factories instead of properties that current model is using, for example:

JsonSerializer.Serialize(hashset, MyJsonSerializerContext.Default.CreateHashSetT());

Activity

  1. added this to the 7.0.0 milestone on Nov 18, 2021
  2. ghost added
    untriagedNew issue has not been triaged by the area owner
    on Nov 18, 2021
  3. added
    enhancementProduct code improvement that does NOT require public API changes/additions
    and removed
    untriagedNew issue has not been triaged by the area owner
    on Nov 18, 2021
  4. modified the milestones: 7.0.0, 8.0.0 on Jul 19, 2022
  5. eiriktsarpalis commented on Jul 19, 2022

    @eiriktsarpalis
    MemberAuthor

    Moving to the .NET 8 milestone.

  6. modified the milestones: 8.0.0, Future on Sep 28, 2022
  7. krwq commented on Sep 28, 2022

    @krwq
    Member

    we should improve error message here for 8.0

  8. layomia commented on Dec 2, 2022

    @layomia
    Contributor

    Up for grabs. We should check if an input type has open generics and issue a diagnostic error if so. Somewhere in this method would be a good place for such logic, and would involve adding an entry to the _typeLevelDiagnostics cache.

  9. added
    help wanted[up-for-grabs] Good issue for external contributors
    on Dec 2, 2022
  10. modified the milestones: 8.0.0, Future on Jan 25, 2023
  11. krwq commented on Jan 25, 2023

    @krwq
    Member

    [Triage] We need to add the diagnostics message and possibly consider making that work. Diagnostics messages need design review.

  12. modified the milestones: Future, 8.0.0 on Jun 15, 2023
  13. ghost added
    in-prThere is an active PR which will close this issue when it is merged
    on Jun 20, 2023
  14. ghost removed
    in-prThere is an active PR which will close this issue when it is merged
    on Jun 20, 2023
  15. ghost locked as resolved and limited conversation to collaborators on Jul 20, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

area-System.Text.JsonbugenhancementProduct code improvement that does NOT require public API changes/additionshelp wanted[up-for-grabs] Good issue for external contributorssource-generatorIndicates an issue with a source generator feature

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions