Skip to content

Commit 3e9708a

Browse files
committed
Remove overlooked code duplication in OpenApiTests, revert reflection caching in object extension
1 parent 63ba43d commit 3e9708a

File tree

9 files changed

+211
-812
lines changed

9 files changed

+211
-812
lines changed
+6-22
Original file line numberDiff line numberDiff line change
@@ -1,53 +1,37 @@
1-
using System.Collections.Concurrent;
21
using System.Reflection;
32
using JsonApiDotNetCore.OpenApi.Client;
43

54
namespace OpenApiClientTests;
65

76
internal static class ObjectExtensions
87
{
9-
private static readonly ConcurrentDictionary<string, PropertyInfo> PropertyInfoCache = new();
10-
118
public static object? GetPropertyValue(this object source, string propertyName)
129
{
1310
ArgumentGuard.NotNull(source);
1411
ArgumentGuard.NotNull(propertyName);
1512

16-
string cacheKey = EnsurePropertyInfoIsCached(source, propertyName);
17-
18-
return PropertyInfoCache[cacheKey].GetValue(source);
19-
}
20-
21-
private static string EnsurePropertyInfoIsCached(object source, string propertyName)
22-
{
23-
string cacheKey = $"{source.GetType().FullName}-{propertyName}";
24-
25-
if (!PropertyInfoCache.ContainsKey(cacheKey))
26-
{
27-
PropertyInfo propertyInfo = source.GetType().GetProperties().Single(attribute => attribute.Name == propertyName);
28-
PropertyInfoCache[cacheKey] = propertyInfo;
29-
}
13+
PropertyInfo propertyInfo = source.GetType().GetProperties().Single(property => property.Name == propertyName);
3014

31-
return cacheKey;
15+
return propertyInfo.GetValue(source);
3216
}
3317

3418
public static void SetPropertyValue(this object source, string propertyName, object? value)
3519
{
3620
ArgumentGuard.NotNull(source);
3721
ArgumentGuard.NotNull(propertyName);
3822

39-
string cacheKey = EnsurePropertyInfoIsCached(source, propertyName);
23+
PropertyInfo propertyInfo = source.GetType().GetProperties().Single(property => property.Name == propertyName);
4024

41-
PropertyInfoCache[cacheKey].SetValue(source, value);
25+
propertyInfo.SetValue(source, value);
4226
}
4327

4428
public static object? GetDefaultValueForProperty(this object source, string propertyName)
4529
{
4630
ArgumentGuard.NotNull(source);
4731
ArgumentGuard.NotNull(propertyName);
4832

49-
string cacheKey = EnsurePropertyInfoIsCached(source, propertyName);
33+
PropertyInfo propertyInfo = source.GetType().GetProperties().Single(property => property.Name == propertyName);
5034

51-
return Activator.CreateInstance(PropertyInfoCache[cacheKey].PropertyType);
35+
return Activator.CreateInstance(propertyInfo.PropertyType);
5236
}
5337
}

test/OpenApiTests/ResourceFieldValidation/NullableReferenceTypesDisabled/ModelStateValidationDisabled/NullabilityTests.cs

+22-108
Original file line numberDiff line numberDiff line change
@@ -20,160 +20,74 @@ public NullabilityTests(
2020
testContext.SwaggerDocumentOutputPath = "test/OpenApiClientTests/ResourceFieldValidation/NullableReferenceTypesDisabled/ModelStateValidationDisabled";
2121
}
2222

23-
[Fact]
24-
public async Task Schema_property_for_reference_type_attribute_is_nullable()
23+
[Theory]
24+
[InlineData("referenceType")]
25+
[InlineData("requiredReferenceType")]
26+
[InlineData("nullableValueType")]
27+
[InlineData("requiredNullableValueType")]
28+
public async Task Schema_property_for_attribute_is_nullable(string jsonPropertyName)
2529
{
2630
// Act
2731
JsonElement document = await _testContext.GetSwaggerDocumentAsync();
2832

2933
// Assert
3034
document.ShouldContainPath("components.schemas.resourceAttributesInResponse.properties").With(schemaProperties =>
3135
{
32-
schemaProperties.ShouldContainPath("referenceType").With(schemaProperty =>
36+
schemaProperties.ShouldContainPath(jsonPropertyName).With(schemaProperty =>
3337
{
3438
schemaProperty.ShouldContainPath("nullable").With(nullableProperty => nullableProperty.ValueKind.Should().Be(JsonValueKind.True));
3539
});
3640
});
3741
}
3842

39-
[Fact]
40-
public async Task Schema_property_for_required_reference_type_attribute_is_nullable()
43+
[Theory]
44+
[InlineData("valueType")]
45+
[InlineData("requiredValueType")]
46+
public async Task Schema_property_for_attribute_is_not_nullable(string jsonPropertyName)
4147
{
4248
// Act
4349
JsonElement document = await _testContext.GetSwaggerDocumentAsync();
4450

4551
// Assert
4652
document.ShouldContainPath("components.schemas.resourceAttributesInResponse.properties").With(schemaProperties =>
4753
{
48-
schemaProperties.ShouldContainPath("requiredReferenceType").With(schemaProperty =>
49-
{
50-
schemaProperty.ShouldContainPath("nullable").With(nullableProperty => nullableProperty.ValueKind.Should().Be(JsonValueKind.True));
51-
});
52-
});
53-
}
54-
55-
[Fact]
56-
public async Task Schema_property_for_value_type_attribute_is_not_nullable()
57-
{
58-
// Act
59-
JsonElement document = await _testContext.GetSwaggerDocumentAsync();
60-
61-
// Assert
62-
document.ShouldContainPath("components.schemas.resourceAttributesInResponse.properties").With(schemaProperties =>
63-
{
64-
schemaProperties.ShouldContainPath("valueType").With(schemaProperty =>
54+
schemaProperties.ShouldContainPath(jsonPropertyName).With(schemaProperty =>
6555
{
6656
schemaProperty.ShouldNotContainPath("nullable");
6757
});
6858
});
6959
}
7060

71-
[Fact]
72-
public async Task Schema_property_for_required_value_type_attribute_is_not_nullable()
73-
{
74-
// Act
75-
JsonElement document = await _testContext.GetSwaggerDocumentAsync();
76-
77-
// Assert
78-
document.ShouldContainPath("components.schemas.resourceAttributesInResponse.properties").With(schemaProperties =>
79-
{
80-
schemaProperties.ShouldContainPath("requiredValueType").With(schemaProperty =>
81-
{
82-
schemaProperty.ShouldNotContainPath("nullable");
83-
});
84-
});
85-
}
86-
87-
[Fact]
88-
public async Task Schema_property_for_nullable_value_type_attribute_is_nullable()
89-
{
90-
// Act
91-
JsonElement document = await _testContext.GetSwaggerDocumentAsync();
92-
93-
// Assert
94-
document.ShouldContainPath("components.schemas.resourceAttributesInResponse.properties").With(schemaProperties =>
95-
{
96-
schemaProperties.ShouldContainPath("nullableValueType").With(schemaProperty =>
97-
{
98-
schemaProperty.ShouldContainPath("nullable").With(nullableProperty => nullableProperty.ValueKind.Should().Be(JsonValueKind.True));
99-
});
100-
});
101-
}
102-
103-
[Fact]
104-
public async Task Schema_property_for_required_nullable_value_type_attribute_is_nullable()
105-
{
106-
// Act
107-
JsonElement document = await _testContext.GetSwaggerDocumentAsync();
108-
109-
// Assert
110-
document.ShouldContainPath("components.schemas.resourceAttributesInResponse.properties").With(schemaProperties =>
111-
{
112-
schemaProperties.ShouldContainPath("requiredNullableValueType").With(schemaProperty =>
113-
{
114-
schemaProperty.ShouldContainPath("nullable").With(nullableProperty => nullableProperty.ValueKind.Should().Be(JsonValueKind.True));
115-
});
116-
});
117-
}
118-
119-
[Fact]
120-
public async Task Schema_property_for_to_one_relationship_is_nullable()
61+
[Theory]
62+
[InlineData("toOne")]
63+
[InlineData("requiredToOne")]
64+
public async Task Schema_property_for_relationship_is_nullable(string jsonPropertyName)
12165
{
12266
// Act
12367
JsonElement document = await _testContext.GetSwaggerDocumentAsync();
12468

12569
// Assert
12670
document.ShouldContainPath("components.schemas.resourceRelationshipsInPostRequest.properties").With(schemaProperties =>
12771
{
128-
schemaProperties.ShouldContainPath("toOne.$ref").WithSchemaReferenceId(schemaReferenceId =>
72+
schemaProperties.ShouldContainPath($"{jsonPropertyName}.$ref").WithSchemaReferenceId(schemaReferenceId =>
12973
{
13074
document.ShouldContainPath($"components.schemas.{schemaReferenceId}.properties.data.oneOf[1].$ref").ShouldBeSchemaReferenceId("nullValue");
13175
});
13276
});
13377
}
13478

135-
[Fact]
136-
public async Task Schema_property_for_required_to_one_relationship_is_nullable()
137-
{
138-
// Act
139-
JsonElement document = await _testContext.GetSwaggerDocumentAsync();
140-
141-
// Assert
142-
document.ShouldContainPath("components.schemas.resourceRelationshipsInPostRequest.properties").With(schemaProperties =>
143-
{
144-
schemaProperties.ShouldContainPath("requiredToOne.$ref").WithSchemaReferenceId(schemaReferenceId =>
145-
{
146-
document.ShouldContainPath($"components.schemas.{schemaReferenceId}.properties.data.oneOf[1].$ref").ShouldBeSchemaReferenceId("nullValue");
147-
});
148-
});
149-
}
150-
151-
[Fact]
152-
public async Task Schema_property_for_to_many_relationship_is_not_nullable()
153-
{
154-
// Act
155-
JsonElement document = await _testContext.GetSwaggerDocumentAsync();
156-
157-
// Assert
158-
document.ShouldContainPath("components.schemas.resourceRelationshipsInPostRequest.properties").With(schemaProperties =>
159-
{
160-
schemaProperties.ShouldContainPath("toMany.$ref").WithSchemaReferenceId(schemaReferenceId =>
161-
{
162-
document.ShouldContainPath($"components.schemas.{schemaReferenceId}.properties.data").ShouldNotContainPath("oneOf[1].$ref");
163-
});
164-
});
165-
}
166-
167-
[Fact]
168-
public async Task Schema_property_for_required_to_many_relationship_is_not_nullable()
79+
[Theory]
80+
[InlineData("toMany")]
81+
[InlineData("requiredToMany")]
82+
public async Task Schema_property_for_relationship_is_not_nullable(string jsonPropertyName)
16983
{
17084
// Act
17185
JsonElement document = await _testContext.GetSwaggerDocumentAsync();
17286

17387
// Assert
17488
document.ShouldContainPath("components.schemas.resourceRelationshipsInPostRequest.properties").With(schemaProperties =>
17589
{
176-
schemaProperties.ShouldContainPath("requiredToMany.$ref").WithSchemaReferenceId(schemaReferenceId =>
90+
schemaProperties.ShouldContainPath($"{jsonPropertyName}.$ref").WithSchemaReferenceId(schemaReferenceId =>
17791
{
17892
document.ShouldContainPath($"components.schemas.{schemaReferenceId}.properties.data").ShouldNotContainPath("oneOf[1].$ref");
17993
});

0 commit comments

Comments
 (0)