Skip to content

Commit bd50a68

Browse files
Amortise Content-Type arrays (#56525)
- 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 fce2441 commit bd50a68

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",

0 commit comments

Comments
 (0)