Skip to content

Commit 41d6d67

Browse files
committed
Apply Resharper hint: convert switch statement to expression
1 parent 7c6f9eb commit 41d6d67

File tree

3 files changed

+37
-94
lines changed

3 files changed

+37
-94
lines changed

src/JsonApiDotNetCore.OpenApi/JsonApiMetadata/JsonApiEndpointMetadataProvider.cs

Lines changed: 14 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -50,27 +50,14 @@ public JsonApiEndpointMetadataContainer Get(MethodInfo controllerAction)
5050

5151
private IJsonApiRequestMetadata? GetRequestMetadata(JsonApiEndpoint endpoint, ResourceType primaryResourceType)
5252
{
53-
switch (endpoint)
53+
return endpoint switch
5454
{
55-
case JsonApiEndpoint.Post:
56-
{
57-
return GetPostRequestMetadata(primaryResourceType.ClrType);
58-
}
59-
case JsonApiEndpoint.Patch:
60-
{
61-
return GetPatchRequestMetadata(primaryResourceType.ClrType);
62-
}
63-
case JsonApiEndpoint.PostRelationship:
64-
case JsonApiEndpoint.PatchRelationship:
65-
case JsonApiEndpoint.DeleteRelationship:
66-
{
67-
return GetRelationshipRequestMetadata(primaryResourceType.Relationships, endpoint != JsonApiEndpoint.PatchRelationship);
68-
}
69-
default:
70-
{
71-
return null;
72-
}
73-
}
55+
JsonApiEndpoint.Post => GetPostRequestMetadata(primaryResourceType.ClrType),
56+
JsonApiEndpoint.Patch => GetPatchRequestMetadata(primaryResourceType.ClrType),
57+
JsonApiEndpoint.PostRelationship or JsonApiEndpoint.PatchRelationship or JsonApiEndpoint.DeleteRelationship => GetRelationshipRequestMetadata(
58+
primaryResourceType.Relationships, endpoint != JsonApiEndpoint.PatchRelationship),
59+
_ => null
60+
};
7461
}
7562

7663
private static PrimaryRequestMetadata GetPostRequestMetadata(Type resourceClrType)
@@ -99,28 +86,14 @@ private RelationshipRequestMetadata GetRelationshipRequestMetadata(IEnumerable<R
9986

10087
private IJsonApiResponseMetadata? GetResponseMetadata(JsonApiEndpoint endpoint, ResourceType primaryResourceType)
10188
{
102-
switch (endpoint)
89+
return endpoint switch
10390
{
104-
case JsonApiEndpoint.GetCollection:
105-
case JsonApiEndpoint.GetSingle:
106-
case JsonApiEndpoint.Post:
107-
case JsonApiEndpoint.Patch:
108-
{
109-
return GetPrimaryResponseMetadata(primaryResourceType.ClrType, endpoint == JsonApiEndpoint.GetCollection);
110-
}
111-
case JsonApiEndpoint.GetSecondary:
112-
{
113-
return GetSecondaryResponseMetadata(primaryResourceType.Relationships);
114-
}
115-
case JsonApiEndpoint.GetRelationship:
116-
{
117-
return GetRelationshipResponseMetadata(primaryResourceType.Relationships);
118-
}
119-
default:
120-
{
121-
return null;
122-
}
123-
}
91+
JsonApiEndpoint.GetCollection or JsonApiEndpoint.GetSingle or JsonApiEndpoint.Post or JsonApiEndpoint.Patch => GetPrimaryResponseMetadata(
92+
primaryResourceType.ClrType, endpoint == JsonApiEndpoint.GetCollection),
93+
JsonApiEndpoint.GetSecondary => GetSecondaryResponseMetadata(primaryResourceType.Relationships),
94+
JsonApiEndpoint.GetRelationship => GetRelationshipResponseMetadata(primaryResourceType.Relationships),
95+
_ => null
96+
};
12497
}
12598

12699
private static PrimaryResponseMetadata GetPrimaryResponseMetadata(Type resourceClrType, bool endpointReturnsCollection)

src/JsonApiDotNetCore.OpenApi/OpenApiEndpointConvention.cs

Lines changed: 17 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -105,49 +105,28 @@ private void SetResponseMetadata(ActionModel action, JsonApiEndpoint endpoint)
105105

106106
private static IList<int> GetStatusCodesForEndpoint(JsonApiEndpoint endpoint)
107107
{
108-
switch (endpoint)
108+
return endpoint switch
109109
{
110-
case JsonApiEndpoint.GetCollection:
111-
case JsonApiEndpoint.GetSingle:
112-
case JsonApiEndpoint.GetSecondary:
113-
case JsonApiEndpoint.GetRelationship:
110+
JsonApiEndpoint.GetCollection or JsonApiEndpoint.GetSingle or JsonApiEndpoint.GetSecondary or JsonApiEndpoint.GetRelationship => new[]
114111
{
115-
return new[]
116-
{
117-
StatusCodes.Status200OK
118-
};
119-
}
120-
case JsonApiEndpoint.Post:
112+
StatusCodes.Status200OK
113+
},
114+
JsonApiEndpoint.Post => new[]
121115
{
122-
return new[]
123-
{
124-
StatusCodes.Status201Created,
125-
StatusCodes.Status204NoContent
126-
};
127-
}
128-
case JsonApiEndpoint.Patch:
116+
StatusCodes.Status201Created,
117+
StatusCodes.Status204NoContent
118+
},
119+
JsonApiEndpoint.Patch => new[]
129120
{
130-
return new[]
131-
{
132-
StatusCodes.Status200OK,
133-
StatusCodes.Status204NoContent
134-
};
135-
}
136-
case JsonApiEndpoint.Delete:
137-
case JsonApiEndpoint.PostRelationship:
138-
case JsonApiEndpoint.PatchRelationship:
139-
case JsonApiEndpoint.DeleteRelationship:
121+
StatusCodes.Status200OK,
122+
StatusCodes.Status204NoContent
123+
},
124+
JsonApiEndpoint.Delete or JsonApiEndpoint.PostRelationship or JsonApiEndpoint.PatchRelationship or JsonApiEndpoint.DeleteRelationship => new[]
140125
{
141-
return new[]
142-
{
143-
StatusCodes.Status204NoContent
144-
};
145-
}
146-
default:
147-
{
148-
throw new UnreachableCodeException();
149-
}
150-
}
126+
StatusCodes.Status204NoContent
127+
},
128+
_ => throw new UnreachableCodeException()
129+
};
151130
}
152131

153132
private static void SetRequestMetadata(ActionModel action, JsonApiEndpoint endpoint)

test/OpenApiClientTests/FakerFactory.cs

Lines changed: 6 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -54,22 +54,13 @@ public override void Generate(AutoGenerateOverrideContext context)
5454

5555
private static object ToPositiveValue(object idValue)
5656
{
57-
if (idValue is short shortValue)
57+
return idValue switch
5858
{
59-
return Math.Abs(shortValue);
60-
}
61-
62-
if (idValue is int intValue)
63-
{
64-
return Math.Abs(intValue);
65-
}
66-
67-
if (idValue is long longValue)
68-
{
69-
return Math.Abs(longValue);
70-
}
71-
72-
return idValue;
59+
short shortValue => Math.Abs(shortValue),
60+
int intValue => Math.Abs(intValue),
61+
long longValue => Math.Abs(longValue),
62+
_ => idValue
63+
};
7364
}
7465

7566
[UsedImplicitly(ImplicitUseTargetFlags.Members)]

0 commit comments

Comments
 (0)