Skip to content

Commit

Permalink
Ensure the correct typeToConvert parameter is being passed to const…
Browse files Browse the repository at this point in the history
…ructor parameter converters. (#87111)
  • Loading branch information
eiriktsarpalis committed Jun 4, 2023
1 parent da1da02 commit 48ce1d9
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 2 deletions.
Expand Up @@ -17,9 +17,8 @@ internal class LargeObjectWithParameterizedConstructorConverter<T> : ObjectWithP
protected sealed override bool ReadAndCacheConstructorArgument(scoped ref ReadStack state, ref Utf8JsonReader reader, JsonParameterInfo jsonParameterInfo)
{
Debug.Assert(jsonParameterInfo.ShouldDeserialize);
Debug.Assert(jsonParameterInfo.Options != null);

bool success = jsonParameterInfo.EffectiveConverter.TryReadAsObject(ref reader, TypeToConvert, jsonParameterInfo.Options!, ref state, out object? arg);
bool success = jsonParameterInfo.EffectiveConverter.TryReadAsObject(ref reader, jsonParameterInfo.ParameterType, jsonParameterInfo.Options, ref state, out object? arg);

if (success && !(arg == null && jsonParameterInfo.IgnoreNullTokensOnRead))
{
Expand Down
Expand Up @@ -1602,5 +1602,32 @@ public class ClassWithIgnoredPropertyDefaultParam

public ClassWithIgnoredPropertyDefaultParam(int x, int y = 5) => (X, Y) = (x, y);
}

[Fact]
public async Task TestClassWithCustomConverterOnCtorParameter_ShouldPassCorrectTypeToConvertParameter()
{
ClassWithCustomConverterOnCtorParameter result = await Serializer.DeserializeWrapper<ClassWithCustomConverterOnCtorParameter>("""{"Id":"id"}""");
Assert.Equal("id", result.Id);
}

public class ClassWithCustomConverterOnCtorParameter
{
public ClassWithCustomConverterOnCtorParameter(string id) => Id = id;

[JsonConverter(typeof(CustomCtorParameterConverter))]
public string Id { get; }
}

public class CustomCtorParameterConverter : JsonConverter<string>
{
public override string? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
Assert.Equal(typeof(string), typeToConvert);
return reader.GetString();
}

public override void Write(Utf8JsonWriter writer, string value, JsonSerializerOptions options)
=> writer.WriteStringValue(value);
}
}
}
Expand Up @@ -149,6 +149,7 @@ protected ConstructorTests_Metadata(JsonSerializerWrapper stringWrapper)
[JsonSerializable(typeof(ClassWithInvalidDictionary))]
[JsonSerializable(typeof(TypeWithEnumParameters))]
[JsonSerializable(typeof(ClassWithIgnoredPropertyDefaultParam))]
[JsonSerializable(typeof(ClassWithCustomConverterOnCtorParameter))]
internal sealed partial class ConstructorTestsContext_Metadata : JsonSerializerContext
{
}
Expand Down Expand Up @@ -293,6 +294,7 @@ public ConstructorTests_Default(JsonSerializerWrapper jsonSerializer) : base(jso
[JsonSerializable(typeof(ClassWithInvalidDictionary))]
[JsonSerializable(typeof(TypeWithEnumParameters))]
[JsonSerializable(typeof(ClassWithIgnoredPropertyDefaultParam))]
[JsonSerializable(typeof(ClassWithCustomConverterOnCtorParameter))]
internal sealed partial class ConstructorTestsContext_Default : JsonSerializerContext
{
}
Expand Down

0 comments on commit 48ce1d9

Please sign in to comment.