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

Use NullabilityInfoContext to determine if member is nullable #3046

Merged
Merged
Changes from 1 commit
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 @@ -8,11 +8,13 @@ namespace Swashbuckle.AspNetCore.SwaggerGen
{
public static class MemberInfoExtensions
{
#if !NET6_0_OR_GREATER
private const string NullableAttributeFullTypeName = "System.Runtime.CompilerServices.NullableAttribute";
private const string NullableFlagsFieldName = "NullableFlags";
private const string NullableContextAttributeFullTypeName = "System.Runtime.CompilerServices.NullableContextAttribute";
private const string FlagFieldName = "Flag";
private const int NotAnnotated = 1; // See https://github.com/dotnet/roslyn/blob/af7b0ebe2b0ed5c335a928626c25620566372dd1/docs/features/nullable-metadata.md?plain=1#L40
#endif

public static IEnumerable<object> GetInlineAndMetadataAttributes(this MemberInfo memberInfo)
{
Expand All @@ -34,8 +36,27 @@ public static IEnumerable<object> GetInlineAndMetadataAttributes(this MemberInfo
return attributes;
}

#if NET6_0_OR_GREATER
private static NullabilityInfo GetNullabilityInfo(this MemberInfo memberInfo)
{
var context = new NullabilityInfoContext();

return memberInfo switch
{
FieldInfo fieldInfo => context.Create(fieldInfo),
PropertyInfo propertyInfo => context.Create(propertyInfo),
EventInfo eventInfo => context.Create(eventInfo),
_ => throw new InvalidOperationException($"MemberInfo type {memberInfo.MemberType} is not supported.")
};
}
#endif

public static bool IsNonNullableReferenceType(this MemberInfo memberInfo)
{
#if NET6_0_OR_GREATER
var nullableInfo = GetNullabilityInfo(memberInfo);
return nullableInfo.ReadState == NullabilityState.NotNull;
#else
var memberType = memberInfo.MemberType == MemberTypes.Field
? ((FieldInfo)memberInfo).FieldType
: ((PropertyInfo)memberInfo).PropertyType;
Expand All @@ -57,16 +78,13 @@ public static bool IsNonNullableReferenceType(this MemberInfo memberInfo)
}

return false;
#endif
}

public static bool IsDictionaryValueNonNullable(this MemberInfo memberInfo)
{
#if NET6_0_OR_GREATER
var context = new NullabilityInfoContext();
var nullableInfo = memberInfo.MemberType == MemberTypes.Field
? context.Create((FieldInfo)memberInfo)
: context.Create((PropertyInfo)memberInfo);

var nullableInfo = GetNullabilityInfo(memberInfo);
if (nullableInfo.GenericTypeArguments.Length != 2)
{
var length = nullableInfo.GenericTypeArguments.Length;
Expand Down Expand Up @@ -124,6 +142,8 @@ public static bool IsDictionaryValueNonNullable(this MemberInfo memberInfo)
#endif
}


#if !NET6_0_OR_GREATER
private static object GetNullableAttribute(this MemberInfo memberInfo)
{
var nullableAttribute = memberInfo
Expand Down Expand Up @@ -162,5 +182,6 @@ private static bool GetNullableFallbackValue(this MemberInfo memberInfo)

return false;
}
#endif
}
}