Skip to content

Commit

Permalink
Merge branch 'master' into nullabilityinfo-member
Browse files Browse the repository at this point in the history
  • Loading branch information
martincostello authored Aug 26, 2024
2 parents 9fb2765 + f91f560 commit ec62df7
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,6 @@ public static bool IsDictionaryValueNonNullable(this MemberInfo memberInfo)
#endif
}


#if !NET6_0_OR_GREATER
private static object GetNullableAttribute(this MemberInfo memberInfo)
{
Expand All @@ -156,8 +155,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 @@ -183,5 +182,19 @@ private static bool GetNullableFallbackValue(this MemberInfo memberInfo)
return false;
}
#endif

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 ec62df7

Please sign in to comment.