Skip to content

Commit

Permalink
Fix nested types nullable context check (#3043)
Browse files Browse the repository at this point in the history
Fix nested types nullable context check.
  • Loading branch information
VladimirTyrin committed Aug 26, 2024
1 parent 16e2a94 commit f91f560
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -136,8 +136,8 @@ private static object GetNullableAttribute(this MemberInfo memberInfo)
private static bool GetNullableFallbackValue(this MemberInfo memberInfo)
{
var declaringTypes = memberInfo.DeclaringType.IsNested
? new Type[] { memberInfo.DeclaringType, memberInfo.DeclaringType.DeclaringType }
: new Type[] { memberInfo.DeclaringType };
? GetDeclaringTypeChain(memberInfo)
: new List<Type>(1) { memberInfo.DeclaringType };

foreach (var declaringType in declaringTypes)
{
Expand All @@ -162,5 +162,19 @@ private static bool GetNullableFallbackValue(this MemberInfo memberInfo)

return false;
}

private static List<Type> GetDeclaringTypeChain(MemberInfo memberInfo)
{
var chain = new List<Type>();
var currentType = memberInfo.DeclaringType;

while (currentType != null)
{
chain.Add(currentType);
currentType = currentType.DeclaringType;
}

return chain;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -981,6 +981,28 @@ public void GenerateSchema_SupportsOption_SuppressImplicitRequiredAttributeForNo
Assert.Equal(!suppress, propertyIsRequired);
}

[Theory]
[InlineData(typeof(TypeWithNullableContextAnnotated), nameof(TypeWithNullableContextAnnotated.SubTypeWithNestedSubType.Nested), nameof(TypeWithNullableContextAnnotated.SubTypeWithNestedSubType.Nested.NullableString), true)]
[InlineData(typeof(TypeWithNullableContextAnnotated), nameof(TypeWithNullableContextAnnotated.SubTypeWithNestedSubType.Nested), nameof(TypeWithNullableContextAnnotated.SubTypeWithNestedSubType.Nested.NonNullableString), false)]
[InlineData(typeof(TypeWithNullableContextNotAnnotated), nameof(TypeWithNullableContextNotAnnotated.SubTypeWithNestedSubType.Nested), nameof(TypeWithNullableContextAnnotated.SubTypeWithNestedSubType.Nested.NullableString), true)]
[InlineData(typeof(TypeWithNullableContextNotAnnotated), nameof(TypeWithNullableContextNotAnnotated.SubTypeWithNestedSubType.Nested), nameof(TypeWithNullableContextAnnotated.SubTypeWithNestedSubType.Nested.NonNullableString), false)]
public void GenerateSchema_SupportsOption_SupportNonNullableReferenceTypes_NestedWithinNested(
Type declaringType,
string subType,
string propertyName,
bool expectedNullable)
{
var subject = Subject(
configureGenerator: c => c.SupportNonNullableReferenceTypes = true
);
var schemaRepository = new SchemaRepository();

subject.GenerateSchema(declaringType, schemaRepository);

var propertySchema = schemaRepository.Schemas[subType].Properties[propertyName];
Assert.Equal(expectedNullable, propertySchema.Nullable);
}

[Theory]
[InlineData(typeof(TypeWithNullableContextAnnotated))]
[InlineData(typeof(TypeWithNullableContextNotAnnotated))]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ public class TypeWithNullableContextNotAnnotated

public List<SubTypeWithOneNullableContent>? NullableList { get; set; }
public List<SubTypeWithOneNonNullableContent> NonNullableList { get; set; } = default!;
public SubTypeWithNestedSubType NestedWithNested { get; set; } = default!;

public Dictionary<string, string>? NullableDictionaryInNonNullableContent { get; set; }
public Dictionary<string, string> NonNullableDictionaryInNonNullableContent { get; set; } = default!;
Expand Down Expand Up @@ -88,6 +89,17 @@ public class SubTypeWithOneNonNullableContent
{
public string NonNullableString { get; set; } = default!;
}

public class SubTypeWithNestedSubType
{
public Nested NestedProperty { get; set; } = default!;

public class Nested
{
public string? NullableString { get; set; }
public string NonNullableString { get; set; } = default!;
}
}
}

/// <summary>
Expand Down Expand Up @@ -128,6 +140,7 @@ public class TypeWithNullableContextAnnotated

public List<SubTypeWithOneNullableContent>? NullableList { get; set; }
public List<SubTypeWithOneNonNullableContent> NonNullableList { get; set; } = default!;
public SubTypeWithNestedSubType NestedWithNested { get; set; } = default!;

public Dictionary<string, string>? NullableDictionaryInNonNullableContent { get; set; }
public Dictionary<string, string> NonNullableDictionaryInNonNullableContent { get; set; } = default!;
Expand Down Expand Up @@ -168,6 +181,17 @@ public class SubTypeWithOneNonNullableContent
{
public string NonNullableString { get; set; } = default!;
}

public class SubTypeWithNestedSubType
{
public Nested NestedProperty { get; set; } = default!;

public class Nested
{
public string? NullableString { get; set; }
public string NonNullableString { get; set; } = default!;
}
}
}
#nullable restore
}

0 comments on commit f91f560

Please sign in to comment.