Skip to content

Commit 4d4d96f

Browse files
committed
Improve test coverage
1 parent 58cc1cb commit 4d4d96f

File tree

3 files changed

+149
-9
lines changed

3 files changed

+149
-9
lines changed

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

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,14 @@ public JsonApiEndpointMetadataProvider(IControllerResourceMapping controllerReso
3131
ArgumentNullException.ThrowIfNull(descriptor);
3232

3333
var actionMethod = OpenApiActionMethod.Create(descriptor);
34+
JsonApiEndpointMetadata? metadata = null;
3435

3536
switch (actionMethod)
3637
{
3738
case AtomicOperationsActionMethod:
3839
{
39-
return new JsonApiEndpointMetadata(AtomicOperationsRequestMetadata.Instance, AtomicOperationsResponseMetadata.Instance);
40+
metadata = new JsonApiEndpointMetadata(AtomicOperationsRequestMetadata.Instance, AtomicOperationsResponseMetadata.Instance);
41+
break;
4042
}
4143
case JsonApiActionMethod jsonApiActionMethod:
4244
{
@@ -45,13 +47,13 @@ public JsonApiEndpointMetadataProvider(IControllerResourceMapping controllerReso
4547

4648
IJsonApiRequestMetadata? requestMetadata = GetRequestMetadata(jsonApiActionMethod.Endpoint, primaryResourceType);
4749
IJsonApiResponseMetadata? responseMetadata = GetResponseMetadata(jsonApiActionMethod.Endpoint, primaryResourceType);
48-
return new JsonApiEndpointMetadata(requestMetadata, responseMetadata);
49-
}
50-
default:
51-
{
52-
return null;
50+
metadata = new JsonApiEndpointMetadata(requestMetadata, responseMetadata);
51+
break;
5352
}
5453
}
54+
55+
ConsistencyGuard.ThrowIf(metadata == null);
56+
return metadata;
5557
}
5658

5759
private IJsonApiRequestMetadata? GetRequestMetadata(JsonApiEndpoints endpoint, ResourceType primaryResourceType)

src/JsonApiDotNetCore.OpenApi.Swashbuckle/JsonApiRequestFormatMetadataProvider.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System.Diagnostics;
2+
using System.Diagnostics.CodeAnalysis;
23
using Microsoft.AspNetCore.Mvc.ApiExplorer;
34
using Microsoft.AspNetCore.Mvc.Formatters;
45

@@ -10,12 +11,14 @@ namespace JsonApiDotNetCore.OpenApi.Swashbuckle;
1011
internal sealed class JsonApiRequestFormatMetadataProvider : IInputFormatter, IApiRequestFormatMetadataProvider
1112
{
1213
/// <inheritdoc />
14+
[ExcludeFromCodeCoverage]
1315
public bool CanRead(InputFormatterContext context)
1416
{
1517
return false;
1618
}
1719

1820
/// <inheritdoc />
21+
[ExcludeFromCodeCoverage]
1922
public Task<InputFormatterResult> ReadAsync(InputFormatterContext context)
2023
{
2124
throw new UnreachableException();

test/OpenApiTests/MixedControllers/MixedControllerTests.cs

Lines changed: 138 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ public async Task Default_JsonApi_endpoints_are_exposed()
4444
}
4545

4646
[Fact]
47-
public async Task Upload_endpoint_is_exposed()
47+
public async Task Upload_file_endpoint_is_exposed()
4848
{
4949
// Act
5050
JsonElement document = await _testContext.GetSwaggerDocumentAsync();
@@ -97,7 +97,7 @@ public async Task Upload_endpoint_is_exposed()
9797
}
9898

9999
[Fact]
100-
public async Task Exists_endpoint_is_exposed()
100+
public async Task File_exists_endpoint_is_exposed()
101101
{
102102
// Act
103103
JsonElement document = await _testContext.GetSwaggerDocumentAsync();
@@ -159,7 +159,7 @@ public async Task Exists_endpoint_is_exposed()
159159
}
160160

161161
[Fact]
162-
public async Task Download_endpoint_is_exposed()
162+
public async Task Download_file_endpoint_is_exposed()
163163
{
164164
// Act
165165
JsonElement document = await _testContext.GetSwaggerDocumentAsync();
@@ -227,4 +227,139 @@ public async Task Download_endpoint_is_exposed()
227227
}
228228
""");
229229
}
230+
231+
[Fact]
232+
public async Task Send_email_endpoint_is_exposed()
233+
{
234+
// Act
235+
JsonElement document = await _testContext.GetSwaggerDocumentAsync();
236+
237+
// Assert
238+
document.Should().ContainPath("paths./emails/send.post").Should().BeJson("""
239+
{
240+
"tags": [
241+
"emails"
242+
],
243+
"description": "Sends an email to the specified recipient.",
244+
"operationId": "sendEmail",
245+
"requestBody": {
246+
"content": {
247+
"application/json": {
248+
"schema": {
249+
"allOf": [
250+
{
251+
"$ref": "#/components/schemas/email"
252+
}
253+
],
254+
"description": "The email to send."
255+
}
256+
}
257+
},
258+
"required": true
259+
},
260+
"responses": {
261+
"200": {
262+
"description": "OK"
263+
},
264+
"400": {
265+
"description": "Bad Request",
266+
"content": {
267+
"application/problem+json": {
268+
"schema": {
269+
"$ref": "#/components/schemas/httpValidationProblemDetails"
270+
}
271+
}
272+
}
273+
}
274+
}
275+
}
276+
""");
277+
}
278+
279+
[Fact]
280+
public async Task Emails_sent_since_endpoint_is_exposed()
281+
{
282+
// Act
283+
JsonElement document = await _testContext.GetSwaggerDocumentAsync();
284+
285+
// Assert
286+
document.Should().ContainPath("paths./emails/sent-since.get").Should().BeJson("""
287+
{
288+
"tags": [
289+
"emails"
290+
],
291+
"description": "Gets all emails sent since the specified date/time.",
292+
"operationId": "getSentSince",
293+
"parameters": [
294+
{
295+
"name": "sinceUtc",
296+
"in": "query",
297+
"description": "The date/time (in UTC) since which the email was sent.",
298+
"required": true,
299+
"schema": {
300+
"type": "string",
301+
"description": "The date/time (in UTC) since which the email was sent.",
302+
"format": "date-time"
303+
}
304+
}
305+
],
306+
"responses": {
307+
"200": {
308+
"description": "OK",
309+
"content": {
310+
"application/json": {
311+
"schema": {
312+
"type": "array",
313+
"items": {
314+
"$ref": "#/components/schemas/email"
315+
}
316+
}
317+
}
318+
}
319+
},
320+
"400": {
321+
"description": "Bad Request",
322+
"content": {
323+
"application/problem+json": {
324+
"schema": {
325+
"$ref": "#/components/schemas/httpValidationProblemDetails"
326+
}
327+
}
328+
}
329+
}
330+
}
331+
}
332+
""");
333+
334+
document.Should().ContainPath("paths./emails/sent-since.head").Should().BeJson("""
335+
{
336+
"tags": [
337+
"emails"
338+
],
339+
"description": "Gets all emails sent since the specified date/time.",
340+
"operationId": "tryGetSentSince",
341+
"parameters": [
342+
{
343+
"name": "sinceUtc",
344+
"in": "query",
345+
"description": "The date/time (in UTC) since which the email was sent.",
346+
"required": true,
347+
"schema": {
348+
"type": "string",
349+
"description": "The date/time (in UTC) since which the email was sent.",
350+
"format": "date-time"
351+
}
352+
}
353+
],
354+
"responses": {
355+
"200": {
356+
"description": "OK"
357+
},
358+
"400": {
359+
"description": "Bad Request"
360+
}
361+
}
362+
}
363+
""");
364+
}
230365
}

0 commit comments

Comments
 (0)