Skip to content

Commit f5bb7fc

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

File tree

9 files changed

+213
-132
lines changed

9 files changed

+213
-132
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

+35-7
Original file line numberDiff line numberDiff line change
@@ -18,21 +18,49 @@ 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)
26+
{
27+
// Act
28+
JsonElement document = await _testContext.GetSwaggerDocumentAsync();
29+
30+
// Assert
31+
document.ShouldContainPath("components.schemas.chickenAttributesInPostRequest.required").With(propertySet =>
32+
{
33+
var requiredProperties = JsonSerializer.Deserialize<List<string>>(propertySet.GetRawText());
34+
35+
requiredProperties.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)
2344
{
2445
// Act
2546
JsonElement document = await _testContext.GetSwaggerDocumentAsync();
2647

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

33-
requiredAttributes.Should().Contain("nameOfCurrentFarm");
34-
requiredAttributes.Should().Contain("weight");
35-
requiredAttributes.Should().Contain("hasProducedEggs");
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

+36-8
Original file line numberDiff line numberDiff line change
@@ -19,22 +19,50 @@ 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)
28+
{
29+
// Act
30+
JsonElement document = await _testContext.GetSwaggerDocumentAsync();
31+
32+
// Assert
33+
document.ShouldContainPath("components.schemas.cowAttributesInPostRequest.required").With(propertySet =>
34+
{
35+
var requiredProperties = JsonSerializer.Deserialize<List<string>>(propertySet.GetRawText());
36+
37+
requiredProperties.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)
2446
{
2547
// Act
2648
JsonElement document = await _testContext.GetSwaggerDocumentAsync();
2749

2850
// Assert
2951
document.ShouldContainPath("components.schemas.cowAttributesInPostRequest.required").With(propertySet =>
3052
{
31-
var requiredAttributes = JsonSerializer.Deserialize<List<string>>(propertySet.GetRawText());
32-
requiredAttributes.ShouldHaveCount(4);
53+
var requiredProperties = JsonSerializer.Deserialize<List<string>>(propertySet.GetRawText());
3354

34-
requiredAttributes.Should().Contain("nameOfCurrentFarm");
35-
requiredAttributes.Should().Contain("nickname");
36-
requiredAttributes.Should().Contain("weight");
37-
requiredAttributes.Should().Contain("hasProducedMilk");
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

+37-10
Original file line numberDiff line numberDiff line change
@@ -18,23 +18,50 @@ 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();
2631

2732
// Assert
2833
document.ShouldContainPath("components.schemas.cowAttributesInPostRequest.required").With(propertySet =>
2934
{
30-
var requiredAttributes = JsonSerializer.Deserialize<List<string>>(propertySet.GetRawText());
31-
requiredAttributes.ShouldHaveCount(5);
32-
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");
35+
var requiredProperties = JsonSerializer.Deserialize<List<string>>(propertySet.GetRawText());
36+
37+
requiredProperties.Should().Contain(propertyName);
3838
});
3939
}
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);
55+
});
56+
}
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)