Skip to content

Commit d58306b

Browse files
committed
Improve code coverage by skipping unreachable code
1 parent fb78af3 commit d58306b

11 files changed

+33
-73
lines changed
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
using System.Diagnostics;
2+
using System.Diagnostics.CodeAnalysis;
3+
4+
namespace JsonApiDotNetCore.OpenApi.Swashbuckle;
5+
6+
#pragma warning disable AV1008 // Class should not be static
7+
8+
internal static class ConsistencyGuard
9+
{
10+
[ExcludeFromCodeCoverage]
11+
public static void ThrowIf([DoesNotReturnIf(true)] bool condition)
12+
{
13+
if (condition)
14+
{
15+
throw new UnreachableException();
16+
}
17+
}
18+
}

src/JsonApiDotNetCore.OpenApi.Swashbuckle/JsonApiMetadata/JsonApiEndpointMetadataProvider.cs

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
using System.Diagnostics;
21
using System.Reflection;
32
using JsonApiDotNetCore.Configuration;
43
using JsonApiDotNetCore.Controllers;
@@ -43,11 +42,7 @@ public JsonApiEndpointMetadataContainer Get(MethodInfo controllerAction)
4342
}
4443

4544
ResourceType? primaryResourceType = _controllerResourceMapping.GetResourceTypeForController(controllerAction.ReflectedType);
46-
47-
if (primaryResourceType == null)
48-
{
49-
throw new UnreachableException();
50-
}
45+
ConsistencyGuard.ThrowIf(primaryResourceType == null);
5146

5247
IJsonApiRequestMetadata? requestMetadata = GetRequestMetadata(endpoint, primaryResourceType);
5348
IJsonApiResponseMetadata? responseMetadata = GetResponseMetadata(endpoint, primaryResourceType);

src/JsonApiDotNetCore.OpenApi.Swashbuckle/OpenApiOperationIdSelector.cs

Lines changed: 4 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
using System.Diagnostics;
21
using System.Reflection;
32
using System.Text.Json;
43
using Humanizer;
@@ -64,23 +63,14 @@ public string GetOpenApiOperationId(ApiDescription endpoint)
6463
private static string GetTemplate(ApiDescription endpoint)
6564
{
6665
Type bodyType = GetBodyType(endpoint);
67-
68-
if (!SchemaOpenTypeToOpenApiOperationIdTemplateMap.TryGetValue(bodyType, out string? template))
69-
{
70-
throw new UnreachableException();
71-
}
72-
66+
ConsistencyGuard.ThrowIf(!SchemaOpenTypeToOpenApiOperationIdTemplateMap.TryGetValue(bodyType, out string? template));
7367
return template;
7468
}
7569

7670
private static Type GetBodyType(ApiDescription endpoint)
7771
{
7872
var producesResponseTypeAttribute = endpoint.ActionDescriptor.GetFilterMetadata<ProducesResponseTypeAttribute>();
79-
80-
if (producesResponseTypeAttribute == null)
81-
{
82-
throw new UnreachableException();
83-
}
73+
ConsistencyGuard.ThrowIf(producesResponseTypeAttribute == null);
8474

8575
ControllerParameterDescriptor? requestBodyDescriptor = endpoint.ActionDescriptor.GetBodyParameterDescriptor();
8676
Type bodyType = (requestBodyDescriptor?.ParameterType ?? producesResponseTypeAttribute.Type).ConstructedToOpenType();
@@ -95,10 +85,8 @@ private static Type GetBodyType(ApiDescription endpoint)
9585

9686
private string ApplyTemplate(string openApiOperationIdTemplate, ResourceType? resourceType, ApiDescription endpoint)
9787
{
98-
if (endpoint.RelativePath == null || endpoint.HttpMethod == null)
99-
{
100-
throw new UnreachableException();
101-
}
88+
ConsistencyGuard.ThrowIf(endpoint.RelativePath == null);
89+
ConsistencyGuard.ThrowIf(endpoint.HttpMethod == null);
10290

10391
string method = endpoint.HttpMethod.ToLowerInvariant();
10492
string relationshipName = openApiOperationIdTemplate.Contains("[RelationshipName]") ? endpoint.RelativePath.Split('/').Last() : string.Empty;

src/JsonApiDotNetCore.OpenApi.Swashbuckle/OpenApiSchemaExtensions.cs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
using System.Diagnostics;
21
using Microsoft.OpenApi.Models;
32

43
namespace JsonApiDotNetCore.OpenApi.Swashbuckle;
@@ -20,10 +19,7 @@ public static void ReorderProperties(this OpenApiSchema fullSchema, IEnumerable<
2019
}
2120
}
2221

23-
if (fullSchema.Properties.Count != propertiesInOrder.Count)
24-
{
25-
throw new UnreachableException();
26-
}
22+
ConsistencyGuard.ThrowIf(fullSchema.Properties.Count != propertiesInOrder.Count);
2723

2824
fullSchema.Properties = propertiesInOrder;
2925
}

src/JsonApiDotNetCore.OpenApi.Swashbuckle/SchemaGenerators/Components/DataContainerSchemaGenerator.cs

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
using System.Diagnostics;
21
using System.Reflection;
32
using JsonApiDotNetCore.Configuration;
43
using JsonApiDotNetCore.OpenApi.Swashbuckle.JsonApiObjects.ResourceObjects;
@@ -64,11 +63,7 @@ public OpenApiSchema GenerateSchema(Type dataContainerSchemaType, ResourceType r
6463
private static Type GetElementTypeOfDataProperty(Type dataContainerConstructedType, ResourceType resourceType)
6564
{
6665
PropertyInfo? dataProperty = dataContainerConstructedType.GetProperty("Data");
67-
68-
if (dataProperty == null)
69-
{
70-
throw new UnreachableException();
71-
}
66+
ConsistencyGuard.ThrowIf(dataProperty == null);
7267

7368
Type innerPropertyType = dataProperty.PropertyType.ConstructedToOpenType().IsAssignableTo(typeof(ICollection<>))
7469
? dataProperty.PropertyType.GenericTypeArguments[0]
@@ -79,10 +74,7 @@ private static Type GetElementTypeOfDataProperty(Type dataContainerConstructedTy
7974
return typeof(DataInResponse<>).MakeGenericType(resourceType.ClrType);
8075
}
8176

82-
if (!innerPropertyType.IsGenericType)
83-
{
84-
throw new UnreachableException();
85-
}
77+
ConsistencyGuard.ThrowIf(!innerPropertyType.IsGenericType);
8678

8779
return innerPropertyType;
8880
}

src/JsonApiDotNetCore.OpenApi.Swashbuckle/SchemaGenerators/Components/DataSchemaGenerator.cs

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -390,11 +390,7 @@ private void SetFieldSchemaMembers(OpenApiSchema fullSchemaForData, ResourceSche
390390
GetResourceSchemaTypeForFieldsProperty(resourceSchemaTypeForData, forAttributes ? "Attributes" : "Relationships");
391391

392392
Type? commonFieldsSchemaType = GetCommonSchemaType(resourceSchemaTypeForFields.SchemaOpenType);
393-
394-
if (commonFieldsSchemaType == null)
395-
{
396-
throw new UnreachableException();
397-
}
393+
ConsistencyGuard.ThrowIf(commonFieldsSchemaType == null);
398394

399395
_ = GenerateSchemaForCommonFields(commonFieldsSchemaType, schemaRepository);
400396

@@ -432,11 +428,7 @@ private void SetFieldSchemaMembers(OpenApiSchema fullSchemaForData, ResourceSche
432428
private ResourceSchemaType GetResourceSchemaTypeForFieldsProperty(ResourceSchemaType resourceSchemaTypeForData, string propertyName)
433429
{
434430
PropertyInfo? fieldsProperty = resourceSchemaTypeForData.SchemaConstructedType.GetProperty(propertyName);
435-
436-
if (fieldsProperty == null)
437-
{
438-
throw new UnreachableException();
439-
}
431+
ConsistencyGuard.ThrowIf(fieldsProperty == null);
440432

441433
Type fieldsConstructedType = fieldsProperty.PropertyType;
442434
return ResourceSchemaType.Create(fieldsConstructedType, _resourceGraph);

src/JsonApiDotNetCore.OpenApi.Swashbuckle/SchemaGenerators/Components/RelationshipIdentifierSchemaGenerator.cs

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
using System.Diagnostics;
21
using JsonApiDotNetCore.Configuration;
32
using JsonApiDotNetCore.OpenApi.Swashbuckle.JsonApiObjects.ResourceObjects;
43
using JsonApiDotNetCore.Resources.Annotations;
@@ -52,11 +51,7 @@ public OpenApiSchema GenerateSchema(RelationshipAttribute relationship, SchemaRe
5251
}
5352

5453
Type relationshipIdentifierConstructedType = typeof(RelationshipIdentifier<>).MakeGenericType(relationship.LeftType.ClrType);
55-
56-
if (schemaRepository.TryLookupByType(relationshipIdentifierConstructedType, out _))
57-
{
58-
throw new UnreachableException();
59-
}
54+
ConsistencyGuard.ThrowIf(schemaRepository.TryLookupByType(relationshipIdentifierConstructedType, out _));
6055

6156
OpenApiSchema referenceSchemaForIdentifier = _defaultSchemaGenerator.GenerateSchema(relationshipIdentifierConstructedType, schemaRepository);
6257
OpenApiSchema fullSchemaForIdentifier = schemaRepository.Schemas[referenceSchemaForIdentifier.Reference.Id];

src/JsonApiDotNetCore.OpenApi.Swashbuckle/SchemaGenerators/Documents/AtomicOperationsDocumentSchemaGenerator.cs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -336,11 +336,7 @@ private void GenerateSchemaForRelationshipOperation(Type operationOpenType, Rela
336336
RemoveProperties(inlineSchemaForOperation);
337337

338338
string baseRelationshipSchemaId = _schemaIdSelector.GetRelationshipAtomicOperationSchemaId(relationshipInAnyBaseResourceType, operationCode);
339-
340-
if (!schemaRepository.Schemas.ContainsKey(baseRelationshipSchemaId))
341-
{
342-
throw new UnreachableException();
343-
}
339+
ConsistencyGuard.ThrowIf(!schemaRepository.Schemas.ContainsKey(baseRelationshipSchemaId));
344340

345341
fullSchemaForOperation.AllOf[0] = new OpenApiSchema
346342
{

src/JsonApiDotNetCore.OpenApi.Swashbuckle/SwaggerComponents/DocumentationOpenApiOperationFilter.cs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
using System.Diagnostics;
21
using System.Net;
32
using System.Reflection;
43
using Humanizer;
@@ -439,10 +438,7 @@ private void ApplyDeleteRelationship(OpenApiOperation operation, RelationshipAtt
439438

440439
private static RelationshipAttribute GetRelationshipFromRoute(ApiDescription apiDescription, ResourceType resourceType)
441440
{
442-
if (apiDescription.RelativePath == null)
443-
{
444-
throw new UnreachableException();
445-
}
441+
ConsistencyGuard.ThrowIf(apiDescription.RelativePath == null);
446442

447443
string relationshipName = apiDescription.RelativePath.Split('/').Last();
448444
return resourceType.GetRelationshipByPublicName(relationshipName);

src/JsonApiDotNetCore.OpenApi.Swashbuckle/SwaggerComponents/ResourceFieldSchemaBuilder.cs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -221,9 +221,6 @@ private OpenApiSchema CreateReferenceSchemaForRelationship(Type relationshipSche
221221

222222
private static void AssertHasNoProperties(OpenApiSchema fullSchema)
223223
{
224-
if (fullSchema.Properties.Count > 0)
225-
{
226-
throw new UnreachableException();
227-
}
224+
ConsistencyGuard.ThrowIf(fullSchema.Properties.Count > 0);
228225
}
229226
}

src/JsonApiDotNetCore.OpenApi.Swashbuckle/SwaggerComponents/StringEnumOrderingFilter.cs

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
using System.Diagnostics;
21
using JetBrains.Annotations;
32
using Microsoft.OpenApi.Any;
43
using Microsoft.OpenApi.Interfaces;
@@ -47,11 +46,7 @@ private static bool HasSortAnnotation(OpenApiSchema schema)
4746
private static void OrderEnumMembers(OpenApiSchema schema)
4847
{
4948
List<IOpenApiAny> ordered = schema.Enum.OfType<OpenApiString>().OrderBy(openApiString => openApiString.Value).Cast<IOpenApiAny>().ToList();
50-
51-
if (ordered.Count != schema.Enum.Count)
52-
{
53-
throw new UnreachableException();
54-
}
49+
ConsistencyGuard.ThrowIf(ordered.Count != schema.Enum.Count);
5550

5651
schema.Enum = ordered;
5752
}

0 commit comments

Comments
 (0)