Skip to content

Commit f8e640b

Browse files
committed
Review feedback
OpenApiTests/SchemaProperties test collection: * Allow for usage of OpenApiStartup directly * Remove unneeded adding of JsonConverter * Replace null checks with .ShouldHaveCount() calls * Adjust test names Misc: * Reverted .editorconfig changes and fixed ApiException constructor instead * Remove Debug statement
1 parent 526d03f commit f8e640b

File tree

12 files changed

+37
-64
lines changed

12 files changed

+37
-64
lines changed

.editorconfig

-4
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,6 @@ insert_final_newline = true
1111
[*.{config,csproj,css,js,json,props,ruleset,xslt}]
1212
indent_size = 2
1313

14-
[test/OpenApiClientTests/obj/*.{cs}]
15-
# Ignore compiler warnings triggered by code auto-generated by NSWag
16-
dotnet_diagnostic.CS8625.severity = suggestion
17-
1814
[*.{cs}]
1915
#### C#/.NET Coding Conventions ####
2016

src/JsonApiDotNetCore.OpenApi.Client/ApiException.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ public sealed class ApiException : Exception
1515

1616
public IReadOnlyDictionary<string, IEnumerable<string>> Headers { get; }
1717

18-
public ApiException(string message, int statusCode, string? response, IReadOnlyDictionary<string, IEnumerable<string>> headers, Exception innerException)
18+
public ApiException(string message, int statusCode, string? response, IReadOnlyDictionary<string, IEnumerable<string>> headers, Exception? innerException)
1919
: base($"{message}\n\nStatus: {statusCode}\nResponse: \n{response ?? "(null)"}", innerException)
2020
{
2121
StatusCode = statusCode;

src/JsonApiDotNetCore.OpenApi/SwaggerComponents/ResourceFieldObjectSchemaBuilder.cs

-2
Original file line numberDiff line numberDiff line change
@@ -203,8 +203,6 @@ private OpenApiSchema CreateRelationshipSchema(Type relationshipSchemaType)
203203

204204
OpenApiSchema fullSchema = _schemaRepositoryAccessor.Current.Schemas[referenceSchema.Reference.Id];
205205

206-
Console.WriteLine(relationshipSchemaType.FullName);
207-
208206
if (IsDataPropertyNullableInRelationshipSchemaType(relationshipSchemaType))
209207
{
210208
fullSchema.Properties[JsonApiObjectPropertyName.Data] =

test/OpenApiTests/OpenApiStartup.cs

+2-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
namespace OpenApiTests;
88

9-
public abstract class OpenApiStartup<TDbContext> : TestableStartup<TDbContext>
9+
public class OpenApiStartup<TDbContext> : TestableStartup<TDbContext>
1010
where TDbContext : TestableDbContext
1111
{
1212
public override void ConfigureServices(IServiceCollection services)
@@ -26,3 +26,4 @@ public override void Configure(IApplicationBuilder app)
2626
app.UseEndpoints(endpoints => endpoints.MapControllers());
2727
}
2828
}
29+

test/OpenApiTests/SchemaProperties/ModelStateValidationDisabledStartup.cs

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
using System.Text.Json.Serialization;
21
using JetBrains.Annotations;
32
using JsonApiDotNetCore.Configuration;
43
using TestBuildingBlocks;
@@ -13,7 +12,7 @@ protected override void SetJsonApiOptions(JsonApiOptions options)
1312
{
1413
base.SetJsonApiOptions(options);
1514

16-
options.SerializerOptions.Converters.Add(new JsonStringEnumConverter());
1715
options.ValidateModelState = false;
1816
}
1917
}
18+

test/OpenApiTests/SchemaProperties/NullableReferenceTypesDisabled/ModelStateValidationDisabledTests.cs

+4-4
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,12 @@ public ModelStateValidationDisabledTests(
1515
OpenApiTestContext<ModelStateValidationDisabledStartup<NullableReferenceTypesDisabledDbContext>, NullableReferenceTypesDisabledDbContext> testContext)
1616
{
1717
_testContext = testContext;
18+
1819
testContext.UseController<ChickensController>();
1920
}
2021

2122
[Fact]
22-
public async Task Resource_when_ModelStateValidation_is_disabled_produces_expected_required_property_in_schema()
23+
public async Task Produces_expected_required_property_in_schema_for_resource()
2324
{
2425
// Act
2526
JsonElement document = await _testContext.GetSwaggerDocumentAsync();
@@ -28,13 +29,12 @@ public async Task Resource_when_ModelStateValidation_is_disabled_produces_expect
2829
document.ShouldContainPath("components.schemas.chickenAttributesInPostRequest.required").With(requiredElement =>
2930
{
3031
var requiredAttributes = JsonSerializer.Deserialize<List<string>>(requiredElement.GetRawText());
31-
requiredAttributes.ShouldNotBeNull();
32+
requiredAttributes.ShouldHaveCount(3);
3233

3334
requiredAttributes.Should().Contain("nameOfCurrentFarm");
3435
requiredAttributes.Should().Contain("weight");
3536
requiredAttributes.Should().Contain("hasProducedEggs");
36-
37-
requiredAttributes.ShouldHaveCount(3);
3837
});
3938
}
4039
}
40+

test/OpenApiTests/SchemaProperties/NullableReferenceTypesDisabled/ModelStateValidationEnabledTests.cs

+6-7
Original file line numberDiff line numberDiff line change
@@ -6,20 +6,20 @@
66
namespace OpenApiTests.SchemaProperties.NullableReferenceTypesDisabled;
77

88
public sealed class ModelStateValidationEnabledTests
9-
: IClassFixture<OpenApiTestContext<SchemaPropertiesStartup<NullableReferenceTypesDisabledDbContext>, NullableReferenceTypesDisabledDbContext>>
9+
: IClassFixture<OpenApiTestContext<OpenApiStartup<NullableReferenceTypesDisabledDbContext>, NullableReferenceTypesDisabledDbContext>>
1010
{
11-
private readonly OpenApiTestContext<SchemaPropertiesStartup<NullableReferenceTypesDisabledDbContext>, NullableReferenceTypesDisabledDbContext> _testContext;
11+
private readonly OpenApiTestContext<OpenApiStartup<NullableReferenceTypesDisabledDbContext>, NullableReferenceTypesDisabledDbContext> _testContext;
1212

1313
public ModelStateValidationEnabledTests(
14-
OpenApiTestContext<SchemaPropertiesStartup<NullableReferenceTypesDisabledDbContext>, NullableReferenceTypesDisabledDbContext> testContext)
14+
OpenApiTestContext<OpenApiStartup<NullableReferenceTypesDisabledDbContext>, NullableReferenceTypesDisabledDbContext> testContext)
1515
{
1616
_testContext = testContext;
1717

1818
testContext.UseController<ChickensController>();
1919
}
2020

2121
[Fact]
22-
public async Task Resource_when_ModelStateValidation_is_enabled_produces_expected_required_property_in_schema()
22+
public async Task Produces_expected_required_property_in_schema_for_resource()
2323
{
2424
// Act
2525
JsonElement document = await _testContext.GetSwaggerDocumentAsync();
@@ -28,13 +28,12 @@ public async Task Resource_when_ModelStateValidation_is_enabled_produces_expecte
2828
document.ShouldContainPath("components.schemas.chickenAttributesInPostRequest.required").With(requiredElement =>
2929
{
3030
var requiredAttributes = JsonSerializer.Deserialize<List<string>>(requiredElement.GetRawText());
31-
requiredAttributes.ShouldNotBeNull();
31+
requiredAttributes.ShouldHaveCount(3);
3232

3333
requiredAttributes.Should().Contain("nameOfCurrentFarm");
3434
requiredAttributes.Should().Contain("weight");
3535
requiredAttributes.Should().Contain("hasProducedEggs");
36-
37-
requiredAttributes.ShouldHaveCount(3);
3836
});
3937
}
4038
}
39+

test/OpenApiTests/SchemaProperties/NullableReferenceTypesDisabled/NullabilityTests.cs

+7-7
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,11 @@
66
namespace OpenApiTests.SchemaProperties.NullableReferenceTypesDisabled;
77

88
public sealed class NullabilityTests
9-
: IClassFixture<OpenApiTestContext<SchemaPropertiesStartup<NullableReferenceTypesDisabledDbContext>, NullableReferenceTypesDisabledDbContext>>
9+
: IClassFixture<OpenApiTestContext<OpenApiStartup<NullableReferenceTypesDisabledDbContext>, NullableReferenceTypesDisabledDbContext>>
1010
{
11-
private readonly OpenApiTestContext<SchemaPropertiesStartup<NullableReferenceTypesDisabledDbContext>, NullableReferenceTypesDisabledDbContext> _testContext;
11+
private readonly OpenApiTestContext<OpenApiStartup<NullableReferenceTypesDisabledDbContext>, NullableReferenceTypesDisabledDbContext> _testContext;
1212

13-
public NullabilityTests(
14-
OpenApiTestContext<SchemaPropertiesStartup<NullableReferenceTypesDisabledDbContext>, NullableReferenceTypesDisabledDbContext> testContext)
13+
public NullabilityTests(OpenApiTestContext<OpenApiStartup<NullableReferenceTypesDisabledDbContext>, NullableReferenceTypesDisabledDbContext> testContext)
1514
{
1615
_testContext = testContext;
1716

@@ -20,7 +19,7 @@ public NullabilityTests(
2019
}
2120

2221
[Fact]
23-
public async Task Resource_produces_expected_nullable_properties_in_schema()
22+
public async Task Produces_expected_nullable_properties_in_schema_for_resource()
2423
{
2524
// Act
2625
JsonElement document = await _testContext.GetSwaggerDocumentAsync();
@@ -30,7 +29,7 @@ public async Task Resource_produces_expected_nullable_properties_in_schema()
3029
{
3130
propertiesElement.ShouldContainPath("name").With(propertyElement =>
3231
{
33-
propertyElement.ShouldContainPath("nullable").With(nullableProperty => nullableProperty.ValueKind.Should().Be(JsonValueKind.True));
32+
propertyElement.ShouldContainPath("nullable").With(element => element.ValueKind.Should().Be(JsonValueKind.True));
3433
});
3534

3635
propertiesElement.ShouldContainPath("nameOfCurrentFarm").With(propertyElement =>
@@ -50,7 +49,7 @@ public async Task Resource_produces_expected_nullable_properties_in_schema()
5049

5150
propertiesElement.ShouldContainPath("timeAtCurrentFarmInDays").With(propertyElement =>
5251
{
53-
propertyElement.ShouldContainPath("nullable").With(nullableProperty => nullableProperty.ValueKind.Should().Be(JsonValueKind.True));
52+
propertyElement.ShouldContainPath("nullable").With(element => element.ValueKind.Should().Be(JsonValueKind.True));
5453
});
5554

5655
propertiesElement.ShouldContainPath("hasProducedEggs").With(propertyElement =>
@@ -60,3 +59,4 @@ public async Task Resource_produces_expected_nullable_properties_in_schema()
6059
});
6160
}
6261
}
62+

test/OpenApiTests/SchemaProperties/NullableReferenceTypesEnabled/ModelStateValidationDisabledTests.cs

+3-4
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ public ModelStateValidationDisabledTests(
2020
}
2121

2222
[Fact]
23-
public async Task Resource_when_ModelStateValidation_is_disabled_produces_expected_required_property_in_schema()
23+
public async Task Produces_expected_required_property_in_schema_for_resource()
2424
{
2525
// Act
2626
JsonElement document = await _testContext.GetSwaggerDocumentAsync();
@@ -29,14 +29,13 @@ public async Task Resource_when_ModelStateValidation_is_disabled_produces_expect
2929
document.ShouldContainPath("components.schemas.cowAttributesInPostRequest.required").With(requiredElement =>
3030
{
3131
var requiredAttributes = JsonSerializer.Deserialize<List<string>>(requiredElement.GetRawText());
32-
requiredAttributes.ShouldNotBeNull();
32+
requiredAttributes.ShouldHaveCount(4);
3333

3434
requiredAttributes.Should().Contain("nameOfCurrentFarm");
3535
requiredAttributes.Should().Contain("nickname");
3636
requiredAttributes.Should().Contain("weight");
3737
requiredAttributes.Should().Contain("hasProducedMilk");
38-
39-
requiredAttributes.ShouldHaveCount(4);
4038
});
4139
}
4240
}
41+

test/OpenApiTests/SchemaProperties/NullableReferenceTypesEnabled/ModelStateValidationEnabledTests.cs

+6-7
Original file line numberDiff line numberDiff line change
@@ -6,20 +6,20 @@
66
namespace OpenApiTests.SchemaProperties.NullableReferenceTypesEnabled;
77

88
public sealed class ModelStateValidationEnabledTests
9-
: IClassFixture<OpenApiTestContext<SchemaPropertiesStartup<NullableReferenceTypesEnabledDbContext>, NullableReferenceTypesEnabledDbContext>>
9+
: IClassFixture<OpenApiTestContext<OpenApiStartup<NullableReferenceTypesEnabledDbContext>, NullableReferenceTypesEnabledDbContext>>
1010
{
11-
private readonly OpenApiTestContext<SchemaPropertiesStartup<NullableReferenceTypesEnabledDbContext>, NullableReferenceTypesEnabledDbContext> _testContext;
11+
private readonly OpenApiTestContext<OpenApiStartup<NullableReferenceTypesEnabledDbContext>, NullableReferenceTypesEnabledDbContext> _testContext;
1212

1313
public ModelStateValidationEnabledTests(
14-
OpenApiTestContext<SchemaPropertiesStartup<NullableReferenceTypesEnabledDbContext>, NullableReferenceTypesEnabledDbContext> testContext)
14+
OpenApiTestContext<OpenApiStartup<NullableReferenceTypesEnabledDbContext>, NullableReferenceTypesEnabledDbContext> testContext)
1515
{
1616
_testContext = testContext;
1717

1818
testContext.UseController<CowsController>();
1919
}
2020

2121
[Fact]
22-
public async Task Resource_when_ModelStateValidation_is_enabled_produces_expected_required_property_in_schema()
22+
public async Task Produces_expected_required_property_in_schema_for_resource()
2323
{
2424
// Act
2525
JsonElement document = await _testContext.GetSwaggerDocumentAsync();
@@ -28,15 +28,14 @@ public async Task Resource_when_ModelStateValidation_is_enabled_produces_expecte
2828
document.ShouldContainPath("components.schemas.cowAttributesInPostRequest.required").With(requiredElement =>
2929
{
3030
var requiredAttributes = JsonSerializer.Deserialize<List<string>>(requiredElement.GetRawText());
31-
requiredAttributes.ShouldNotBeNull();
31+
requiredAttributes.ShouldHaveCount(5);
3232

3333
requiredAttributes.Should().Contain("name");
3434
requiredAttributes.Should().Contain("nameOfCurrentFarm");
3535
requiredAttributes.Should().Contain("nickname");
3636
requiredAttributes.Should().Contain("weight");
3737
requiredAttributes.Should().Contain("hasProducedMilk");
38-
39-
requiredAttributes.ShouldHaveCount(5);
4038
});
4139
}
4240
}
41+

test/OpenApiTests/SchemaProperties/NullableReferenceTypesEnabled/NullabilityTests.cs

+7-7
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,11 @@
66
namespace OpenApiTests.SchemaProperties.NullableReferenceTypesEnabled;
77

88
public sealed class NullabilityTests
9-
: IClassFixture<OpenApiTestContext<SchemaPropertiesStartup<NullableReferenceTypesEnabledDbContext>, NullableReferenceTypesEnabledDbContext>>
9+
: IClassFixture<OpenApiTestContext<OpenApiStartup<NullableReferenceTypesEnabledDbContext>, NullableReferenceTypesEnabledDbContext>>
1010
{
11-
private readonly OpenApiTestContext<SchemaPropertiesStartup<NullableReferenceTypesEnabledDbContext>, NullableReferenceTypesEnabledDbContext> _testContext;
11+
private readonly OpenApiTestContext<OpenApiStartup<NullableReferenceTypesEnabledDbContext>, NullableReferenceTypesEnabledDbContext> _testContext;
1212

13-
public NullabilityTests(
14-
OpenApiTestContext<SchemaPropertiesStartup<NullableReferenceTypesEnabledDbContext>, NullableReferenceTypesEnabledDbContext> testContext)
13+
public NullabilityTests(OpenApiTestContext<OpenApiStartup<NullableReferenceTypesEnabledDbContext>, NullableReferenceTypesEnabledDbContext> testContext)
1514
{
1615
_testContext = testContext;
1716

@@ -20,7 +19,7 @@ public NullabilityTests(
2019
}
2120

2221
[Fact]
23-
public async Task Resource_produces_expected_nullable_properties_in_schema()
22+
public async Task Produces_expected_nullable_properties_in_schema_for_resource()
2423
{
2524
// Act
2625
JsonElement document = await _testContext.GetSwaggerDocumentAsync();
@@ -40,7 +39,7 @@ public async Task Resource_produces_expected_nullable_properties_in_schema()
4039

4140
propertiesElement.ShouldContainPath("nameOfPreviousFarm").With(propertyElement =>
4241
{
43-
propertyElement.ShouldContainPath("nullable").With(nullableProperty => nullableProperty.ValueKind.Should().Be(JsonValueKind.True));
42+
propertyElement.ShouldContainPath("nullable").With(element => element.ValueKind.Should().Be(JsonValueKind.True));
4443
});
4544

4645
propertiesElement.ShouldContainPath("nickname").With(propertyElement =>
@@ -60,7 +59,7 @@ public async Task Resource_produces_expected_nullable_properties_in_schema()
6059

6160
propertiesElement.ShouldContainPath("timeAtCurrentFarmInDays").With(propertyElement =>
6261
{
63-
propertyElement.ShouldContainPath("nullable").With(nullableProperty => nullableProperty.ValueKind.Should().Be(JsonValueKind.True));
62+
propertyElement.ShouldContainPath("nullable").With(element => element.ValueKind.Should().Be(JsonValueKind.True));
6463
});
6564

6665
propertiesElement.ShouldContainPath("hasProducedMilk").With(propertyElement =>
@@ -70,3 +69,4 @@ public async Task Resource_produces_expected_nullable_properties_in_schema()
7069
});
7170
}
7271
}
72+

test/OpenApiTests/SchemaProperties/SchemaPropertiesStartup.cs

-18
This file was deleted.

0 commit comments

Comments
 (0)