Skip to content

Commit 32f7515

Browse files
committed
Post-merge fixes
1 parent 111a898 commit 32f7515

File tree

5 files changed

+51
-27
lines changed

5 files changed

+51
-27
lines changed

src/JsonApiDotNetCore.OpenApi.Swashbuckle/JsonApiActionDescriptorCollectionProvider.cs

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
using Microsoft.AspNetCore.Mvc.Filters;
1010
using Microsoft.AspNetCore.Mvc.Infrastructure;
1111
using Microsoft.AspNetCore.Mvc.Routing;
12+
using Microsoft.Net.Http.Headers;
1213

1314
namespace JsonApiDotNetCore.OpenApi.Swashbuckle;
1415

@@ -21,6 +22,8 @@ namespace JsonApiDotNetCore.OpenApi.Swashbuckle;
2122
/// </summary>
2223
internal sealed class JsonApiActionDescriptorCollectionProvider : IActionDescriptorCollectionProvider
2324
{
25+
private static readonly string DefaultMediaType = JsonApiMediaType.Default.ToString();
26+
2427
private readonly IActionDescriptorCollectionProvider _defaultProvider;
2528
private readonly JsonApiEndpointMetadataProvider _jsonApiEndpointMetadataProvider;
2629

@@ -129,8 +132,21 @@ private static bool ProducesJsonApiResponseDocument(ActionDescriptor endpoint)
129132
{
130133
var produces = endpoint.GetFilterMetadata<ProducesAttribute>();
131134

132-
return produces != null && produces.ContentTypes.Any(contentType =>
133-
contentType is HeaderConstants.MediaType or HeaderConstants.AtomicOperationsMediaType or HeaderConstants.RelaxedAtomicOperationsMediaType);
135+
if (produces != null)
136+
{
137+
foreach (string contentType in produces.ContentTypes)
138+
{
139+
if (MediaTypeHeaderValue.TryParse(contentType, out MediaTypeHeaderValue? headerValue))
140+
{
141+
if (headerValue.MediaType.Equals(DefaultMediaType, StringComparison.OrdinalIgnoreCase))
142+
{
143+
return true;
144+
}
145+
}
146+
}
147+
}
148+
149+
return false;
134150
}
135151

136152
private static List<ActionDescriptor> Expand(ActionDescriptor genericEndpoint, NonPrimaryEndpointMetadata metadata,

src/JsonApiDotNetCore.OpenApi.Swashbuckle/JsonApiRequestFormatMetadataProvider.cs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ namespace JsonApiDotNetCore.OpenApi.Swashbuckle;
77

88
internal sealed class JsonApiRequestFormatMetadataProvider : IInputFormatter, IApiRequestFormatMetadataProvider
99
{
10+
private static readonly string DefaultMediaType = JsonApiMediaType.Default.ToString();
11+
1012
/// <inheritdoc />
1113
public bool CanRead(InputFormatterContext context)
1214
{
@@ -25,12 +27,12 @@ public IReadOnlyList<string> GetSupportedContentTypes(string contentType, Type o
2527
ArgumentGuard.NotNullNorEmpty(contentType);
2628
ArgumentGuard.NotNull(objectType);
2729

28-
if (JsonApiSchemaFacts.IsRequestBodySchemaType(objectType) && contentType is HeaderConstants.MediaType or HeaderConstants.AtomicOperationsMediaType or
29-
HeaderConstants.RelaxedAtomicOperationsMediaType)
30+
if (JsonApiSchemaFacts.IsRequestBodySchemaType(objectType) && MediaTypeHeaderValue.TryParse(contentType, out MediaTypeHeaderValue? headerValue) &&
31+
headerValue.MediaType.Equals(DefaultMediaType, StringComparison.OrdinalIgnoreCase))
3032
{
3133
return new MediaTypeCollection
3234
{
33-
MediaTypeHeaderValue.Parse(contentType)
35+
headerValue
3436
};
3537
}
3638

src/JsonApiDotNetCore.OpenApi.Swashbuckle/OpenApiEndpointConvention.cs

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -129,8 +129,8 @@ private static bool IsSecondaryOrRelationshipEndpoint(JsonApiEndpoint endpoint)
129129

130130
private void SetResponseMetadata(ActionModel action, JsonApiEndpoint endpoint, ResourceType? resourceType)
131131
{
132-
string contentType = endpoint == JsonApiEndpoint.PostOperations ? HeaderConstants.RelaxedAtomicOperationsMediaType : HeaderConstants.MediaType;
133-
action.Filters.Add(new ProducesAttribute(contentType));
132+
JsonApiMediaType mediaType = GetMediaTypeForEndpoint(endpoint);
133+
action.Filters.Add(new ProducesAttribute(mediaType.ToString()));
134134

135135
foreach (HttpStatusCode statusCode in GetSuccessStatusCodesForEndpoint(endpoint))
136136
{
@@ -144,6 +144,11 @@ private void SetResponseMetadata(ActionModel action, JsonApiEndpoint endpoint, R
144144
}
145145
}
146146

147+
private JsonApiMediaType GetMediaTypeForEndpoint(JsonApiEndpoint endpoint)
148+
{
149+
return endpoint == JsonApiEndpoint.PostOperations ? JsonApiMediaType.RelaxedAtomicOperations : JsonApiMediaType.Default;
150+
}
151+
147152
private static HttpStatusCode[] GetSuccessStatusCodesForEndpoint(JsonApiEndpoint endpoint)
148153
{
149154
return endpoint switch
@@ -230,12 +235,12 @@ private HttpStatusCode[] GetErrorStatusCodesForEndpoint(JsonApiEndpoint endpoint
230235
};
231236
}
232237

233-
private static void SetRequestMetadata(ActionModel action, JsonApiEndpoint endpoint)
238+
private void SetRequestMetadata(ActionModel action, JsonApiEndpoint endpoint)
234239
{
235240
if (RequiresRequestBody(endpoint))
236241
{
237-
string contentType = endpoint == JsonApiEndpoint.PostOperations ? HeaderConstants.RelaxedAtomicOperationsMediaType : HeaderConstants.MediaType;
238-
action.Filters.Add(new ConsumesAttribute(contentType));
242+
JsonApiMediaType mediaType = GetMediaTypeForEndpoint(endpoint);
243+
action.Filters.Add(new ConsumesAttribute(mediaType.ToString()));
239244
}
240245
}
241246

test/OpenApiNSwagClientTests/FakeHttpClientWrapper.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using System.Net.Http.Headers;
33
using System.Text;
44
using System.Text.Json;
5+
using JsonApiDotNetCore.Middleware;
56
using JsonApiDotNetCore.OpenApi.Client.NSwag;
67

78
namespace OpenApiNSwagClientTests;
@@ -64,7 +65,7 @@ private static HttpResponseMessage CreateResponse(HttpStatusCode statusCode, str
6465
if (!string.IsNullOrEmpty(responseBody))
6566
{
6667
response.Content = new StringContent(responseBody, Encoding.UTF8);
67-
response.Content.Headers.ContentType = MediaTypeHeaderValue.Parse("application/vnd.api+json");
68+
response.Content.Headers.ContentType = MediaTypeHeaderValue.Parse(JsonApiMediaType.Default.ToString());
6869
}
6970

7071
return response;

test/OpenApiNSwagClientTests/LegacyOpenApi/RequestTests.cs

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ public async Task Getting_resource_collection_produces_expected_request()
2727

2828
// Assert
2929
wrapper.Request.ShouldNotBeNull();
30-
wrapper.Request.Headers.GetValue(HeaderNames.Accept).Should().Be(HeaderConstants.MediaType);
30+
wrapper.Request.Headers.GetValue(HeaderNames.Accept).Should().Be(JsonApiMediaType.Default.ToString());
3131
wrapper.Request.Method.Should().Be(HttpMethod.Get);
3232
wrapper.Request.RequestUri.Should().Be($"{HostPrefix}flights");
3333
wrapper.RequestBody.Should().BeNull();
@@ -47,7 +47,7 @@ public async Task Getting_resource_produces_expected_request()
4747

4848
// Assert
4949
wrapper.Request.ShouldNotBeNull();
50-
wrapper.Request.Headers.GetValue(HeaderNames.Accept).Should().Be(HeaderConstants.MediaType);
50+
wrapper.Request.Headers.GetValue(HeaderNames.Accept).Should().Be(JsonApiMediaType.Default.ToString());
5151
wrapper.Request.Method.Should().Be(HttpMethod.Get);
5252
wrapper.Request.RequestUri.Should().Be($"{HostPrefix}flights/{flightId}");
5353
wrapper.RequestBody.Should().BeNull();
@@ -92,12 +92,12 @@ public async Task Partial_posting_resource_with_selected_relationships_produces_
9292

9393
// Assert
9494
wrapper.Request.ShouldNotBeNull();
95-
wrapper.Request.Headers.GetValue(HeaderNames.Accept).Should().Be(HeaderConstants.MediaType);
95+
wrapper.Request.Headers.GetValue(HeaderNames.Accept).Should().Be(JsonApiMediaType.Default.ToString());
9696
wrapper.Request.Method.Should().Be(HttpMethod.Post);
9797
wrapper.Request.RequestUri.Should().Be($"{HostPrefix}flights");
9898
wrapper.Request.Content.Should().NotBeNull();
9999
wrapper.Request.Content!.Headers.ContentType.Should().NotBeNull();
100-
wrapper.Request.Content!.Headers.ContentType!.ToString().Should().Be(HeaderConstants.MediaType);
100+
wrapper.Request.Content!.Headers.ContentType!.ToString().Should().Be(JsonApiMediaType.Default.ToString());
101101

102102
wrapper.RequestBody.Should().BeJson("""
103103
{
@@ -164,12 +164,12 @@ public async Task Partial_posting_resource_produces_expected_request()
164164

165165
// Assert
166166
wrapper.Request.ShouldNotBeNull();
167-
wrapper.Request.Headers.GetValue(HeaderNames.Accept).Should().Be(HeaderConstants.MediaType);
167+
wrapper.Request.Headers.GetValue(HeaderNames.Accept).Should().Be(JsonApiMediaType.Default.ToString());
168168
wrapper.Request.Method.Should().Be(HttpMethod.Post);
169169
wrapper.Request.RequestUri.Should().Be($"{HostPrefix}airplanes");
170170
wrapper.Request.Content.Should().NotBeNull();
171171
wrapper.Request.Content!.Headers.ContentType.Should().NotBeNull();
172-
wrapper.Request.Content!.Headers.ContentType!.ToString().Should().Be(HeaderConstants.MediaType);
172+
wrapper.Request.Content!.Headers.ContentType!.ToString().Should().Be(JsonApiMediaType.Default.ToString());
173173

174174
wrapper.RequestBody.Should().BeJson($$"""
175175
{
@@ -217,12 +217,12 @@ public async Task Partial_patching_resource_produces_expected_request()
217217

218218
// Assert
219219
wrapper.Request.ShouldNotBeNull();
220-
wrapper.Request.Headers.GetValue(HeaderNames.Accept).Should().Be(HeaderConstants.MediaType);
220+
wrapper.Request.Headers.GetValue(HeaderNames.Accept).Should().Be(JsonApiMediaType.Default.ToString());
221221
wrapper.Request.Method.Should().Be(HttpMethod.Patch);
222222
wrapper.Request.RequestUri.Should().Be($"{HostPrefix}airplanes/{airplaneId}");
223223
wrapper.Request.Content.Should().NotBeNull();
224224
wrapper.Request.Content!.Headers.ContentType.Should().NotBeNull();
225-
wrapper.Request.Content!.Headers.ContentType!.ToString().Should().Be(HeaderConstants.MediaType);
225+
wrapper.Request.Content!.Headers.ContentType!.ToString().Should().Be(JsonApiMediaType.Default.ToString());
226226

227227
wrapper.RequestBody.Should().BeJson("""
228228
{
@@ -273,7 +273,7 @@ public async Task Getting_secondary_resource_produces_expected_request()
273273

274274
// Assert
275275
wrapper.Request.ShouldNotBeNull();
276-
wrapper.Request.Headers.GetValue(HeaderNames.Accept).Should().Be(HeaderConstants.MediaType);
276+
wrapper.Request.Headers.GetValue(HeaderNames.Accept).Should().Be(JsonApiMediaType.Default.ToString());
277277
wrapper.Request.Method.Should().Be(HttpMethod.Get);
278278
wrapper.Request.RequestUri.Should().Be($"{HostPrefix}flights/{flightId}/purser");
279279
wrapper.RequestBody.Should().BeNull();
@@ -293,7 +293,7 @@ public async Task Getting_secondary_resources_produces_expected_request()
293293

294294
// Assert
295295
wrapper.Request.ShouldNotBeNull();
296-
wrapper.Request.Headers.GetValue(HeaderNames.Accept).Should().Be(HeaderConstants.MediaType);
296+
wrapper.Request.Headers.GetValue(HeaderNames.Accept).Should().Be(JsonApiMediaType.Default.ToString());
297297
wrapper.Request.Method.Should().Be(HttpMethod.Get);
298298
wrapper.Request.RequestUri.Should().Be($"{HostPrefix}flights/{flightId}/cabin-crew-members");
299299
wrapper.RequestBody.Should().BeNull();
@@ -313,7 +313,7 @@ public async Task Getting_ToOne_relationship_produces_expected_request()
313313

314314
// Assert
315315
wrapper.Request.ShouldNotBeNull();
316-
wrapper.Request.Headers.GetValue(HeaderNames.Accept).Should().Be(HeaderConstants.MediaType);
316+
wrapper.Request.Headers.GetValue(HeaderNames.Accept).Should().Be(JsonApiMediaType.Default.ToString());
317317
wrapper.Request.Method.Should().Be(HttpMethod.Get);
318318
wrapper.Request.RequestUri.Should().Be($"{HostPrefix}flights/{flightId}/relationships/purser");
319319
wrapper.RequestBody.Should().BeNull();
@@ -346,7 +346,7 @@ public async Task Patching_ToOne_relationship_produces_expected_request()
346346
wrapper.Request.RequestUri.Should().Be($"{HostPrefix}flights/{flightId}/relationships/purser");
347347
wrapper.Request.Content.Should().NotBeNull();
348348
wrapper.Request.Content!.Headers.ContentType.Should().NotBeNull();
349-
wrapper.Request.Content!.Headers.ContentType!.ToString().Should().Be(HeaderConstants.MediaType);
349+
wrapper.Request.Content!.Headers.ContentType!.ToString().Should().Be(JsonApiMediaType.Default.ToString());
350350

351351
wrapper.RequestBody.Should().BeJson("""
352352
{
@@ -372,7 +372,7 @@ public async Task Getting_ToMany_relationship_produces_expected_request()
372372

373373
// Assert
374374
wrapper.Request.ShouldNotBeNull();
375-
wrapper.Request.Headers.GetValue(HeaderNames.Accept).Should().Be(HeaderConstants.MediaType);
375+
wrapper.Request.Headers.GetValue(HeaderNames.Accept).Should().Be(JsonApiMediaType.Default.ToString());
376376
wrapper.Request.Method.Should().Be(HttpMethod.Get);
377377
wrapper.Request.RequestUri.Should().Be($"{HostPrefix}flights/{flightId}/relationships/cabin-crew-members");
378378
wrapper.RequestBody.Should().BeNull();
@@ -413,7 +413,7 @@ public async Task Posting_ToMany_relationship_produces_expected_request()
413413
wrapper.Request.RequestUri.Should().Be($"{HostPrefix}flights/{flightId}/relationships/cabin-crew-members");
414414
wrapper.Request.Content.Should().NotBeNull();
415415
wrapper.Request.Content!.Headers.ContentType.Should().NotBeNull();
416-
wrapper.Request.Content!.Headers.ContentType!.ToString().Should().Be(HeaderConstants.MediaType);
416+
wrapper.Request.Content!.Headers.ContentType!.ToString().Should().Be(JsonApiMediaType.Default.ToString());
417417

418418
wrapper.RequestBody.Should().BeJson("""
419419
{
@@ -466,7 +466,7 @@ public async Task Patching_ToMany_relationship_produces_expected_request()
466466
wrapper.Request.RequestUri.Should().Be($"{HostPrefix}flights/{flightId}/relationships/cabin-crew-members");
467467
wrapper.Request.Content.Should().NotBeNull();
468468
wrapper.Request.Content!.Headers.ContentType.Should().NotBeNull();
469-
wrapper.Request.Content!.Headers.ContentType!.ToString().Should().Be(HeaderConstants.MediaType);
469+
wrapper.Request.Content!.Headers.ContentType!.ToString().Should().Be(JsonApiMediaType.Default.ToString());
470470

471471
wrapper.RequestBody.Should().BeJson("""
472472
{
@@ -519,7 +519,7 @@ public async Task Deleting_ToMany_relationship_produces_expected_request()
519519
wrapper.Request.RequestUri.Should().Be($"{HostPrefix}flights/{flightId}/relationships/cabin-crew-members");
520520
wrapper.Request.Content.Should().NotBeNull();
521521
wrapper.Request.Content!.Headers.ContentType.Should().NotBeNull();
522-
wrapper.Request.Content!.Headers.ContentType!.ToString().Should().Be(HeaderConstants.MediaType);
522+
wrapper.Request.Content!.Headers.ContentType!.ToString().Should().Be(JsonApiMediaType.Default.ToString());
523523

524524
wrapper.RequestBody.Should().BeJson("""
525525
{

0 commit comments

Comments
 (0)