Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix nested types nullable context check #3043

Merged
merged 4 commits into from
Aug 26, 2024
Merged
Show file tree
Hide file tree
Changes from 3 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 @@ -136,7 +136,7 @@ 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 }
? GetDeclaringTypeChain(memberInfo)
: new Type[] { memberInfo.DeclaringType };
VladimirTyrin marked this conversation as resolved.
Show resolved Hide resolved

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

return false;
}

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

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

return chain.ToArray();
}
}
}
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
}