Skip to content

Json source generator does not produce a valid diagnostic for JsonConverterAttribute subtype with no constructor parameters #88638

Description

@Khitiara

Description

If using a subtype of JsonConverterAttribute which overrides the CreateConverter method but has no constructor parameters, JsonSourceGenerator.Parser.cs#L1234 produces an IndexOutOfRangeException instead of failing with an error diagnostic, which leads to errors involving unimplemented abstracts rather than expected information on the actual source of the error.

Reproduction Steps

public class MyJsonConverterAttribute : JsonConverterAttribute {
    public override JsonConverter? CreateConverter(Type typeToConvert) => null; // actual converter logic would go here
}

public class MyType {
    [MyJsonConverter]
    public int Foo { get; init; }
}

[JsonSerializable(typeof(MyType))]
public partial class MyJsonContext : JsonSerializerContext;

Expected behavior

An error diagnostic on the attribute usage.

Actual behavior

Several unimplemented abstract method errors on the context class declaration and no generated code.

Regression?

No response

Known Workarounds

Using a JsonConverterFactory implementation with the default JsonConverterAttribute sidesteps the issue neatly while also just working rather than being a compiler error, but with third party libraries that may not be easy or possible.

Configuration

.net 8 preview; I do not believe it to be specific to that configuration.

Other information

Related issue: nodatime.serialization#97

Metadata

Metadata

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions