Skip to content

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

Closed

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

Activity

  1. ghost added
    untriaged
    on Jul 11, 2023
  2. added and removed
    untriaged
    on Jul 11, 2023
  3. added this to the 8.0.0 milestone on Jul 11, 2023
  4. eiriktsarpalis commented on Jul 11, 2023

    @eiriktsarpalis
    Member

    Ah yes, the JsonConverterAttribute.CreateConverter method was never supported by the source generator. I think it would make sense to at least handle that case gracefully and emit a diagnostic.

  5. Khitiara commented on Jul 11, 2023

    @Khitiara
    Author

    Plus, JsonConverterFactory covers most if not all of the bases youd want JsonConverterAttribute.CreateConverter for anyway

  6. added
    in-pr
    on Jul 19, 2023
  7. ghost removed
    in-pr
    on Jul 21, 2023
  8. ghost locked as resolved and limited conversation to collaborators on Aug 20, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions