Skip to content

Amortise HTTP Result Content-Type arrays #56525

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ Microsoft.AspNetCore.Http.HttpResponse</Description>
<ItemGroup>
<InternalsVisibleTo Include="Microsoft.AspNetCore.Http.Abstractions.Microbenchmarks" />
<InternalsVisibleTo Include="Microsoft.AspNetCore.Http.Extensions" />
<InternalsVisibleTo Include="Microsoft.AspNetCore.Http.Results" />
</ItemGroup>

<ItemGroup>
Expand Down
2 changes: 1 addition & 1 deletion src/Http/Http.Results/src/AcceptedAtRouteOfT.cs
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,6 @@ static void IEndpointMetadataProvider.PopulateMetadata(MethodInfo method, Endpoi
ArgumentNullException.ThrowIfNull(method);
ArgumentNullException.ThrowIfNull(builder);

builder.Metadata.Add(new ProducesResponseTypeMetadata(StatusCodes.Status202Accepted, typeof(TValue), new[] { "application/json" }));
builder.Metadata.Add(ProducesResponseTypeMetadata.CreateUnvalidated(typeof(TValue), StatusCodes.Status202Accepted, HttpResultsHelper.ApplicationJsonContentTypes));
}
}
2 changes: 1 addition & 1 deletion src/Http/Http.Results/src/AcceptedOfT.cs
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,6 @@ static void IEndpointMetadataProvider.PopulateMetadata(MethodInfo method, Endpoi
ArgumentNullException.ThrowIfNull(method);
ArgumentNullException.ThrowIfNull(builder);

builder.Metadata.Add(new ProducesResponseTypeMetadata(StatusCodes.Status202Accepted, typeof(TValue), new[] { "application/json" }));
builder.Metadata.Add(ProducesResponseTypeMetadata.CreateUnvalidated(typeof(TValue), StatusCodes.Status202Accepted, HttpResultsHelper.ApplicationJsonContentTypes));
}
}
2 changes: 1 addition & 1 deletion src/Http/Http.Results/src/BadRequestOfT.cs
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,6 @@ static void IEndpointMetadataProvider.PopulateMetadata(MethodInfo method, Endpoi
ArgumentNullException.ThrowIfNull(method);
ArgumentNullException.ThrowIfNull(builder);

builder.Metadata.Add(new ProducesResponseTypeMetadata(StatusCodes.Status400BadRequest, typeof(TValue), new[] { "application/json" }));
builder.Metadata.Add(ProducesResponseTypeMetadata.CreateUnvalidated(typeof(TValue), StatusCodes.Status400BadRequest, HttpResultsHelper.ApplicationJsonContentTypes));
}
}
2 changes: 1 addition & 1 deletion src/Http/Http.Results/src/ConflictOfT.cs
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,6 @@ static void IEndpointMetadataProvider.PopulateMetadata(MethodInfo method, Endpoi
ArgumentNullException.ThrowIfNull(method);
ArgumentNullException.ThrowIfNull(builder);

builder.Metadata.Add(new ProducesResponseTypeMetadata(StatusCodes.Status409Conflict, typeof(TValue), new[] { "application/json" }));
builder.Metadata.Add(ProducesResponseTypeMetadata.CreateUnvalidated(typeof(TValue), StatusCodes.Status409Conflict, HttpResultsHelper.ApplicationJsonContentTypes));
}
}
2 changes: 1 addition & 1 deletion src/Http/Http.Results/src/CreatedAtRouteOfT.cs
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,6 @@ static void IEndpointMetadataProvider.PopulateMetadata(MethodInfo method, Endpoi
ArgumentNullException.ThrowIfNull(method);
ArgumentNullException.ThrowIfNull(builder);

builder.Metadata.Add(new ProducesResponseTypeMetadata(StatusCodes.Status201Created, typeof(TValue), new[] { "application/json" }));
builder.Metadata.Add(ProducesResponseTypeMetadata.CreateUnvalidated(typeof(TValue), StatusCodes.Status201Created, HttpResultsHelper.ApplicationJsonContentTypes));
}
}
2 changes: 1 addition & 1 deletion src/Http/Http.Results/src/CreatedOfT.cs
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,6 @@ static void IEndpointMetadataProvider.PopulateMetadata(MethodInfo method, Endpoi
ArgumentNullException.ThrowIfNull(method);
ArgumentNullException.ThrowIfNull(builder);

builder.Metadata.Add(new ProducesResponseTypeMetadata(StatusCodes.Status201Created, typeof(TValue), new[] { "application/json" }));
builder.Metadata.Add(ProducesResponseTypeMetadata.CreateUnvalidated(typeof(TValue), StatusCodes.Status201Created, HttpResultsHelper.ApplicationJsonContentTypes));
}
}
2 changes: 1 addition & 1 deletion src/Http/Http.Results/src/FileContentHttpResult.cs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ internal FileContentHttpResult(
{
FileContents = fileContents;
FileLength = fileContents.Length;
ContentType = contentType ?? "application/octet-stream";
ContentType = contentType ?? HttpResultsHelper.BinaryContentType;
FileDownloadName = fileDownloadName;
EnableRangeProcessing = enableRangeProcessing;
LastModified = lastModified;
Expand Down
2 changes: 1 addition & 1 deletion src/Http/Http.Results/src/FileStreamHttpResult.cs
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ internal FileStreamHttpResult(
FileLength = fileStream.Length;
}

ContentType = contentType ?? "application/octet-stream";
ContentType = contentType ?? HttpResultsHelper.BinaryContentType;
FileDownloadName = fileDownloadName;
EnableRangeProcessing = enableRangeProcessing;
LastModified = lastModified;
Expand Down
6 changes: 6 additions & 0 deletions src/Http/Http.Results/src/HttpResultsHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,13 @@ namespace Microsoft.AspNetCore.Http;

internal static partial class HttpResultsHelper
{
internal const string BinaryContentType = "application/octet-stream";
internal const string DefaultContentType = "text/plain; charset=utf-8";
internal const string ProblemDetailsContentType = "application/problem+json";

internal static IEnumerable<string> ApplicationJsonContentTypes { get; } = ["application/json"];
internal static IEnumerable<string> ProblemDetailsContentTypes { get; } = [ProblemDetailsContentType];

private static readonly Encoding DefaultEncoding = Encoding.UTF8;

[UnconditionalSuppressMessage("Trimming", "IL2026:RequiresUnreferencedCode",
Expand Down
2 changes: 1 addition & 1 deletion src/Http/Http.Results/src/InternalServerErrorOfT.cs
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,6 @@ static void IEndpointMetadataProvider.PopulateMetadata(MethodInfo method, Endpoi
ArgumentNullException.ThrowIfNull(method);
ArgumentNullException.ThrowIfNull(builder);

builder.Metadata.Add(new ProducesResponseTypeMetadata(StatusCodes.Status500InternalServerError, typeof(TValue), new[] { "application/json" }));
builder.Metadata.Add(ProducesResponseTypeMetadata.CreateUnvalidated(typeof(TValue), StatusCodes.Status500InternalServerError, HttpResultsHelper.ApplicationJsonContentTypes));
}
}
2 changes: 1 addition & 1 deletion src/Http/Http.Results/src/NotFoundOfT.cs
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,6 @@ static void IEndpointMetadataProvider.PopulateMetadata(MethodInfo method, Endpoi
ArgumentNullException.ThrowIfNull(method);
ArgumentNullException.ThrowIfNull(builder);

builder.Metadata.Add(new ProducesResponseTypeMetadata(StatusCodes.Status404NotFound, typeof(TValue), new[] { "application/json" }));
builder.Metadata.Add(ProducesResponseTypeMetadata.CreateUnvalidated(typeof(TValue), StatusCodes.Status404NotFound, HttpResultsHelper.ApplicationJsonContentTypes));
}
}
2 changes: 1 addition & 1 deletion src/Http/Http.Results/src/OkOfT.cs
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,6 @@ static void IEndpointMetadataProvider.PopulateMetadata(MethodInfo method, Endpoi
ArgumentNullException.ThrowIfNull(method);
ArgumentNullException.ThrowIfNull(builder);

builder.Metadata.Add(new ProducesResponseTypeMetadata(StatusCodes.Status200OK, typeof(TValue), new[] { "application/json" }));
builder.Metadata.Add(ProducesResponseTypeMetadata.CreateUnvalidated(typeof(TValue), StatusCodes.Status200OK, HttpResultsHelper.ApplicationJsonContentTypes));
}
}
2 changes: 1 addition & 1 deletion src/Http/Http.Results/src/PhysicalFileHttpResult.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ internal PhysicalFileHttpResult(
EntityTagHeaderValue? entityTag = null)
{
FileName = fileName;
ContentType = contentType ?? "application/octet-stream";
ContentType = contentType ?? HttpResultsHelper.BinaryContentType;
FileDownloadName = fileDownloadName;
EnableRangeProcessing = enableRangeProcessing;
LastModified = lastModified;
Expand Down
2 changes: 1 addition & 1 deletion src/Http/Http.Results/src/ProblemHttpResult.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ internal ProblemHttpResult(ProblemDetails problemDetails)
/// <summary>
/// Gets the value for the <c>Content-Type</c> header: <c>application/problem+json</c>
/// </summary>
public string ContentType => "application/problem+json";
public string ContentType => HttpResultsHelper.ProblemDetailsContentType;

/// <summary>
/// Gets the HTTP status code.
Expand Down
2 changes: 1 addition & 1 deletion src/Http/Http.Results/src/UnprocessableEntityOfT.cs
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,6 @@ static void IEndpointMetadataProvider.PopulateMetadata(MethodInfo method, Endpoi
ArgumentNullException.ThrowIfNull(method);
ArgumentNullException.ThrowIfNull(builder);

builder.Metadata.Add(new ProducesResponseTypeMetadata(StatusCodes.Status422UnprocessableEntity, typeof(TValue), new[] { "application/json" }));
builder.Metadata.Add(ProducesResponseTypeMetadata.CreateUnvalidated(typeof(TValue), StatusCodes.Status422UnprocessableEntity, HttpResultsHelper.ApplicationJsonContentTypes));
}
}
4 changes: 2 additions & 2 deletions src/Http/Http.Results/src/ValidationProblem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ internal ValidationProblem(HttpValidationProblemDetails problemDetails)
/// <summary>
/// Gets the value for the <c>Content-Type</c> header: <c>application/problem+json</c>.
/// </summary>
public string ContentType => "application/problem+json";
public string ContentType => HttpResultsHelper.ProblemDetailsContentType;

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

builder.Metadata.Add(new ProducesResponseTypeMetadata(StatusCodes.Status400BadRequest, typeof(HttpValidationProblemDetails), new[] { "application/problem+json" }));
builder.Metadata.Add(ProducesResponseTypeMetadata.CreateUnvalidated(typeof(HttpValidationProblemDetails), StatusCodes.Status400BadRequest, HttpResultsHelper.ProblemDetailsContentTypes));
}
}
2 changes: 1 addition & 1 deletion src/Http/Http.Results/src/VirtualFileHttpResult.cs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ internal VirtualFileHttpResult(
EntityTagHeaderValue? entityTag = null)
{
FileName = fileName;
ContentType = contentType ?? "application/octet-stream";
ContentType = contentType ?? HttpResultsHelper.BinaryContentType;
FileDownloadName = fileDownloadName;
EnableRangeProcessing = enableRangeProcessing;
LastModified = lastModified;
Expand Down
Loading