Skip to content

Commit d72e43e

Browse files
committed
Fixed: do not share NullabilityInfoContext, it is not thread-safe
1 parent d7c8b4b commit d72e43e

File tree

3 files changed

+8
-10
lines changed

3 files changed

+8
-10
lines changed

src/JsonApiDotNetCore.OpenApi/ResourceFieldAttributeExtensions.cs

+2-3
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,6 @@ namespace JsonApiDotNetCore.OpenApi;
77

88
internal static class ResourceFieldAttributeExtensions
99
{
10-
private static readonly NullabilityInfoContext NullabilityInfoContext = new();
11-
1210
public static bool IsNullable(this ResourceFieldAttribute source)
1311
{
1412
bool hasRequiredAttribute = source.Property.HasAttribute<RequiredAttribute>();
@@ -24,7 +22,8 @@ public static bool IsNullable(this ResourceFieldAttribute source)
2422
return false;
2523
}
2624

27-
NullabilityInfo nullabilityInfo = NullabilityInfoContext.Create(source.Property);
25+
NullabilityInfoContext nullabilityContext = new();
26+
NullabilityInfo nullabilityInfo = nullabilityContext.Create(source.Property);
2827

2928
// Reflects the following cases:
3029
// Independent of NRT:

src/JsonApiDotNetCore.OpenApi/SwaggerComponents/ResourceFieldObjectSchemaBuilder.cs

+2-3
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,6 @@ namespace JsonApiDotNetCore.OpenApi.SwaggerComponents;
1212

1313
internal sealed class ResourceFieldObjectSchemaBuilder
1414
{
15-
private static readonly NullabilityInfoContext NullabilityInfoContext = new();
16-
1715
private static readonly Type[] RelationshipSchemaInResponseOpenTypes =
1816
{
1917
typeof(ToOneRelationshipInResponse<>),
@@ -121,7 +119,8 @@ private bool IsFieldRequired(ResourceFieldAttribute field)
121119

122120
bool hasRequiredAttribute = field.Property.HasAttribute<RequiredAttribute>();
123121

124-
NullabilityInfo nullabilityInfo = NullabilityInfoContext.Create(field.Property);
122+
NullabilityInfoContext nullabilityContext = new();
123+
NullabilityInfo nullabilityInfo = nullabilityContext.Create(field.Property);
125124

126125
return field.Property.PropertyType.IsValueType switch
127126
{

test/OpenApiClientTests/PropertyInfoAssertionsExtension.cs

+4-4
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,13 @@ namespace OpenApiClientTests;
66

77
internal static class PropertyInfoAssertionsExtensions
88
{
9-
private static readonly NullabilityInfoContext NullabilityInfoContext = new();
10-
119
[CustomAssertion]
1210
public static void BeNullable(this PropertyInfoAssertions source, string because = "", params object[] becauseArgs)
1311
{
1412
PropertyInfo propertyInfo = source.Subject;
1513

16-
NullabilityInfo nullabilityInfo = NullabilityInfoContext.Create(propertyInfo);
14+
NullabilityInfoContext nullabilityContext = new();
15+
NullabilityInfo nullabilityInfo = nullabilityContext.Create(propertyInfo);
1716

1817
nullabilityInfo.ReadState.Should().NotBe(NullabilityState.NotNull, because, becauseArgs);
1918
}
@@ -23,7 +22,8 @@ public static void BeNonNullable(this PropertyInfoAssertions source, string beca
2322
{
2423
PropertyInfo propertyInfo = source.Subject;
2524

26-
NullabilityInfo nullabilityInfo = NullabilityInfoContext.Create(propertyInfo);
25+
NullabilityInfoContext nullabilityContext = new();
26+
NullabilityInfo nullabilityInfo = nullabilityContext.Create(propertyInfo);
2727

2828
nullabilityInfo.ReadState.Should().Be(NullabilityState.NotNull, because, becauseArgs);
2929
}

0 commit comments

Comments
 (0)