Skip to content

Commit 0cc59db

Browse files
committed
Use NullabilityInfoContext for nullability information
1 parent a3fd9bc commit 0cc59db

File tree

6 files changed

+22
-63
lines changed

6 files changed

+22
-63
lines changed

src/JsonApiDotNetCore.OpenApi/JsonApiObjects/SingleData.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@ namespace JsonApiDotNetCore.OpenApi.JsonApiObjects;
77

88
[UsedImplicitly(ImplicitUseTargetFlags.Members)]
99
internal abstract class SingleData<TData>
10-
where TData : ResourceIdentifierObject
10+
// ReSharper disable once RedundantNotNullConstraint
11+
// See ReSharper bug: https://youtrack.jetbrains.com/issue/RSRP-489137/False-positive-on-RedundantNotNullConstraint
12+
where TData : notnull, ResourceIdentifierObject
1113
{
1214
[Required]
1315
[JsonPropertyName("data")]

src/JsonApiDotNetCore.OpenApi/MemberInfoExtensions.cs

Lines changed: 0 additions & 36 deletions
This file was deleted.
Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,20 @@
11
using System.ComponentModel.DataAnnotations;
2+
using System.Reflection;
23
using JsonApiDotNetCore.Resources.Annotations;
34
using Swashbuckle.AspNetCore.SwaggerGen;
45

56
namespace JsonApiDotNetCore.OpenApi;
67

78
internal static class ResourceFieldAttributeExtensions
89
{
10+
private static readonly NullabilityInfoContext NullabilityInfoContext = new();
11+
912
public static bool IsNullable(this ResourceFieldAttribute source)
1013
{
11-
TypeCategory fieldTypeCategory = source.Property.GetTypeCategory();
1214
bool hasRequiredAttribute = source.Property.HasAttribute<RequiredAttribute>();
1315

14-
return fieldTypeCategory switch
15-
{
16-
TypeCategory.NonNullableReferenceType or TypeCategory.ValueType => false,
17-
TypeCategory.NullableReferenceType or TypeCategory.NullableValueType => !hasRequiredAttribute,
18-
_ => throw new UnreachableCodeException()
19-
};
16+
NullabilityInfo nullabilityInfo = NullabilityInfoContext.Create(source.Property);
17+
18+
return nullabilityInfo.ReadState == NullabilityState.Nullable && !hasRequiredAttribute;
2019
}
2120
}

src/JsonApiDotNetCore.OpenApi/SwaggerComponents/JsonApiSchemaGenerator.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ namespace JsonApiDotNetCore.OpenApi.SwaggerComponents;
1010

1111
internal sealed class JsonApiSchemaGenerator : ISchemaGenerator
1212
{
13+
private static readonly NullabilityInfoContext NullabilityInfoContext = new();
14+
1315
private static readonly Type[] JsonApiDocumentOpenTypes =
1416
{
1517
typeof(ResourceCollectionResponseDocument<>),
@@ -107,9 +109,8 @@ private static bool IsDataPropertyNullable(Type type)
107109
throw new UnreachableCodeException();
108110
}
109111

110-
TypeCategory typeCategory = dataProperty.GetTypeCategory();
111-
112-
return typeCategory == TypeCategory.NullableReferenceType;
112+
NullabilityInfo nullabilityInfo = NullabilityInfoContext.Create(dataProperty);
113+
return nullabilityInfo.ReadState == NullabilityState.Nullable;
113114
}
114115

115116
private void SetDataObjectSchemaToNullable(OpenApiSchema referenceSchemaForDocument)

src/JsonApiDotNetCore.OpenApi/SwaggerComponents/ResourceFieldObjectSchemaBuilder.cs

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ namespace JsonApiDotNetCore.OpenApi.SwaggerComponents;
1212

1313
internal sealed class ResourceFieldObjectSchemaBuilder
1414
{
15+
private static readonly NullabilityInfoContext NullabilityInfoContext = new();
16+
1517
private static readonly Type[] RelationshipInResponseOpenTypes =
1618
{
1719
typeof(ToOneRelationshipInResponse<>),
@@ -108,15 +110,14 @@ private bool IsFieldRequired(ResourceFieldAttribute field)
108110
return false;
109111
}
110112

111-
TypeCategory fieldTypeCategory = field.Property.GetTypeCategory();
112113
bool hasRequiredAttribute = field.Property.HasAttribute<RequiredAttribute>();
113114

114-
return fieldTypeCategory switch
115+
NullabilityInfo nullabilityInfo = NullabilityInfoContext.Create(field.Property);
116+
117+
return field.Property.PropertyType.IsValueType switch
115118
{
116-
TypeCategory.NonNullableReferenceType => true,
117-
TypeCategory.ValueType => hasRequiredAttribute,
118-
TypeCategory.NullableReferenceType or TypeCategory.NullableValueType => hasRequiredAttribute,
119-
_ => throw new UnreachableCodeException()
119+
true => hasRequiredAttribute,
120+
false => nullabilityInfo.ReadState == NullabilityState.NotNull || hasRequiredAttribute
120121
};
121122
}
122123

@@ -224,6 +225,7 @@ private static bool IsDataPropertyNullable(Type type)
224225
throw new UnreachableCodeException();
225226
}
226227

227-
return dataProperty.GetTypeCategory() == TypeCategory.NullableReferenceType;
228+
NullabilityInfo nullabilityInfo = NullabilityInfoContext.Create(dataProperty);
229+
return nullabilityInfo.ReadState == NullabilityState.Nullable;
228230
}
229231
}

src/JsonApiDotNetCore.OpenApi/TypeCategory.cs

Lines changed: 0 additions & 9 deletions
This file was deleted.

0 commit comments

Comments
 (0)