Skip to content

Commit 45696f6

Browse files
committed
Convert switch expressions to if statements to improve code coverage
1 parent d58306b commit 45696f6

File tree

6 files changed

+228
-117
lines changed

6 files changed

+228
-117
lines changed

Diff for: src/JsonApiDotNetCore.OpenApi.Swashbuckle/JsonApiActionDescriptorCollectionProvider.cs

+5-4
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
using System.Diagnostics;
21
using System.Reflection;
32
using JsonApiDotNetCore.Errors;
43
using JsonApiDotNetCore.Middleware;
@@ -115,18 +114,20 @@ private static List<ActionDescriptor> AddJsonApiMetadataToAction(ActionDescripto
115114

116115
private static void UpdateProducesResponseTypeAttribute(ActionDescriptor endpoint, Type responseDocumentType)
117116
{
117+
ProducesResponseTypeAttribute? attribute = null;
118+
118119
if (ProducesJsonApiResponseDocument(endpoint))
119120
{
120121
var producesResponse = endpoint.GetFilterMetadata<ProducesResponseTypeAttribute>();
121122

122123
if (producesResponse != null)
123124
{
124-
producesResponse.Type = responseDocumentType;
125-
return;
125+
attribute = producesResponse;
126126
}
127127
}
128128

129-
throw new UnreachableException();
129+
ConsistencyGuard.ThrowIf(attribute == null);
130+
attribute.Type = responseDocumentType;
130131
}
131132

132133
private static bool ProducesJsonApiResponseDocument(ActionDescriptor endpoint)

Diff for: src/JsonApiDotNetCore.OpenApi.Swashbuckle/OpenApiEndpointConvention.cs

+106-48
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 JsonApiDotNetCore.Configuration;
@@ -97,20 +96,56 @@ private static bool IsEndpointAvailable(JsonApiEndpoints endpoint, ResourceType
9796

9897
// For an overridden JSON:API action method in a partial class to show up, it's flag must be turned on in [Resource].
9998
// Otherwise, it is considered to be an action method that throws because the endpoint is unavailable.
100-
return endpoint switch
101-
{
102-
JsonApiEndpoints.GetCollection => availableEndpoints.HasFlag(JsonApiEndpoints.GetCollection),
103-
JsonApiEndpoints.GetSingle => availableEndpoints.HasFlag(JsonApiEndpoints.GetSingle),
104-
JsonApiEndpoints.GetSecondary => availableEndpoints.HasFlag(JsonApiEndpoints.GetSecondary),
105-
JsonApiEndpoints.GetRelationship => availableEndpoints.HasFlag(JsonApiEndpoints.GetRelationship),
106-
JsonApiEndpoints.Post => availableEndpoints.HasFlag(JsonApiEndpoints.Post),
107-
JsonApiEndpoints.PostRelationship => availableEndpoints.HasFlag(JsonApiEndpoints.PostRelationship),
108-
JsonApiEndpoints.Patch => availableEndpoints.HasFlag(JsonApiEndpoints.Patch),
109-
JsonApiEndpoints.PatchRelationship => availableEndpoints.HasFlag(JsonApiEndpoints.PatchRelationship),
110-
JsonApiEndpoints.Delete => availableEndpoints.HasFlag(JsonApiEndpoints.Delete),
111-
JsonApiEndpoints.DeleteRelationship => availableEndpoints.HasFlag(JsonApiEndpoints.DeleteRelationship),
112-
_ => throw new UnreachableException()
113-
};
99+
return IncludesEndpoint(endpoint, availableEndpoints);
100+
}
101+
102+
private static bool IncludesEndpoint(JsonApiEndpoints endpoint, JsonApiEndpoints availableEndpoints)
103+
{
104+
bool? isIncluded = null;
105+
106+
if (endpoint == JsonApiEndpoints.GetCollection)
107+
{
108+
isIncluded = availableEndpoints.HasFlag(JsonApiEndpoints.GetCollection);
109+
}
110+
else if (endpoint == JsonApiEndpoints.GetSingle)
111+
{
112+
isIncluded = availableEndpoints.HasFlag(JsonApiEndpoints.GetSingle);
113+
}
114+
else if (endpoint == JsonApiEndpoints.GetSecondary)
115+
{
116+
isIncluded = availableEndpoints.HasFlag(JsonApiEndpoints.GetSecondary);
117+
}
118+
else if (endpoint == JsonApiEndpoints.GetRelationship)
119+
{
120+
isIncluded = availableEndpoints.HasFlag(JsonApiEndpoints.GetRelationship);
121+
}
122+
else if (endpoint == JsonApiEndpoints.Post)
123+
{
124+
isIncluded = availableEndpoints.HasFlag(JsonApiEndpoints.Post);
125+
}
126+
else if (endpoint == JsonApiEndpoints.PostRelationship)
127+
{
128+
isIncluded = availableEndpoints.HasFlag(JsonApiEndpoints.PostRelationship);
129+
}
130+
else if (endpoint == JsonApiEndpoints.Patch)
131+
{
132+
isIncluded = availableEndpoints.HasFlag(JsonApiEndpoints.Patch);
133+
}
134+
else if (endpoint == JsonApiEndpoints.PatchRelationship)
135+
{
136+
isIncluded = availableEndpoints.HasFlag(JsonApiEndpoints.PatchRelationship);
137+
}
138+
else if (endpoint == JsonApiEndpoints.Delete)
139+
{
140+
isIncluded = availableEndpoints.HasFlag(JsonApiEndpoints.Delete);
141+
}
142+
else if (endpoint == JsonApiEndpoints.DeleteRelationship)
143+
{
144+
isIncluded = availableEndpoints.HasFlag(JsonApiEndpoints.DeleteRelationship);
145+
}
146+
147+
ConsistencyGuard.ThrowIf(isIncluded == null);
148+
return isIncluded.Value;
114149
}
115150

116151
private static JsonApiEndpoints GetGeneratedControllerEndpoints(ResourceType resourceType)
@@ -158,29 +193,40 @@ private static HttpStatusCode[] GetSuccessStatusCodesForEndpoint(JsonApiEndpoint
158193
];
159194
}
160195

161-
return endpoint.Value switch
196+
HttpStatusCode[]? statusCodes = null;
197+
198+
if (endpoint.Value is JsonApiEndpoints.GetCollection or JsonApiEndpoints.GetSingle or JsonApiEndpoints.GetSecondary or JsonApiEndpoints.GetRelationship)
162199
{
163-
JsonApiEndpoints.GetCollection or JsonApiEndpoints.GetSingle or JsonApiEndpoints.GetSecondary or JsonApiEndpoints.GetRelationship =>
200+
statusCodes =
164201
[
165202
HttpStatusCode.OK,
166203
HttpStatusCode.NotModified
167-
],
168-
JsonApiEndpoints.Post =>
204+
];
205+
}
206+
else if (endpoint.Value == JsonApiEndpoints.Post)
207+
{
208+
statusCodes =
169209
[
170210
HttpStatusCode.Created,
171211
HttpStatusCode.NoContent
172-
],
173-
JsonApiEndpoints.Patch =>
212+
];
213+
}
214+
else if (endpoint.Value == JsonApiEndpoints.Patch)
215+
{
216+
statusCodes =
174217
[
175218
HttpStatusCode.OK,
176219
HttpStatusCode.NoContent
177-
],
178-
JsonApiEndpoints.Delete or JsonApiEndpoints.PostRelationship or JsonApiEndpoints.PatchRelationship or JsonApiEndpoints.DeleteRelationship =>
179-
[
180-
HttpStatusCode.NoContent
181-
],
182-
_ => throw new UnreachableException()
183-
};
220+
];
221+
}
222+
else if (endpoint.Value is JsonApiEndpoints.Delete or JsonApiEndpoints.PostRelationship or JsonApiEndpoints.PatchRelationship or
223+
JsonApiEndpoints.DeleteRelationship)
224+
{
225+
statusCodes = [HttpStatusCode.NoContent];
226+
}
227+
228+
ConsistencyGuard.ThrowIf(statusCodes == null);
229+
return statusCodes;
184230
}
185231

186232
private HttpStatusCode[] GetErrorStatusCodesForEndpoint(JsonApiEndpointWrapper endpoint, ResourceType? resourceType)
@@ -200,46 +246,58 @@ private HttpStatusCode[] GetErrorStatusCodesForEndpoint(JsonApiEndpointWrapper e
200246
// Condition doesn't apply to atomic operations, because Forbidden is also used when an operation is not accessible.
201247
ClientIdGenerationMode clientIdGeneration = resourceType?.ClientIdGeneration ?? _options.ClientIdGeneration;
202248

203-
return endpoint.Value switch
249+
HttpStatusCode[]? statusCodes = null;
250+
251+
if (endpoint.Value == JsonApiEndpoints.GetCollection)
252+
{
253+
statusCodes = [HttpStatusCode.BadRequest];
254+
}
255+
else if (endpoint.Value is JsonApiEndpoints.GetSingle or JsonApiEndpoints.GetSecondary or JsonApiEndpoints.GetRelationship)
204256
{
205-
JsonApiEndpoints.GetCollection => [HttpStatusCode.BadRequest],
206-
JsonApiEndpoints.GetSingle or JsonApiEndpoints.GetSecondary or JsonApiEndpoints.GetRelationship =>
257+
statusCodes =
207258
[
208259
HttpStatusCode.BadRequest,
209260
HttpStatusCode.NotFound
210-
],
211-
JsonApiEndpoints.Post when clientIdGeneration == ClientIdGenerationMode.Forbidden =>
261+
];
262+
}
263+
else if (endpoint.Value == JsonApiEndpoints.Post && clientIdGeneration == ClientIdGenerationMode.Forbidden)
264+
{
265+
statusCodes =
212266
[
213267
HttpStatusCode.BadRequest,
214268
HttpStatusCode.Forbidden,
215269
HttpStatusCode.NotFound,
216270
HttpStatusCode.Conflict,
217271
HttpStatusCode.UnprocessableEntity
218-
],
219-
JsonApiEndpoints.Post =>
220-
[
221-
HttpStatusCode.BadRequest,
222-
HttpStatusCode.NotFound,
223-
HttpStatusCode.Conflict,
224-
HttpStatusCode.UnprocessableEntity
225-
],
226-
JsonApiEndpoints.Patch =>
272+
];
273+
}
274+
else if (endpoint.Value is JsonApiEndpoints.Post or JsonApiEndpoints.Patch)
275+
{
276+
statusCodes =
227277
[
228278
HttpStatusCode.BadRequest,
229279
HttpStatusCode.NotFound,
230280
HttpStatusCode.Conflict,
231281
HttpStatusCode.UnprocessableEntity
232-
],
233-
JsonApiEndpoints.Delete => [HttpStatusCode.NotFound],
234-
JsonApiEndpoints.PostRelationship or JsonApiEndpoints.PatchRelationship or JsonApiEndpoints.DeleteRelationship =>
282+
];
283+
}
284+
else if (endpoint.Value == JsonApiEndpoints.Delete)
285+
{
286+
statusCodes = [HttpStatusCode.NotFound];
287+
}
288+
else if (endpoint.Value is JsonApiEndpoints.PostRelationship or JsonApiEndpoints.PatchRelationship or JsonApiEndpoints.DeleteRelationship)
289+
{
290+
statusCodes =
235291
[
236292
HttpStatusCode.BadRequest,
237293
HttpStatusCode.NotFound,
238294
HttpStatusCode.Conflict,
239295
HttpStatusCode.UnprocessableEntity
240-
],
241-
_ => throw new UnreachableException()
242-
};
296+
];
297+
}
298+
299+
ConsistencyGuard.ThrowIf(statusCodes == null);
300+
return statusCodes;
243301
}
244302

245303
private void SetRequestMetadata(ActionModel action, JsonApiEndpointWrapper endpoint)

Diff for: src/JsonApiDotNetCore.OpenApi.Swashbuckle/SchemaGenerators/Components/DataSchemaGenerator.cs

+26-33
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
using System.Collections.Concurrent;
2-
using System.Diagnostics;
32
using System.Reflection;
3+
using System.Runtime.CompilerServices;
44
using JsonApiDotNetCore.Configuration;
55
using JsonApiDotNetCore.OpenApi.Swashbuckle.JsonApiMetadata;
66
using JsonApiDotNetCore.OpenApi.Swashbuckle.JsonApiObjects.ResourceObjects;
@@ -144,62 +144,55 @@ public OpenApiSchema GenerateSchema(Type dataSchemaType, bool forRequestSchema,
144144

145145
private static Type? GetCommonSchemaType(Type schemaOpenType)
146146
{
147+
StrongBox<Type?>? boxedSchemaType = null;
148+
147149
if (schemaOpenType == typeof(IdentifierInRequest<>))
148150
{
149-
return typeof(IdentifierInRequest);
151+
boxedSchemaType = new StrongBox<Type?>(typeof(IdentifierInRequest));
150152
}
151-
152-
if (schemaOpenType == typeof(DataInCreateRequest<>))
153+
else if (schemaOpenType == typeof(DataInCreateRequest<>))
153154
{
154-
return typeof(ResourceInCreateRequest);
155+
boxedSchemaType = new StrongBox<Type?>(typeof(ResourceInCreateRequest));
155156
}
156-
157-
if (schemaOpenType == typeof(AttributesInCreateRequest<>))
157+
else if (schemaOpenType == typeof(AttributesInCreateRequest<>))
158158
{
159-
return typeof(AttributesInCreateRequest);
159+
boxedSchemaType = new StrongBox<Type?>(typeof(AttributesInCreateRequest));
160160
}
161-
162-
if (schemaOpenType == typeof(RelationshipsInCreateRequest<>))
161+
else if (schemaOpenType == typeof(RelationshipsInCreateRequest<>))
163162
{
164-
return typeof(RelationshipsInCreateRequest);
163+
boxedSchemaType = new StrongBox<Type?>(typeof(RelationshipsInCreateRequest));
165164
}
166-
167-
if (schemaOpenType == typeof(DataInUpdateRequest<>))
165+
else if (schemaOpenType == typeof(DataInUpdateRequest<>))
168166
{
169-
return typeof(ResourceInUpdateRequest);
167+
boxedSchemaType = new StrongBox<Type?>(typeof(ResourceInUpdateRequest));
170168
}
171-
172-
if (schemaOpenType == typeof(AttributesInUpdateRequest<>))
169+
else if (schemaOpenType == typeof(AttributesInUpdateRequest<>))
173170
{
174-
return typeof(AttributesInUpdateRequest);
171+
boxedSchemaType = new StrongBox<Type?>(typeof(AttributesInUpdateRequest));
175172
}
176-
177-
if (schemaOpenType == typeof(RelationshipsInUpdateRequest<>))
173+
else if (schemaOpenType == typeof(RelationshipsInUpdateRequest<>))
178174
{
179-
return typeof(RelationshipsInUpdateRequest);
175+
boxedSchemaType = new StrongBox<Type?>(typeof(RelationshipsInUpdateRequest));
180176
}
181-
182-
if (schemaOpenType == typeof(IdentifierInResponse<>))
177+
else if (schemaOpenType == typeof(IdentifierInResponse<>))
183178
{
184-
return null;
179+
boxedSchemaType = new StrongBox<Type?>(null);
185180
}
186-
187-
if (schemaOpenType == typeof(DataInResponse<>))
181+
else if (schemaOpenType == typeof(DataInResponse<>))
188182
{
189-
return typeof(ResourceInResponse);
183+
boxedSchemaType = new StrongBox<Type?>(typeof(ResourceInResponse));
190184
}
191-
192-
if (schemaOpenType == typeof(AttributesInResponse<>))
185+
else if (schemaOpenType == typeof(AttributesInResponse<>))
193186
{
194-
return typeof(AttributesInResponse);
187+
boxedSchemaType = new StrongBox<Type?>(typeof(AttributesInResponse));
195188
}
196-
197-
if (schemaOpenType == typeof(RelationshipsInResponse<>))
189+
else if (schemaOpenType == typeof(RelationshipsInResponse<>))
198190
{
199-
return typeof(RelationshipsInResponse);
191+
boxedSchemaType = new StrongBox<Type?>(typeof(RelationshipsInResponse));
200192
}
201193

202-
throw new UnreachableException();
194+
ConsistencyGuard.ThrowIf(boxedSchemaType == null);
195+
return boxedSchemaType.Value;
203196
}
204197

205198
public OpenApiSchema GenerateSchemaForCommonData(Type commonDataSchemaType, SchemaRepository schemaRepository)

0 commit comments

Comments
 (0)