Skip to content

Commit a21c9ed

Browse files
authored
Add generic variants of ProducesProblem and ProducesValidationProblem (#56810)
* Add generic variants of ProducesProblem and ProducesValidationProblem * Centralize const strings for content types
1 parent 8b39e65 commit a21c9ed

30 files changed

+130
-77
lines changed

src/Http/Http.Extensions/src/HttpRequestJsonExtensions.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -283,7 +283,7 @@ private static bool HasJsonContentType(this HttpRequest request, out StringSegme
283283
}
284284

285285
// Matches application/json
286-
if (mt.MediaType.Equals(JsonConstants.JsonContentType, StringComparison.OrdinalIgnoreCase))
286+
if (mt.MediaType.Equals(ContentTypeConstants.JsonContentType, StringComparison.OrdinalIgnoreCase))
287287
{
288288
charset = mt.Charset;
289289
return true;

src/Http/Http.Extensions/src/HttpResponseJsonExtensions.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ public static Task WriteAsJsonAsync<TValue>(
8888

8989
options ??= ResolveSerializerOptions(response.HttpContext);
9090

91-
response.ContentType = contentType ?? JsonConstants.JsonContentTypeWithCharset;
91+
response.ContentType = contentType ?? ContentTypeConstants.JsonContentTypeWithCharset;
9292

9393
var startTask = Task.CompletedTask;
9494
if (!response.HasStarted)
@@ -129,7 +129,7 @@ public static Task WriteAsJsonAsync<TValue>(
129129
{
130130
ArgumentNullException.ThrowIfNull(response);
131131

132-
response.ContentType = contentType ?? JsonConstants.JsonContentTypeWithCharset;
132+
response.ContentType = contentType ?? ContentTypeConstants.JsonContentTypeWithCharset;
133133

134134
var startTask = Task.CompletedTask;
135135
if (!response.HasStarted)
@@ -182,7 +182,7 @@ public static Task WriteAsJsonAsync(
182182
{
183183
ArgumentNullException.ThrowIfNull(response);
184184

185-
response.ContentType = contentType ?? JsonConstants.JsonContentTypeWithCharset;
185+
response.ContentType = contentType ?? ContentTypeConstants.JsonContentTypeWithCharset;
186186

187187
var startTask = Task.CompletedTask;
188188
if (!response.HasStarted)
@@ -302,7 +302,7 @@ public static Task WriteAsJsonAsync(
302302

303303
options ??= ResolveSerializerOptions(response.HttpContext);
304304

305-
response.ContentType = contentType ?? JsonConstants.JsonContentTypeWithCharset;
305+
response.ContentType = contentType ?? ContentTypeConstants.JsonContentTypeWithCharset;
306306

307307
var startTask = Task.CompletedTask;
308308
if (!response.HasStarted)
@@ -365,7 +365,7 @@ public static Task WriteAsJsonAsync(
365365
ArgumentNullException.ThrowIfNull(type);
366366
ArgumentNullException.ThrowIfNull(context);
367367

368-
response.ContentType = contentType ?? JsonConstants.JsonContentTypeWithCharset;
368+
response.ContentType = contentType ?? ContentTypeConstants.JsonContentTypeWithCharset;
369369

370370
var startTask = Task.CompletedTask;
371371
if (!response.HasStarted)

src/Http/Http.Extensions/src/JsonConstants.cs

Lines changed: 0 additions & 10 deletions
This file was deleted.

src/Http/Http.Extensions/src/Microsoft.AspNetCore.Http.Extensions.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
<Compile Remove="$(RepoRoot)src\Components\Endpoints\src\FormMapping\HttpContextFormValueMapper.cs" LinkBase="SharedFormMapping" />
3636
<Compile Remove="$(RepoRoot)src\Components\Endpoints\src\FormMapping\HttpContextFormDataProvider.cs" LinkBase="SharedFormMapping" />
3737
<Compile Remove="$(RepoRoot)src\Components\Endpoints\src\FormMapping\BrowserFileFromFormFile.cs" LinkBase="SharedFormMapping" />
38+
<Compile Include="$(SharedSourceRoot)ContentTypeConstants.cs" LinkBase="Shared" />
3839
</ItemGroup>
3940

4041
<ItemGroup>

src/Http/Http.Extensions/src/RequestDelegateFactory.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ public static partial class RequestDelegateFactory
126126
private static readonly MethodInfo AsMemoryMethod = new Func<char[]?, int, int, Memory<char>>(MemoryExtensions.AsMemory).Method;
127127
private static readonly MethodInfo ArrayPoolSharedReturnMethod = typeof(ArrayPool<char>).GetMethod(nameof(ArrayPool<char>.Shared.Return))!;
128128

129-
private static readonly string[] DefaultAcceptsAndProducesContentType = new[] { JsonConstants.JsonContentType };
129+
private static readonly string[] DefaultAcceptsAndProducesContentType = new[] { ContentTypeConstants.JsonContentType };
130130
private static readonly string[] FormFileContentType = new[] { "multipart/form-data" };
131131
private static readonly string[] FormContentType = new[] { "multipart/form-data", "application/x-www-form-urlencoded" };
132132
private static readonly string[] PlaintextContentType = new[] { "text/plain" };

src/Http/Http.Extensions/test/HttpResponseJsonExtensionsTests.cs

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ public async Task WriteAsJsonAsyncGeneric_SimpleValue_JsonResponse()
2626
await context.Response.WriteAsJsonAsync(1);
2727

2828
// Assert
29-
Assert.Equal(JsonConstants.JsonContentTypeWithCharset, context.Response.ContentType);
29+
Assert.Equal(ContentTypeConstants.JsonContentTypeWithCharset, context.Response.ContentType);
3030
Assert.Equal(StatusCodes.Status200OK, context.Response.StatusCode);
3131

3232
var data = body.ToArray();
@@ -45,7 +45,7 @@ public async Task WriteAsJsonAsyncGeneric_NullValue_JsonResponse()
4545
await context.Response.WriteAsJsonAsync<Uri?>(value: null);
4646

4747
// Assert
48-
Assert.Equal(JsonConstants.JsonContentTypeWithCharset, context.Response.ContentType);
48+
Assert.Equal(ContentTypeConstants.JsonContentTypeWithCharset, context.Response.ContentType);
4949

5050
var data = Encoding.UTF8.GetString(body.ToArray());
5151
Assert.Equal("null", data);
@@ -65,7 +65,7 @@ public async Task WriteAsJsonAsyncGeneric_WithOptions_JsonResponse()
6565
await context.Response.WriteAsJsonAsync(new int[] { 1, 2, 3 }, options);
6666

6767
// Assert
68-
Assert.Equal(JsonConstants.JsonContentTypeWithCharset, context.Response.ContentType);
68+
Assert.Equal(ContentTypeConstants.JsonContentTypeWithCharset, context.Response.ContentType);
6969

7070
var data = Encoding.UTF8.GetString(body.ToArray());
7171
Assert.Equal("[false,true,false]", data);
@@ -97,7 +97,7 @@ public async Task WriteAsJsonAsyncGeneric_CustomStatusCode_StatusCodeUnchanged()
9797
await context.Response.WriteAsJsonAsync(1);
9898

9999
// Assert
100-
Assert.Equal(JsonConstants.JsonContentTypeWithCharset, context.Response.ContentType);
100+
Assert.Equal(ContentTypeConstants.JsonContentTypeWithCharset, context.Response.ContentType);
101101
Assert.Equal(StatusCodes.Status418ImATeapot, context.Response.StatusCode);
102102
}
103103

@@ -167,7 +167,7 @@ public async Task WriteAsJsonAsync_SimpleValue_JsonResponse()
167167
await context.Response.WriteAsJsonAsync(1, typeof(int));
168168

169169
// Assert
170-
Assert.Equal(JsonConstants.JsonContentTypeWithCharset, context.Response.ContentType);
170+
Assert.Equal(ContentTypeConstants.JsonContentTypeWithCharset, context.Response.ContentType);
171171
Assert.Equal(StatusCodes.Status200OK, context.Response.StatusCode);
172172

173173
var data = body.ToArray();
@@ -186,7 +186,7 @@ public async Task WriteAsJsonAsync_NullValue_JsonResponse()
186186
await context.Response.WriteAsJsonAsync(value: null, typeof(int?));
187187

188188
// Assert
189-
Assert.Equal(JsonConstants.JsonContentTypeWithCharset, context.Response.ContentType);
189+
Assert.Equal(ContentTypeConstants.JsonContentTypeWithCharset, context.Response.ContentType);
190190

191191
var data = Encoding.UTF8.GetString(body.ToArray());
192192
Assert.Equal("null", data);
@@ -249,7 +249,7 @@ public async Task WriteAsJsonAsync_CustomStatusCode_StatusCodeUnchanged()
249249
await context.Response.WriteAsJsonAsync(1, typeof(int));
250250

251251
// Assert
252-
Assert.Equal(JsonConstants.JsonContentTypeWithCharset, context.Response.ContentType);
252+
Assert.Equal(ContentTypeConstants.JsonContentTypeWithCharset, context.Response.ContentType);
253253
Assert.Equal(StatusCodes.Status418ImATeapot, context.Response.StatusCode);
254254
}
255255

@@ -265,7 +265,7 @@ public async Task WriteAsJsonAsyncGeneric_AsyncEnumerable()
265265
await context.Response.WriteAsJsonAsync(AsyncEnumerable());
266266

267267
// Assert
268-
Assert.Equal(JsonConstants.JsonContentTypeWithCharset, context.Response.ContentType);
268+
Assert.Equal(ContentTypeConstants.JsonContentTypeWithCharset, context.Response.ContentType);
269269
Assert.Equal(StatusCodes.Status200OK, context.Response.StatusCode);
270270

271271
Assert.Equal("[1,2]", Encoding.UTF8.GetString(body.ToArray()));
@@ -290,7 +290,7 @@ public async Task WriteAsJsonAsync_AsyncEnumerable()
290290
await context.Response.WriteAsJsonAsync(AsyncEnumerable(), typeof(IAsyncEnumerable<int>));
291291

292292
// Assert
293-
Assert.Equal(JsonConstants.JsonContentTypeWithCharset, context.Response.ContentType);
293+
Assert.Equal(ContentTypeConstants.JsonContentTypeWithCharset, context.Response.ContentType);
294294
Assert.Equal(StatusCodes.Status200OK, context.Response.StatusCode);
295295

296296
Assert.Equal("[1,2]", Encoding.UTF8.GetString(body.ToArray()));
@@ -318,7 +318,7 @@ public async Task WriteAsJsonAsyncGeneric_AsyncEnumerable_ClosedConnecton()
318318
await context.Response.WriteAsJsonAsync(AsyncEnumerable());
319319

320320
// Assert
321-
Assert.Equal(JsonConstants.JsonContentTypeWithCharset, context.Response.ContentType);
321+
Assert.Equal(ContentTypeConstants.JsonContentTypeWithCharset, context.Response.ContentType);
322322
Assert.Equal(StatusCodes.Status200OK, context.Response.StatusCode);
323323

324324
// System.Text.Json might write the '[' before cancellation is observed
@@ -352,7 +352,7 @@ public async Task WriteAsJsonAsync_AsyncEnumerable_ClosedConnecton()
352352
await context.Response.WriteAsJsonAsync(AsyncEnumerable(), typeof(IAsyncEnumerable<int>));
353353

354354
// Assert
355-
Assert.Equal(JsonConstants.JsonContentTypeWithCharset, context.Response.ContentType);
355+
Assert.Equal(ContentTypeConstants.JsonContentTypeWithCharset, context.Response.ContentType);
356356
Assert.Equal(StatusCodes.Status200OK, context.Response.StatusCode);
357357

358358
// System.Text.Json might write the '[' before cancellation is observed
@@ -386,7 +386,7 @@ public async Task WriteAsJsonAsync_AsyncEnumerable_UserPassedTokenThrows()
386386
await Assert.ThrowsAnyAsync<OperationCanceledException>(() => context.Response.WriteAsJsonAsync(AsyncEnumerable(), typeof(IAsyncEnumerable<int>), cts.Token));
387387

388388
// Assert
389-
Assert.Equal(JsonConstants.JsonContentTypeWithCharset, context.Response.ContentType);
389+
Assert.Equal(ContentTypeConstants.JsonContentTypeWithCharset, context.Response.ContentType);
390390
Assert.Equal(StatusCodes.Status200OK, context.Response.StatusCode);
391391

392392
// System.Text.Json might write the '[' before cancellation is observed
@@ -420,7 +420,7 @@ public async Task WriteAsJsonAsyncGeneric_AsyncEnumerable_UserPassedTokenThrows(
420420
await Assert.ThrowsAnyAsync<OperationCanceledException>(() => context.Response.WriteAsJsonAsync(AsyncEnumerable(), cts.Token));
421421

422422
// Assert
423-
Assert.Equal(JsonConstants.JsonContentTypeWithCharset, context.Response.ContentType);
423+
Assert.Equal(ContentTypeConstants.JsonContentTypeWithCharset, context.Response.ContentType);
424424
Assert.Equal(StatusCodes.Status200OK, context.Response.StatusCode);
425425

426426
// System.Text.Json might write the '[' before cancellation is observed
@@ -454,7 +454,7 @@ public async Task WriteAsJsonAsyncGeneric_WithJsonTypeInfo_JsonResponse()
454454
await context.Response.WriteAsJsonAsync(new int[] { 1, 2, 3 }, (JsonTypeInfo<int[]>)options.GetTypeInfo(typeof(int[])));
455455

456456
// Assert
457-
Assert.Equal(JsonConstants.JsonContentTypeWithCharset, context.Response.ContentType);
457+
Assert.Equal(ContentTypeConstants.JsonContentTypeWithCharset, context.Response.ContentType);
458458

459459
var data = Encoding.UTF8.GetString(body.ToArray());
460460
Assert.Equal("[1,2,3]", data);
@@ -475,7 +475,7 @@ public async Task WriteAsJsonAsync_NullValue_WithJsonTypeInfo_JsonResponse()
475475
await context.Response.WriteAsJsonAsync(value : null, options.GetTypeInfo(typeof(Uri)));
476476

477477
// Assert
478-
Assert.Equal(JsonConstants.JsonContentTypeWithCharset, context.Response.ContentType);
478+
Assert.Equal(ContentTypeConstants.JsonContentTypeWithCharset, context.Response.ContentType);
479479

480480
var data = Encoding.UTF8.GetString(body.ToArray());
481481
Assert.Equal("null", data);

src/Http/Http.Results/src/AcceptedAtRouteOfT.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,6 @@ static void IEndpointMetadataProvider.PopulateMetadata(MethodInfo method, Endpoi
122122
ArgumentNullException.ThrowIfNull(method);
123123
ArgumentNullException.ThrowIfNull(builder);
124124

125-
builder.Metadata.Add(ProducesResponseTypeMetadata.CreateUnvalidated(typeof(TValue), StatusCodes.Status202Accepted, HttpResultsHelper.ApplicationJsonContentTypes));
125+
builder.Metadata.Add(ProducesResponseTypeMetadata.CreateUnvalidated(typeof(TValue), StatusCodes.Status202Accepted, ContentTypeConstants.ApplicationJsonContentTypes));
126126
}
127127
}

src/Http/Http.Results/src/AcceptedOfT.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,6 @@ static void IEndpointMetadataProvider.PopulateMetadata(MethodInfo method, Endpoi
100100
ArgumentNullException.ThrowIfNull(method);
101101
ArgumentNullException.ThrowIfNull(builder);
102102

103-
builder.Metadata.Add(ProducesResponseTypeMetadata.CreateUnvalidated(typeof(TValue), StatusCodes.Status202Accepted, HttpResultsHelper.ApplicationJsonContentTypes));
103+
builder.Metadata.Add(ProducesResponseTypeMetadata.CreateUnvalidated(typeof(TValue), StatusCodes.Status202Accepted, ContentTypeConstants.ApplicationJsonContentTypes));
104104
}
105105
}

src/Http/Http.Results/src/BadRequestOfT.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,6 @@ static void IEndpointMetadataProvider.PopulateMetadata(MethodInfo method, Endpoi
6565
ArgumentNullException.ThrowIfNull(method);
6666
ArgumentNullException.ThrowIfNull(builder);
6767

68-
builder.Metadata.Add(ProducesResponseTypeMetadata.CreateUnvalidated(typeof(TValue), StatusCodes.Status400BadRequest, HttpResultsHelper.ApplicationJsonContentTypes));
68+
builder.Metadata.Add(ProducesResponseTypeMetadata.CreateUnvalidated(typeof(TValue), StatusCodes.Status400BadRequest, ContentTypeConstants.ApplicationJsonContentTypes));
6969
}
7070
}

src/Http/Http.Results/src/ConflictOfT.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,6 @@ static void IEndpointMetadataProvider.PopulateMetadata(MethodInfo method, Endpoi
6565
ArgumentNullException.ThrowIfNull(method);
6666
ArgumentNullException.ThrowIfNull(builder);
6767

68-
builder.Metadata.Add(ProducesResponseTypeMetadata.CreateUnvalidated(typeof(TValue), StatusCodes.Status409Conflict, HttpResultsHelper.ApplicationJsonContentTypes));
68+
builder.Metadata.Add(ProducesResponseTypeMetadata.CreateUnvalidated(typeof(TValue), StatusCodes.Status409Conflict, ContentTypeConstants.ApplicationJsonContentTypes));
6969
}
7070
}

0 commit comments

Comments
 (0)