Skip to content

Commit 49108f3

Browse files
committed
Review feedback: NullabilityTests
1 parent 945a803 commit 49108f3

File tree

9 files changed

+209
-128
lines changed

9 files changed

+209
-128
lines changed

test/OpenApiClientTests/PropertyInfoAssertionsExtension.cs

+3-13
Original file line numberDiff line numberDiff line change
@@ -7,24 +7,14 @@ namespace OpenApiClientTests;
77
internal static class PropertyInfoAssertionsExtensions
88
{
99
[CustomAssertion]
10-
public static void BeNullable(this PropertyInfoAssertions source, string because = "", params object[] becauseArgs)
10+
public static void HaveNullabilityState(this PropertyInfoAssertions source, NullabilityState expected, string because = "", params object[] becauseArgs)
1111
{
1212
PropertyInfo propertyInfo = source.Subject;
1313

1414
NullabilityInfoContext nullabilityContext = new();
1515
NullabilityInfo nullabilityInfo = nullabilityContext.Create(propertyInfo);
1616

17-
nullabilityInfo.ReadState.Should().NotBe(NullabilityState.NotNull, because, becauseArgs);
18-
}
19-
20-
[CustomAssertion]
21-
public static void BeNonNullable(this PropertyInfoAssertions source, string because = "", params object[] becauseArgs)
22-
{
23-
PropertyInfo propertyInfo = source.Subject;
24-
25-
NullabilityInfoContext nullabilityContext = new();
26-
NullabilityInfo nullabilityInfo = nullabilityContext.Create(propertyInfo);
27-
28-
nullabilityInfo.ReadState.Should().Be(NullabilityState.NotNull, because, becauseArgs);
17+
nullabilityInfo.ReadState.Should().Be(expected, because, becauseArgs);
18+
nullabilityInfo.WriteState.Should().Be(expected, because, becauseArgs);
2919
}
3020
}

test/OpenApiClientTests/SchemaProperties/NullableReferenceTypesDisabled/NullabilityTests.cs

+11-15
Original file line numberDiff line numberDiff line change
@@ -7,21 +7,17 @@ namespace OpenApiClientTests.SchemaProperties.NullableReferenceTypesDisabled;
77

88
public sealed class NullabilityTests
99
{
10-
[Fact]
11-
public void Nullability_of_generated_types_is_as_expected()
10+
[Theory]
11+
[InlineData(nameof(ChickenAttributesInResponse.Name), NullabilityState.Unknown)]
12+
[InlineData(nameof(ChickenAttributesInResponse.NameOfCurrentFarm), NullabilityState.Unknown)]
13+
[InlineData(nameof(ChickenAttributesInResponse.Age), NullabilityState.NotNull)]
14+
[InlineData(nameof(ChickenAttributesInResponse.Weight), NullabilityState.NotNull)]
15+
[InlineData(nameof(ChickenAttributesInResponse.TimeAtCurrentFarmInDays), NullabilityState.Nullable)]
16+
[InlineData(nameof(ChickenAttributesInResponse.HasProducedEggs), NullabilityState.NotNull)]
17+
public void Nullability_of_generated_property_is_as_expected(string propertyName, NullabilityState expectedState)
1218
{
13-
PropertyInfo[] propertyInfos = typeof(ChickenAttributesInResponse).GetProperties();
14-
15-
PropertyInfo? propertyInfo = propertyInfos.FirstOrDefault(property => property.Name == nameof(ChickenAttributesInResponse.Name));
16-
propertyInfo.Should().BeNullable();
17-
18-
propertyInfo = propertyInfos.FirstOrDefault(property => property.Name == nameof(ChickenAttributesInResponse.NameOfCurrentFarm));
19-
propertyInfo.Should().BeNullable();
20-
21-
propertyInfo = propertyInfos.FirstOrDefault(property => property.Name == nameof(ChickenAttributesInResponse.Age));
22-
propertyInfo.Should().BeNonNullable();
23-
24-
propertyInfo = propertyInfos.FirstOrDefault(property => property.Name == nameof(ChickenAttributesInResponse.TimeAtCurrentFarmInDays));
25-
propertyInfo.Should().BeNullable();
19+
PropertyInfo[] properties = typeof(ChickenAttributesInResponse).GetProperties();
20+
PropertyInfo property = properties.Single(property => property.Name == propertyName);
21+
property.Should().HaveNullabilityState(expectedState);
2622
}
2723
}

test/OpenApiClientTests/SchemaProperties/NullableReferenceTypesEnabled/NullabilityTests.cs

+13-15
Original file line numberDiff line numberDiff line change
@@ -7,21 +7,19 @@ namespace OpenApiClientTests.SchemaProperties.NullableReferenceTypesEnabled;
77

88
public sealed class NullabilityTests
99
{
10-
[Fact]
11-
public void Nullability_of_generated_types_is_as_expected()
10+
[Theory]
11+
[InlineData(nameof(CowAttributesInResponse.Name), NullabilityState.NotNull)]
12+
[InlineData(nameof(CowAttributesInResponse.NameOfCurrentFarm), NullabilityState.NotNull)]
13+
[InlineData(nameof(CowAttributesInResponse.NameOfPreviousFarm), NullabilityState.Nullable)]
14+
[InlineData(nameof(CowAttributesInResponse.Nickname), NullabilityState.NotNull)]
15+
[InlineData(nameof(CowAttributesInResponse.Age), NullabilityState.NotNull)]
16+
[InlineData(nameof(CowAttributesInResponse.Weight), NullabilityState.NotNull)]
17+
[InlineData(nameof(CowAttributesInResponse.TimeAtCurrentFarmInDays), NullabilityState.Nullable)]
18+
[InlineData(nameof(CowAttributesInResponse.HasProducedMilk), NullabilityState.NotNull)]
19+
public void Nullability_of_generated_property_is_as_expected(string propertyName, NullabilityState expectedState)
1220
{
13-
PropertyInfo[] propertyInfos = typeof(CowAttributesInResponse).GetProperties();
14-
15-
PropertyInfo? propertyInfo = propertyInfos.FirstOrDefault(property => property.Name == nameof(CowAttributesInResponse.Name));
16-
propertyInfo.Should().BeNonNullable();
17-
18-
propertyInfo = propertyInfos.FirstOrDefault(property => property.Name == nameof(CowAttributesInResponse.NameOfPreviousFarm));
19-
propertyInfo.Should().BeNullable();
20-
21-
propertyInfo = propertyInfos.FirstOrDefault(property => property.Name == nameof(CowAttributesInResponse.Age));
22-
propertyInfo.Should().BeNonNullable();
23-
24-
propertyInfo = propertyInfos.FirstOrDefault(property => property.Name == nameof(CowAttributesInResponse.TimeAtCurrentFarmInDays));
25-
propertyInfo.Should().BeNullable();
21+
PropertyInfo[] properties = typeof(CowAttributesInResponse).GetProperties();
22+
PropertyInfo property = properties.Single(property => property.Name == propertyName);
23+
property.Should().HaveNullabilityState(expectedState);
2624
}
2725
}

test/OpenApiTests/SchemaProperties/NullableReferenceTypesDisabled/ModelStateValidationDisabledTests.cs

+35-7
Original file line numberDiff line numberDiff line change
@@ -19,21 +19,49 @@ public ModelStateValidationDisabledTests(
1919
testContext.UseController<ChickensController>();
2020
}
2121

22-
[Fact]
23-
public async Task Produces_expected_required_property_set_in_schema_for_resource()
22+
[Theory]
23+
[InlineData("nameOfCurrentFarm")]
24+
[InlineData("weight")]
25+
[InlineData("hasProducedEggs")]
26+
public async Task Property_in_schema_for_attributes_in_POST_request_should_be_required(string propertyName)
27+
{
28+
// Act
29+
JsonElement document = await _testContext.GetSwaggerDocumentAsync();
30+
31+
// Assert
32+
document.ShouldContainPath("components.schemas.chickenAttributesInPostRequest.required").With(propertySet =>
33+
{
34+
var requiredProperties = JsonSerializer.Deserialize<List<string>>(propertySet.GetRawText());
35+
36+
requiredProperties.Should().Contain(propertyName);
37+
});
38+
}
39+
40+
[Theory]
41+
[InlineData("name")]
42+
[InlineData("age")]
43+
[InlineData("timeAtCurrentFarmInDays")]
44+
public async Task Property_in_schema_for_attributes_in_POST_request_should_not_be_required(string propertyName)
2445
{
2546
// Act
2647
JsonElement document = await _testContext.GetSwaggerDocumentAsync();
2748

2849
// Assert
2950
document.ShouldContainPath("components.schemas.chickenAttributesInPostRequest.required").With(propertySet =>
3051
{
31-
var requiredAttributes = JsonSerializer.Deserialize<List<string>>(propertySet.GetRawText());
32-
requiredAttributes.ShouldHaveCount(3);
52+
var requiredProperties = JsonSerializer.Deserialize<List<string>>(propertySet.GetRawText());
3353

34-
requiredAttributes.Should().Contain("nameOfCurrentFarm");
35-
requiredAttributes.Should().Contain("weight");
36-
requiredAttributes.Should().Contain("hasProducedEggs");
54+
requiredProperties.Should().NotContain(propertyName);
3755
});
3856
}
57+
58+
[Fact]
59+
public async Task Schema_for_attributes_in_PATCH_request_should_have_no_required_properties()
60+
{
61+
// Act
62+
JsonElement document = await _testContext.GetSwaggerDocumentAsync();
63+
64+
// Assert
65+
document.ShouldNotContainPath("components.schemas.chickenAttributesInPatchRequest.required");
66+
}
3967
}

test/OpenApiTests/SchemaProperties/NullableReferenceTypesDisabled/ModelStateValidationEnabledTests.cs

+34-6
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,11 @@ public ModelStateValidationEnabledTests(
1818
testContext.UseController<ChickensController>();
1919
}
2020

21-
[Fact]
22-
public async Task Produces_expected_required_property_set_in_schema_for_resource()
21+
[Theory]
22+
[InlineData("nameOfCurrentFarm")]
23+
[InlineData("weight")]
24+
[InlineData("hasProducedEggs")]
25+
public async Task Property_in_schema_for_attributes_in_POST_request_should_be_required(string attributeName)
2326
{
2427
// Act
2528
JsonElement document = await _testContext.GetSwaggerDocumentAsync();
@@ -28,11 +31,36 @@ public async Task Produces_expected_required_property_set_in_schema_for_resource
2831
document.ShouldContainPath("components.schemas.chickenAttributesInPostRequest.required").With(propertySet =>
2932
{
3033
var requiredAttributes = JsonSerializer.Deserialize<List<string>>(propertySet.GetRawText());
31-
requiredAttributes.ShouldHaveCount(3);
3234

33-
requiredAttributes.Should().Contain("nameOfCurrentFarm");
34-
requiredAttributes.Should().Contain("weight");
35-
requiredAttributes.Should().Contain("hasProducedEggs");
35+
requiredAttributes.Should().Contain(attributeName);
36+
});
37+
}
38+
39+
[Theory]
40+
[InlineData("name")]
41+
[InlineData("age")]
42+
[InlineData("timeAtCurrentFarmInDays")]
43+
public async Task Property_in_schema_for_attributes_in_POST_request_should_not_be_required(string attributeName)
44+
{
45+
// Act
46+
JsonElement document = await _testContext.GetSwaggerDocumentAsync();
47+
48+
// Assert
49+
document.ShouldContainPath("components.schemas.chickenAttributesInPostRequest.required").With(propertySet =>
50+
{
51+
var requiredProperties = JsonSerializer.Deserialize<List<string>>(propertySet.GetRawText());
52+
53+
requiredProperties.Should().NotContain(attributeName);
3654
});
3755
}
56+
57+
[Fact]
58+
public async Task Schema_for_attributes_in_PATCH_request_should_have_no_required_properties()
59+
{
60+
// Act
61+
JsonElement document = await _testContext.GetSwaggerDocumentAsync();
62+
63+
// Assert
64+
document.ShouldNotContainPath("components.schemas.chickenAttributesInPatchRequest.required");
65+
}
3866
}

test/OpenApiTests/SchemaProperties/NullableReferenceTypesDisabled/NullabilityTests.cs

+20-23
Original file line numberDiff line numberDiff line change
@@ -18,41 +18,38 @@ public NullabilityTests(OpenApiTestContext<OpenApiStartup<NullableReferenceTypes
1818
testContext.SwaggerDocumentOutputPath = "test/OpenApiClientTests/SchemaProperties/NullableReferenceTypesDisabled";
1919
}
2020

21-
[Fact]
22-
public async Task Produces_expected_nullable_properties_in_schema_for_resource()
21+
[Theory]
22+
[InlineData("name")]
23+
[InlineData("timeAtCurrentFarmInDays")]
24+
public async Task Property_in_schema_for_resource_should_be_nullable(string propertyName)
2325
{
2426
// Act
2527
JsonElement document = await _testContext.GetSwaggerDocumentAsync();
2628

2729
// Assert
2830
document.ShouldContainPath("components.schemas.chickenAttributesInResponse.properties").With(schemaProperties =>
2931
{
30-
schemaProperties.ShouldContainPath("name").With(schemaProperty =>
32+
schemaProperties.ShouldContainPath(propertyName).With(schemaProperty =>
3133
{
3234
schemaProperty.ShouldContainPath("nullable").With(nullableProperty => nullableProperty.ValueKind.Should().Be(JsonValueKind.True));
3335
});
36+
});
37+
}
3438

35-
schemaProperties.ShouldContainPath("nameOfCurrentFarm").With(schemaProperty =>
36-
{
37-
schemaProperty.ShouldNotContainPath("nullable");
38-
});
39-
40-
schemaProperties.ShouldContainPath("age").With(schemaProperty =>
41-
{
42-
schemaProperty.ShouldNotContainPath("nullable");
43-
});
44-
45-
schemaProperties.ShouldContainPath("weight").With(schemaProperty =>
46-
{
47-
schemaProperty.ShouldNotContainPath("nullable");
48-
});
49-
50-
schemaProperties.ShouldContainPath("timeAtCurrentFarmInDays").With(schemaProperty =>
51-
{
52-
schemaProperty.ShouldContainPath("nullable").With(nullableProperty => nullableProperty.ValueKind.Should().Be(JsonValueKind.True));
53-
});
39+
[Theory]
40+
[InlineData("nameOfCurrentFarm")]
41+
[InlineData("age")]
42+
[InlineData("weight")]
43+
[InlineData("hasProducedEggs")]
44+
public async Task Property_in_schema_for_resource_should_not_be_nullable(string propertyName)
45+
{
46+
// Act
47+
JsonElement document = await _testContext.GetSwaggerDocumentAsync();
5448

55-
schemaProperties.ShouldContainPath("hasProducedEggs").With(schemaProperty =>
49+
// Assert
50+
document.ShouldContainPath("components.schemas.chickenAttributesInResponse.properties").With(schemaProperties =>
51+
{
52+
schemaProperties.ShouldContainPath(propertyName).With(schemaProperty =>
5653
{
5754
schemaProperty.ShouldNotContainPath("nullable");
5855
});

test/OpenApiTests/SchemaProperties/NullableReferenceTypesEnabled/ModelStateValidationDisabledTests.cs

+35-7
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,12 @@ public ModelStateValidationDisabledTests(
1919
testContext.UseController<CowsController>();
2020
}
2121

22-
[Fact]
23-
public async Task Produces_expected_required_property_set_in_schema_for_resource()
22+
[Theory]
23+
[InlineData("nameOfCurrentFarm")]
24+
[InlineData("nickname")]
25+
[InlineData("weight")]
26+
[InlineData("hasProducedMilk")]
27+
public async Task Property_in_schema_for_attributes_in_POST_request_should_be_required(string propertyName)
2428
{
2529
// Act
2630
JsonElement document = await _testContext.GetSwaggerDocumentAsync();
@@ -29,12 +33,36 @@ public async Task Produces_expected_required_property_set_in_schema_for_resource
2933
document.ShouldContainPath("components.schemas.cowAttributesInPostRequest.required").With(propertySet =>
3034
{
3135
var requiredAttributes = JsonSerializer.Deserialize<List<string>>(propertySet.GetRawText());
32-
requiredAttributes.ShouldHaveCount(4);
3336

34-
requiredAttributes.Should().Contain("nameOfCurrentFarm");
35-
requiredAttributes.Should().Contain("nickname");
36-
requiredAttributes.Should().Contain("weight");
37-
requiredAttributes.Should().Contain("hasProducedMilk");
37+
requiredAttributes.Should().Contain(propertyName);
38+
});
39+
}
40+
41+
[Theory]
42+
[InlineData("name")]
43+
[InlineData("age")]
44+
[InlineData("timeAtCurrentFarmInDays")]
45+
public async Task Property_in_schema_for_attributes_in_POST_request_should_not_be_required(string propertyName)
46+
{
47+
// Act
48+
JsonElement document = await _testContext.GetSwaggerDocumentAsync();
49+
50+
// Assert
51+
document.ShouldContainPath("components.schemas.cowAttributesInPostRequest.required").With(propertySet =>
52+
{
53+
var requiredProperties = JsonSerializer.Deserialize<List<string>>(propertySet.GetRawText());
54+
55+
requiredProperties.Should().NotContain(propertyName);
3856
});
3957
}
58+
59+
[Fact]
60+
public async Task Schema_for_attributes_in_PATCH_request_should_have_no_required_properties()
61+
{
62+
// Act
63+
JsonElement document = await _testContext.GetSwaggerDocumentAsync();
64+
65+
// Assert
66+
document.ShouldNotContainPath("components.schemas.chickenAttributesInPatchRequest.required");
67+
}
4068
}

test/OpenApiTests/SchemaProperties/NullableReferenceTypesEnabled/ModelStateValidationEnabledTests.cs

+35-8
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,13 @@ public ModelStateValidationEnabledTests(
1818
testContext.UseController<CowsController>();
1919
}
2020

21-
[Fact]
22-
public async Task Produces_expected_required_property_in_schema_for_resource()
21+
[Theory]
22+
[InlineData("name")]
23+
[InlineData("nameOfCurrentFarm")]
24+
[InlineData("nickname")]
25+
[InlineData("weight")]
26+
[InlineData("hasProducedMilk")]
27+
public async Task Property_in_schema_for_attributes_in_POST_request_should_be_required(string propertyName)
2328
{
2429
// Act
2530
JsonElement document = await _testContext.GetSwaggerDocumentAsync();
@@ -28,13 +33,35 @@ public async Task Produces_expected_required_property_in_schema_for_resource()
2833
document.ShouldContainPath("components.schemas.cowAttributesInPostRequest.required").With(propertySet =>
2934
{
3035
var requiredAttributes = JsonSerializer.Deserialize<List<string>>(propertySet.GetRawText());
31-
requiredAttributes.ShouldHaveCount(5);
3236

33-
requiredAttributes.Should().Contain("name");
34-
requiredAttributes.Should().Contain("nameOfCurrentFarm");
35-
requiredAttributes.Should().Contain("nickname");
36-
requiredAttributes.Should().Contain("weight");
37-
requiredAttributes.Should().Contain("hasProducedMilk");
37+
requiredAttributes.Should().Contain(propertyName);
38+
});
39+
}
40+
41+
[Theory]
42+
[InlineData("age")]
43+
[InlineData("timeAtCurrentFarmInDays")]
44+
public async Task Property_in_schema_for_attributes_in_POST_request_should_not_be_required(string propertyName)
45+
{
46+
// Act
47+
JsonElement document = await _testContext.GetSwaggerDocumentAsync();
48+
49+
// Assert
50+
document.ShouldContainPath("components.schemas.cowAttributesInPostRequest.required").With(propertySet =>
51+
{
52+
var requiredProperties = JsonSerializer.Deserialize<List<string>>(propertySet.GetRawText());
53+
54+
requiredProperties.Should().NotContain(propertyName);
3855
});
3956
}
57+
58+
[Fact]
59+
public async Task Schema_for_attributes_in_PATCH_request_should_have_no_required_properties()
60+
{
61+
// Act
62+
JsonElement document = await _testContext.GetSwaggerDocumentAsync();
63+
64+
// Assert
65+
document.ShouldNotContainPath("components.schemas.chickenAttributesInPatchRequest.required");
66+
}
4067
}

0 commit comments

Comments
 (0)