Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -540,7 +540,9 @@ public static void AddJsonExceptionInformation(scoped ref ReadStack state, in Ut
if (string.IsNullOrEmpty(message))
{
// Use a default message.
Type propertyType = state.Current.JsonPropertyInfo?.PropertyType ?? state.Current.JsonTypeInfo.Type;
Type propertyType = state.Current.JsonPropertyInfo?.PropertyType ??
state.Current.CtorArgumentState?.JsonParameterInfo?.ParameterType ??
state.Current.JsonTypeInfo.Type;
message = SR.Format(SR.DeserializeUnableToConvertValue, propertyType);
ex.AppendPathInformation = true;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -350,5 +350,31 @@ public ClassWithInvalidDictionary(Dictionary<string, int[,]> unsupportedDictiona
UnsupportedDictionary = unsupportedDictionary;
}
}

[Fact]
public async Task ParameterizedConstructor_ExceptionMessage_ReportsPropertyType()
{
// Regression test: classes with parameterized constructors should report the
// property type (System.String), not the declaring class type, in the JsonException message.
// https://github.com/dotnet/runtime/pull/126575
JsonException e;

e = await Assert.ThrowsAsync<JsonException>(() =>
Serializer.DeserializeWrapper<ParameterizedClass_WithStringProperty>("""{"Text":{}}"""));
Assert.Contains("System.String", e.Message);
Assert.DoesNotContain(nameof(ParameterizedClass_WithStringProperty), e.Message);

e = await Assert.ThrowsAsync<JsonException>(() =>
Serializer.DeserializeWrapper<ParameterizedRecord_WithStringProperty>("""{"Text":{}}"""));
Assert.Contains("System.String", e.Message);
Assert.DoesNotContain(nameof(ParameterizedRecord_WithStringProperty), e.Message);
}

public class ParameterizedClass_WithStringProperty(string text)
{
public string Text { get; set; } = text;
}

public record ParameterizedRecord_WithStringProperty(string Text);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,8 @@ protected ConstructorTests_Metadata(JsonSerializerWrapper stringWrapper)
[JsonSerializable(typeof(TypeWith_RefReadonlyParameter_Primitive))]
[JsonSerializable(typeof(TypeWith_RefReadonlyParameter_Struct))]
[JsonSerializable(typeof(TypeWith_RefReadonlyParameter_ReferenceType))]
[JsonSerializable(typeof(ParameterizedClass_WithStringProperty))]
[JsonSerializable(typeof(ParameterizedRecord_WithStringProperty))]
internal sealed partial class ConstructorTestsContext_Metadata : JsonSerializerContext
{
}
Expand Down Expand Up @@ -349,6 +351,8 @@ public ConstructorTests_Default(JsonSerializerWrapper jsonSerializer) : base(jso
[JsonSerializable(typeof(TypeWith_RefReadonlyParameter_Primitive))]
[JsonSerializable(typeof(TypeWith_RefReadonlyParameter_Struct))]
[JsonSerializable(typeof(TypeWith_RefReadonlyParameter_ReferenceType))]
[JsonSerializable(typeof(ParameterizedClass_WithStringProperty))]
[JsonSerializable(typeof(ParameterizedRecord_WithStringProperty))]
internal sealed partial class ConstructorTestsContext_Default : JsonSerializerContext
{
}
Expand Down
Loading