Skip to content

JsonSourceGenerator - ArgumentException key already added. #58985

Closed

Description

@fbrosseau

Description

6.0.100-preview.7.21379.14 on Windows x64

Following code crashes the JsonSourceGenerator:

    [JsonSerializable(typeof(B))]
    public partial class MySerializerContext : JsonSerializerContext
    {
    }

    public class B : A
    {
        [JsonIgnore]
        public override int I => 0;
    }

    public abstract class A
    {
        [JsonIgnore]
        public abstract int I { get; }
    }

With the following:

1>CSC : warning CS8785: Generator 'JsonSourceGenerator' failed to generate source. It will not contribute to the output and compilation errors may occur as a result. Exception was of type 'ArgumentException' with message 'An item with the same key has already been added.'

Workarounds:
Interestingly if I change A.I to be virtual instead of abstract, it works.
Also, if I remove JsonIgnore from the base (and keep it abstract), it also works.
So the workarounds are easy.

 	System.Text.Json.SourceGeneration.dll!System.Text.Json.SourceGeneration.JsonSourceGenerator.Parser.CacheMember(System.Text.Json.SourceGeneration.PropertyGenerationSpec propGenSpec, ref System.Collections.Generic.List<System.Text.Json.SourceGeneration.PropertyGenerationSpec> propGenSpecList, ref System.Collections.Generic.Dictionary<string, System.Text.Json.SourceGeneration.PropertyGenerationSpec> ignoredMembers)	Unknown
 	System.Text.Json.SourceGeneration.dll!System.Text.Json.SourceGeneration.JsonSourceGenerator.Parser.GetOrAddTypeGenerationSpec(System.Type type, System.Text.Json.Serialization.JsonSourceGenerationMode generationMode)	Unknown
 	System.Text.Json.SourceGeneration.dll!System.Text.Json.SourceGeneration.JsonSourceGenerator.Parser.GetRootSerializableType(Microsoft.CodeAnalysis.SemanticModel compilationSemanticModel, Microsoft.CodeAnalysis.CSharp.Syntax.AttributeSyntax attributeSyntax, System.Text.Json.Serialization.JsonSourceGenerationMode generationMode)	Unknown
 	System.Text.Json.SourceGeneration.dll!System.Text.Json.SourceGeneration.JsonSourceGenerator.Parser.GetGenerationSpec(System.Collections.Generic.List<Microsoft.CodeAnalysis.CSharp.Syntax.ClassDeclarationSyntax> classDeclarationSyntaxList)	Unknown
 	System.Text.Json.SourceGeneration.dll!System.Text.Json.SourceGeneration.JsonSourceGenerator.Execute(Microsoft.CodeAnalysis.GeneratorExecutionContext executionContext)	Unknown
 	Microsoft.CodeAnalysis.dll!Microsoft.CodeAnalysis.GeneratorDriver.RunGeneratorsCore(Microsoft.CodeAnalysis.Compilation compilation, Microsoft.CodeAnalysis.DiagnosticBag diagnosticsBag, System.Threading.CancellationToken cancellationToken)	Unknown

Activity

  1. ghost added
    untriaged
    on Sep 11, 2021
  2. am11 commented on Sep 12, 2021

    @am11
    Member

    Another non-abstract case where this warning gets triggered is when we try to hide the member of base class using new modifier:

    using System;
    using System.Text.Json;
    using System.Text.Json.Serialization;
    
    Console.WriteLine(JsonSerializer.Serialize(new B(), typeof(B), new MySerializerContext()));
    
    [JsonSerializable(typeof(B))]
    public partial class MySerializerContext : JsonSerializerContext
    {
    }
    
    public class B : A
    {
        [JsonIgnore]
        public new int I => 0;
    }
    
    public abstract class A
    {
        [JsonIgnore]
        public int I { get; }
    }

    CSC : warning CS8785: Generator 'JsonSourceGenerator' failed to generate source. It will not contribute to the output and compilation errors may occur as a result. Exception was of type 'ArgumentException' with message 'An item with the same key has already been added. Key: I'

  3. added and removed
    untriaged
    on Sep 13, 2021
  4. added this to the 6.0.0 milestone on Sep 13, 2021
  5. eiriktsarpalis commented on Sep 13, 2021

    @eiriktsarpalis
    Member

    I can reproduce both with the RC2 SDK. The fact that JsonIgnore is not able to suppress the error is worrying -- we should fix this for 6.0.

  6. self-assigned this
    on Sep 13, 2021
  7. steveharter commented on Sep 27, 2021

    @steveharter
    Contributor

    For the latter case from @am11 it looks like normal serialization also fails:

      // This throws System.ArgumentException:An item with the same key has already been added. Key: Ignored_New_Property
      string json = JsonSerializer.Serialize(new ConcreteDerivedClass());
    
      public class ConcreteDerivedClass : AbstractBaseClass
      {
          [JsonIgnore]
          public new int Ignored_New_Property { get; set; }
      }
      
      public abstract class AbstractBaseClass
      {
          [JsonIgnore]
          public int Ignored_New_Property { get; set; }
      }

    So I created #59675 to address that separately.

  8. ghost added
    in-pr
    on Sep 28, 2021
  9. ghost removed
    in-pr
    on Sep 29, 2021
  10. ghost locked as resolved and limited conversation to collaborators on Nov 3, 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