Skip to content

JsonSourceGenerator - ArgumentException key already added. #58985

@fbrosseau

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

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions