Skip to content

Commit 432cd04

Browse files
Amortise Content-Type arrays
- Amortise arrays used for `TypedResults`' metadata. - Use `ProducesResponseTypeMetadata.CreateUnvalidated` to bypass `Content-Type` validation for static values. - Add and use constant for `application/octet-stream`.
1 parent 02c0cd6 commit 432cd04

18 files changed

+24
-17
lines changed

src/Http/Http.Abstractions/src/Microsoft.AspNetCore.Http.Abstractions.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ Microsoft.AspNetCore.Http.HttpResponse</Description>
3737
<ItemGroup>
3838
<InternalsVisibleTo Include="Microsoft.AspNetCore.Http.Abstractions.Microbenchmarks" />
3939
<InternalsVisibleTo Include="Microsoft.AspNetCore.Http.Extensions" />
40+
<InternalsVisibleTo Include="Microsoft.AspNetCore.Http.Results" />
4041
</ItemGroup>
4142

4243
<ItemGroup>

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(new ProducesResponseTypeMetadata(StatusCodes.Status202Accepted, typeof(TValue), new[] { "application/json" }));
125+
builder.Metadata.Add(ProducesResponseTypeMetadata.CreateUnvalidated(typeof(TValue), StatusCodes.Status202Accepted, HttpResultsHelper.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(new ProducesResponseTypeMetadata(StatusCodes.Status202Accepted, typeof(TValue), new[] { "application/json" }));
103+
builder.Metadata.Add(ProducesResponseTypeMetadata.CreateUnvalidated(typeof(TValue), StatusCodes.Status202Accepted, HttpResultsHelper.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(new ProducesResponseTypeMetadata(StatusCodes.Status400BadRequest, typeof(TValue), new[] { "application/json" }));
68+
builder.Metadata.Add(ProducesResponseTypeMetadata.CreateUnvalidated(typeof(TValue), StatusCodes.Status400BadRequest, HttpResultsHelper.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(new ProducesResponseTypeMetadata(StatusCodes.Status409Conflict, typeof(TValue), new[] { "application/json" }));
68+
builder.Metadata.Add(ProducesResponseTypeMetadata.CreateUnvalidated(typeof(TValue), StatusCodes.Status409Conflict, HttpResultsHelper.ApplicationJsonContentTypes));
6969
}
7070
}

src/Http/Http.Results/src/CreatedAtRouteOfT.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,6 @@ static void IEndpointMetadataProvider.PopulateMetadata(MethodInfo method, Endpoi
125125
ArgumentNullException.ThrowIfNull(method);
126126
ArgumentNullException.ThrowIfNull(builder);
127127

128-
builder.Metadata.Add(new ProducesResponseTypeMetadata(StatusCodes.Status201Created, typeof(TValue), new[] { "application/json" }));
128+
builder.Metadata.Add(ProducesResponseTypeMetadata.CreateUnvalidated(typeof(TValue), StatusCodes.Status201Created, HttpResultsHelper.ApplicationJsonContentTypes));
129129
}
130130
}

src/Http/Http.Results/src/CreatedOfT.cs

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

102-
builder.Metadata.Add(new ProducesResponseTypeMetadata(StatusCodes.Status201Created, typeof(TValue), new[] { "application/json" }));
102+
builder.Metadata.Add(ProducesResponseTypeMetadata.CreateUnvalidated(typeof(TValue), StatusCodes.Status201Created, HttpResultsHelper.ApplicationJsonContentTypes));
103103
}
104104
}

src/Http/Http.Results/src/FileContentHttpResult.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ internal FileContentHttpResult(
6161
{
6262
FileContents = fileContents;
6363
FileLength = fileContents.Length;
64-
ContentType = contentType ?? "application/octet-stream";
64+
ContentType = contentType ?? HttpResultsHelper.BinaryContentType;
6565
FileDownloadName = fileDownloadName;
6666
EnableRangeProcessing = enableRangeProcessing;
6767
LastModified = lastModified;

src/Http/Http.Results/src/FileStreamHttpResult.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ internal FileStreamHttpResult(
6767
FileLength = fileStream.Length;
6868
}
6969

70-
ContentType = contentType ?? "application/octet-stream";
70+
ContentType = contentType ?? HttpResultsHelper.BinaryContentType;
7171
FileDownloadName = fileDownloadName;
7272
EnableRangeProcessing = enableRangeProcessing;
7373
LastModified = lastModified;

src/Http/Http.Results/src/HttpResultsHelper.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,13 @@ namespace Microsoft.AspNetCore.Http;
1717

1818
internal static partial class HttpResultsHelper
1919
{
20+
internal const string BinaryContentType = "application/octet-stream";
2021
internal const string DefaultContentType = "text/plain; charset=utf-8";
22+
internal const string ProblemDetailsContentType = "application/problem+json";
23+
24+
internal static IEnumerable<string> ApplicationJsonContentTypes { get; } = ["application/json"];
25+
internal static IEnumerable<string> ProblemDetailsContentTypes { get; } = [ProblemDetailsContentType];
26+
2127
private static readonly Encoding DefaultEncoding = Encoding.UTF8;
2228

2329
[UnconditionalSuppressMessage("Trimming", "IL2026:RequiresUnreferencedCode",

src/Http/Http.Results/src/InternalServerErrorOfT.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(new ProducesResponseTypeMetadata(StatusCodes.Status500InternalServerError, typeof(TValue), new[] { "application/json" }));
68+
builder.Metadata.Add(ProducesResponseTypeMetadata.CreateUnvalidated(typeof(TValue), StatusCodes.Status500InternalServerError, HttpResultsHelper.ApplicationJsonContentTypes));
6969
}
7070
}

src/Http/Http.Results/src/NotFoundOfT.cs

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

67-
builder.Metadata.Add(new ProducesResponseTypeMetadata(StatusCodes.Status404NotFound, typeof(TValue), new[] { "application/json" }));
67+
builder.Metadata.Add(ProducesResponseTypeMetadata.CreateUnvalidated(typeof(TValue), StatusCodes.Status404NotFound, HttpResultsHelper.ApplicationJsonContentTypes));
6868
}
6969
}

src/Http/Http.Results/src/OkOfT.cs

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

67-
builder.Metadata.Add(new ProducesResponseTypeMetadata(StatusCodes.Status200OK, typeof(TValue), new[] { "application/json" }));
67+
builder.Metadata.Add(ProducesResponseTypeMetadata.CreateUnvalidated(typeof(TValue), StatusCodes.Status200OK, HttpResultsHelper.ApplicationJsonContentTypes));
6868
}
6969
}

src/Http/Http.Results/src/PhysicalFileHttpResult.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ internal PhysicalFileHttpResult(
5858
EntityTagHeaderValue? entityTag = null)
5959
{
6060
FileName = fileName;
61-
ContentType = contentType ?? "application/octet-stream";
61+
ContentType = contentType ?? HttpResultsHelper.BinaryContentType;
6262
FileDownloadName = fileDownloadName;
6363
EnableRangeProcessing = enableRangeProcessing;
6464
LastModified = lastModified;

src/Http/Http.Results/src/ProblemHttpResult.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ internal ProblemHttpResult(ProblemDetails problemDetails)
3636
/// <summary>
3737
/// Gets the value for the <c>Content-Type</c> header: <c>application/problem+json</c>
3838
/// </summary>
39-
public string ContentType => "application/problem+json";
39+
public string ContentType => HttpResultsHelper.ProblemDetailsContentType;
4040

4141
/// <summary>
4242
/// Gets the HTTP status code.

src/Http/Http.Results/src/UnprocessableEntityOfT.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(new ProducesResponseTypeMetadata(StatusCodes.Status422UnprocessableEntity, typeof(TValue), new[] { "application/json" }));
68+
builder.Metadata.Add(ProducesResponseTypeMetadata.CreateUnvalidated(typeof(TValue), StatusCodes.Status422UnprocessableEntity, HttpResultsHelper.ApplicationJsonContentTypes));
6969
}
7070
}

src/Http/Http.Results/src/ValidationProblem.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ internal ValidationProblem(HttpValidationProblemDetails problemDetails)
3939
/// <summary>
4040
/// Gets the value for the <c>Content-Type</c> header: <c>application/problem+json</c>.
4141
/// </summary>
42-
public string ContentType => "application/problem+json";
42+
public string ContentType => HttpResultsHelper.ProblemDetailsContentType;
4343

4444
/// <summary>
4545
/// Gets the HTTP status code: <see cref="StatusCodes.Status400BadRequest"/>
@@ -76,6 +76,6 @@ static void IEndpointMetadataProvider.PopulateMetadata(MethodInfo method, Endpoi
7676
ArgumentNullException.ThrowIfNull(method);
7777
ArgumentNullException.ThrowIfNull(builder);
7878

79-
builder.Metadata.Add(new ProducesResponseTypeMetadata(StatusCodes.Status400BadRequest, typeof(HttpValidationProblemDetails), new[] { "application/problem+json" }));
79+
builder.Metadata.Add(ProducesResponseTypeMetadata.CreateUnvalidated(typeof(HttpValidationProblemDetails), StatusCodes.Status400BadRequest, HttpResultsHelper.ProblemDetailsContentTypes));
8080
}
8181
}

src/Http/Http.Results/src/VirtualFileHttpResult.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ internal VirtualFileHttpResult(
6363
EntityTagHeaderValue? entityTag = null)
6464
{
6565
FileName = fileName;
66-
ContentType = contentType ?? "application/octet-stream";
66+
ContentType = contentType ?? HttpResultsHelper.BinaryContentType;
6767
FileDownloadName = fileDownloadName;
6868
EnableRangeProcessing = enableRangeProcessing;
6969
LastModified = lastModified;

0 commit comments

Comments
 (0)