Assert.Contains("Info2", exception.Message);
}
+ [Fact]
+ public async Task InheritedPersonWithRequiredMembersWorksAsExpected()
+ {
+ var options = new JsonSerializerOptions(Serializer.DefaultOptions);
+ options.MakeReadOnly();
+
+ JsonTypeInfo typeInfo = options.GetTypeInfo(typeof(InheritedPersonWithRequiredMembers));
+ Assert.Equal(3, typeInfo.Properties.Count);
+
+ AssertJsonTypeInfoHasRequiredProperties(GetTypeInfo<InheritedPersonWithRequiredMembers>(options),
+ nameof(InheritedPersonWithRequiredMembers.FirstName),
+ nameof(InheritedPersonWithRequiredMembers.LastName));
+
+ JsonException exception = await Assert.ThrowsAsync<JsonException>(() => Serializer.DeserializeWrapper("{}", typeInfo));
+ Assert.Contains("FirstName", exception.Message);
+ Assert.Contains("LastName", exception.Message);
+ Assert.DoesNotContain("MiddleName", exception.Message);
+ }
+
+ [Fact]
+ public async Task InheritedPersonWithRequiredMembersWithAdditionalRequiredMembersWorksAsExpected()
+ {
+ var options = new JsonSerializerOptions(Serializer.DefaultOptions);
+ options.MakeReadOnly();
+
+ JsonTypeInfo typeInfo = options.GetTypeInfo(typeof(InheritedPersonWithRequiredMembersWithAdditionalRequiredMembers));
+ Assert.Equal(4, typeInfo.Properties.Count);
+
+ AssertJsonTypeInfoHasRequiredProperties(GetTypeInfo<InheritedPersonWithRequiredMembersWithAdditionalRequiredMembers>(options),
+ nameof(InheritedPersonWithRequiredMembersWithAdditionalRequiredMembers.FirstName),
+ nameof(InheritedPersonWithRequiredMembersWithAdditionalRequiredMembers.LastName),
+ nameof(InheritedPersonWithRequiredMembersWithAdditionalRequiredMembers.Post));
+
+ JsonException exception = await Assert.ThrowsAsync<JsonException>(() => Serializer.DeserializeWrapper("{}", typeInfo));
+ Assert.Contains("FirstName", exception.Message);
+ Assert.Contains("LastName", exception.Message);
+ Assert.Contains("Post", exception.Message);
+ Assert.DoesNotContain("MiddleName", exception.Message);
+ }
+
+ [Theory]
+ [MemberData(nameof(InheritedPersonWithRequiredMembersSetsRequiredMembersWorksAsExpectedSources))]
+ public async Task InheritedPersonWithRequiredMembersSetsRequiredMembersWorksAsExpected(string jsonValue,
+ InheritedPersonWithRequiredMembersSetsRequiredMembers expectedValue)
+ {
+ var options = new JsonSerializerOptions(Serializer.DefaultOptions);
+ options.MakeReadOnly();
+
+ JsonTypeInfo typeInfo = options.GetTypeInfo(typeof(InheritedPersonWithRequiredMembersSetsRequiredMembers));
+ Assert.Equal(3, typeInfo.Properties.Count);
+
+ AssertJsonTypeInfoHasRequiredProperties(GetTypeInfo<InheritedPersonWithRequiredMembersSetsRequiredMembers>(options));
+
+ InheritedPersonWithRequiredMembersSetsRequiredMembers actualValue =
+ await Serializer.DeserializeWrapper<InheritedPersonWithRequiredMembersSetsRequiredMembers>(jsonValue, options);
+ Assert.Equal(expectedValue.FirstName, actualValue.FirstName);
+ Assert.Equal(expectedValue.LastName, actualValue.LastName);
+ Assert.Equal(expectedValue.MiddleName, actualValue.MiddleName);
+ }
+
+ public class InheritedPersonWithRequiredMembers : PersonWithRequiredMembers
+ {
+ }
+
+ public class InheritedPersonWithRequiredMembersWithAdditionalRequiredMembers : PersonWithRequiredMembers
+ {
+ public required string Post { get; set; }
+ }
+
+ public class InheritedPersonWithRequiredMembersSetsRequiredMembers : PersonWithRequiredMembers
+ {
+ [SetsRequiredMembers]
+ public InheritedPersonWithRequiredMembersSetsRequiredMembers()
+ {
+ FirstName = "FirstNameValueFromConstructor";
+ LastName = "LastNameValueFromConstructor";
+ MiddleName = "MiddleNameValueFromConstructor";
+ }
+ }
+
public class PersonWithRequiredMembersAndSmallParametrizedCtor
{
public required string FirstName { get; set; }
public required int PropertyWithInitOnlySetter { get; init; }
}
+ public static IEnumerable<object[]> InheritedPersonWithRequiredMembersSetsRequiredMembersWorksAsExpectedSources()
+ {
+ yield return new object[]
+ {
+ "{}",
+ new InheritedPersonWithRequiredMembersSetsRequiredMembers()
+ };
+ yield return new object[]
+ {
+ """{"FirstName": "FirstNameFromJson"}""",
+ new InheritedPersonWithRequiredMembersSetsRequiredMembers
+ {
+ FirstName = "FirstNameFromJson"
+ }
+ };
+ }
+
private static JsonTypeInfo GetTypeInfo<T>(JsonSerializerOptions options)
{
options.TypeInfoResolver ??= JsonSerializerOptions.Default.TypeInfoResolver;