Download aspnetcore_1/solution/solution.patch from lolbench26/LoLBench: direct link, hf CLI and curl.
- Browser
- Download file 36.5 kB
-
https://huggingface.co/datasets/lolbench26/LoLBench/resolve/main/aspnetcore_1/solution/solution.patch
- Command line
-
hf download hf://datasets/lolbench26/LoLBench/aspnetcore_1/solution/solution.patch
-
curl -L -o solution.patch https://huggingface.co/datasets/lolbench26/LoLBench/resolve/main/aspnetcore_1/solution/solution.patch
36.5 kB
| diff --git a/src/Http/Http.Abstractions/src/Metadata/ApiEndpointMetadata.cs b/src/Http/Http.Abstractions/src/Metadata/ApiEndpointMetadata.cs | |
| new file mode 100644 | |
| index 00000000..815bf2c8 | |
| --- /dev/null | |
| +++ b/src/Http/Http.Abstractions/src/Metadata/ApiEndpointMetadata.cs | |
| +// Licensed to the .NET Foundation under one or more agreements. | |
| +// The .NET Foundation licenses this file to you under the MIT license. | |
| + | |
| +namespace Microsoft.AspNetCore.Http.Metadata; | |
| + | |
| +/// <summary> | |
| +/// Metadata that indicates the endpoint is intended for API clients. | |
| +/// When present, authentication handlers should prefer returning status codes over browser redirects. | |
| +/// </summary> | |
| +internal sealed class ApiEndpointMetadata : IApiEndpointMetadata | |
| +{ | |
| + /// <summary> | |
| + /// Singleton instance of <see cref="ApiEndpointMetadata"/>. | |
| + /// </summary> | |
| + public static readonly ApiEndpointMetadata Instance = new(); | |
| + | |
| + private ApiEndpointMetadata() | |
| + { | |
| + } | |
| +} | |
| diff --git a/src/Http/Http.Abstractions/src/Metadata/IApiEndpointMetadata.cs b/src/Http/Http.Abstractions/src/Metadata/IApiEndpointMetadata.cs | |
| new file mode 100644 | |
| index 00000000..cadaafca | |
| --- /dev/null | |
| +++ b/src/Http/Http.Abstractions/src/Metadata/IApiEndpointMetadata.cs | |
| +// Licensed to the .NET Foundation under one or more agreements. | |
| +// The .NET Foundation licenses this file to you under the MIT license. | |
| + | |
| +namespace Microsoft.AspNetCore.Http.Metadata; | |
| + | |
| +/// <summary> | |
| +/// Metadata that indicates the endpoint is an API intended for programmatic access rather than direct browser navigation. | |
| +/// When present, authentication handlers should prefer returning status codes over browser redirects. | |
| +/// </summary> | |
| +public interface IApiEndpointMetadata | |
| +{ | |
| +} | |
| diff --git a/src/Http/Http.Abstractions/src/Microsoft.AspNetCore.Http.Abstractions.csproj b/src/Http/Http.Abstractions/src/Microsoft.AspNetCore.Http.Abstractions.csproj | |
| index d383efa6..37e17a0f 100644 | |
| --- a/src/Http/Http.Abstractions/src/Microsoft.AspNetCore.Http.Abstractions.csproj | |
| +++ b/src/Http/Http.Abstractions/src/Microsoft.AspNetCore.Http.Abstractions.csproj | |
| Microsoft.AspNetCore.Http.HttpResponse</Description> | |
| </ItemGroup> | |
| <ItemGroup> | |
| - <InternalsVisibleTo Include="Microsoft.AspNetCore.Http.Abstractions.Microbenchmarks" /> | |
| <InternalsVisibleTo Include="Microsoft.AspNetCore.Http.Extensions" /> | |
| <InternalsVisibleTo Include="Microsoft.AspNetCore.Http.Results" /> | |
| </ItemGroup> | |
| Microsoft.AspNetCore.Http.HttpResponse</Description> | |
| <ItemGroup> | |
| <InternalsVisibleTo Include="Microsoft.AspNetCore.Http.Abstractions.Tests" /> | |
| + <InternalsVisibleTo Include="Microsoft.AspNetCore.Http.Abstractions.Microbenchmarks" /> | |
| </ItemGroup> | |
| </Project> | |
| diff --git a/src/Http/Http.Abstractions/src/PublicAPI.Unshipped.txt b/src/Http/Http.Abstractions/src/PublicAPI.Unshipped.txt | |
| index 3a246130..2944a853 100644 | |
| --- a/src/Http/Http.Abstractions/src/PublicAPI.Unshipped.txt | |
| +++ b/src/Http/Http.Abstractions/src/PublicAPI.Unshipped.txt | |
| #nullable enable | |
| +Microsoft.AspNetCore.Http.Metadata.IApiEndpointMetadata | |
| Microsoft.AspNetCore.Http.Metadata.IDisableValidationMetadata | |
| Microsoft.AspNetCore.Http.ProducesResponseTypeMetadata.Description.get -> string? | |
| Microsoft.AspNetCore.Http.ProducesResponseTypeMetadata.Description.set -> void | |
| diff --git a/src/Http/Http.Extensions/gen/Microsoft.AspNetCore.Http.RequestDelegateGenerator/RequestDelegateGenerator.cs b/src/Http/Http.Extensions/gen/Microsoft.AspNetCore.Http.RequestDelegateGenerator/RequestDelegateGenerator.cs | |
| index f4e2ad0e..f5e2abac 100644 | |
| --- a/src/Http/Http.Extensions/gen/Microsoft.AspNetCore.Http.RequestDelegateGenerator/RequestDelegateGenerator.cs | |
| +++ b/src/Http/Http.Extensions/gen/Microsoft.AspNetCore.Http.RequestDelegateGenerator/RequestDelegateGenerator.cs | |
| public sealed class RequestDelegateGenerator : IIncrementalGenerator | |
| if (hasFormBody) | |
| { | |
| - codeWriter.WriteLine(RequestDelegateGeneratorSources.AntiforgeryMetadataType); | |
| + codeWriter.WriteLine(RequestDelegateGeneratorSources.AntiforgeryMetadataClass); | |
| + } | |
| + | |
| + if (hasJsonBody || hasResponseMetadata) | |
| + { | |
| + codeWriter.WriteLine(RequestDelegateGeneratorSources.ApiEndpointMetadataClass); | |
| } | |
| if (hasFormBody || hasJsonBody || hasResponseMetadata) | |
| diff --git a/src/Http/Http.Extensions/gen/Microsoft.AspNetCore.Http.RequestDelegateGenerator/RequestDelegateGeneratorSources.cs b/src/Http/Http.Extensions/gen/Microsoft.AspNetCore.Http.RequestDelegateGenerator/RequestDelegateGeneratorSources.cs | |
| index d7c7a480..b626db27 100644 | |
| --- a/src/Http/Http.Extensions/gen/Microsoft.AspNetCore.Http.RequestDelegateGenerator/RequestDelegateGeneratorSources.cs | |
| +++ b/src/Http/Http.Extensions/gen/Microsoft.AspNetCore.Http.RequestDelegateGenerator/RequestDelegateGeneratorSources.cs | |
| internal static class RequestDelegateGeneratorSources | |
| } | |
| """; | |
| - public static string AntiforgeryMetadataType = """ | |
| -file sealed class AntiforgeryMetadata : IAntiforgeryMetadata | |
| -{ | |
| - public static readonly IAntiforgeryMetadata ValidationRequired = new AntiforgeryMetadata(true); | |
| - | |
| - public AntiforgeryMetadata(bool requiresValidation) | |
| + public static string AntiforgeryMetadataClass = """ | |
| + file sealed class AntiforgeryMetadata : IAntiforgeryMetadata | |
| { | |
| - RequiresValidation = requiresValidation; | |
| + public static readonly IAntiforgeryMetadata ValidationRequired = new AntiforgeryMetadata(true); | |
| + | |
| + public AntiforgeryMetadata(bool requiresValidation) | |
| + { | |
| + RequiresValidation = requiresValidation; | |
| + } | |
| + | |
| + public bool RequiresValidation { get; } | |
| } | |
| +"""; | |
| - public bool RequiresValidation { get; } | |
| -} | |
| + public static string ApiEndpointMetadataClass = """ | |
| + file sealed class ApiEndpointMetadata : IApiEndpointMetadata | |
| + { | |
| + public static readonly ApiEndpointMetadata Instance = new(); | |
| + | |
| + private ApiEndpointMetadata() | |
| + { | |
| + } | |
| + | |
| + public static void AddApiEndpointMetadataIfMissing(EndpointBuilder builder) | |
| + { | |
| + if (!builder.Metadata.Any(m => m is IApiEndpointMetadata)) | |
| + { | |
| + builder.Metadata.Add(Instance); | |
| + } | |
| + } | |
| + } | |
| """; | |
| + | |
| public static string GetGeneratedRouteBuilderExtensionsSource(string endpoints, string helperMethods, string helperTypes, ImmutableHashSet<string> verbs) => $$""" | |
| {{SourceHeader}} | |
| diff --git a/src/Http/Http.Extensions/gen/Microsoft.AspNetCore.Http.RequestDelegateGenerator/StaticRouteHandlerModel/StaticRouteHandlerModel.Emitter.cs b/src/Http/Http.Extensions/gen/Microsoft.AspNetCore.Http.RequestDelegateGenerator/StaticRouteHandlerModel/StaticRouteHandlerModel.Emitter.cs | |
| index 83790d44..97fe2dc9 100644 | |
| --- a/src/Http/Http.Extensions/gen/Microsoft.AspNetCore.Http.RequestDelegateGenerator/StaticRouteHandlerModel/StaticRouteHandlerModel.Emitter.cs | |
| +++ b/src/Http/Http.Extensions/gen/Microsoft.AspNetCore.Http.RequestDelegateGenerator/StaticRouteHandlerModel/StaticRouteHandlerModel.Emitter.cs | |
| internal static class StaticRouteHandlerModelEmitter | |
| return; | |
| } | |
| - if (!endpoint.Response.IsAwaitable && (response.HasNoResponse || response.IsIResult)) | |
| + if (response.HasNoResponse || response.IsIResult) | |
| { | |
| return; | |
| } | |
| internal static class StaticRouteHandlerModelEmitter | |
| { | |
| codeWriter.WriteLine($"options.EndpointBuilder.Metadata.Add(new ProducesResponseTypeMetadata(statusCode: StatusCodes.Status200OK, type: typeof(string), contentTypes: GeneratedMetadataConstants.PlaintextContentType));"); | |
| } | |
| - else if (response.IsAwaitable && response.ResponseType == null) | |
| - { | |
| - codeWriter.WriteLine($"options.EndpointBuilder.Metadata.Add(new ProducesResponseTypeMetadata(statusCode: StatusCodes.Status200OK, type: typeof(void), contentTypes: GeneratedMetadataConstants.PlaintextContentType));"); | |
| - } | |
| else if (response.ResponseType is { } responseType) | |
| { | |
| codeWriter.WriteLine($$"""options.EndpointBuilder.Metadata.Add(new ProducesResponseTypeMetadata(statusCode: StatusCodes.Status200OK, type: typeof({{responseType.ToDisplayString(EmitterConstants.DisplayFormatWithoutNullability)}}), contentTypes: GeneratedMetadataConstants.JsonContentType));"""); | |
| + codeWriter.WriteLine("ApiEndpointMetadata.AddApiEndpointMetadataIfMissing(options.EndpointBuilder);"); | |
| } | |
| } | |
| internal static class StaticRouteHandlerModelEmitter | |
| codeWriter.WriteLine("if (!serviceProviderIsService.IsService(type))"); | |
| codeWriter.StartBlock(); | |
| codeWriter.WriteLine("options.EndpointBuilder.Metadata.Add(new AcceptsMetadata(type: type, isOptional: isOptional, contentTypes: GeneratedMetadataConstants.JsonContentType));"); | |
| + codeWriter.WriteLine("options.EndpointBuilder.Metadata.Add(ApiEndpointMetadata.Instance);"); | |
| codeWriter.WriteLine("break;"); | |
| codeWriter.EndBlock(); | |
| codeWriter.EndBlock(); | |
| } | |
| else | |
| { | |
| - codeWriter.WriteLine($"options.EndpointBuilder.Metadata.Add(new AcceptsMetadata(contentTypes: GeneratedMetadataConstants.JsonContentType));"); | |
| + codeWriter.WriteLine("options.EndpointBuilder.Metadata.Add(new AcceptsMetadata(contentTypes: GeneratedMetadataConstants.JsonContentType));"); | |
| + codeWriter.WriteLine("options.EndpointBuilder.Metadata.Add(ApiEndpointMetadata.Instance);"); | |
| } | |
| } | |
| diff --git a/src/Http/Http.Extensions/src/RequestDelegateFactory.cs b/src/Http/Http.Extensions/src/RequestDelegateFactory.cs | |
| index 19965ef1..aaebe417 100644 | |
| --- a/src/Http/Http.Extensions/src/RequestDelegateFactory.cs | |
| +++ b/src/Http/Http.Extensions/src/RequestDelegateFactory.cs | |
| public static partial class RequestDelegateFactory | |
| InferAntiforgeryMetadata(factoryContext); | |
| } | |
| - PopulateBuiltInResponseTypeMetadata(methodInfo.ReturnType, factoryContext.EndpointBuilder); | |
| + // If this endpoint expects a JSON request body, we assume its an API endpoint not intended for browser navigation. | |
| + // When present, authentication handlers should prefer returning status codes over browser redirects. | |
| + if (factoryContext.JsonRequestBodyParameter is not null) | |
| + { | |
| + factoryContext.EndpointBuilder.Metadata.Add(ApiEndpointMetadata.Instance); | |
| + } | |
| + | |
| + PopulateBuiltInResponseTypeMetadata(methodInfo.ReturnType, factoryContext); | |
| // Add metadata provided by the delegate return type and parameter types next, this will be more specific than inferred metadata from above | |
| EndpointMetadataPopulator.PopulateMetadata(methodInfo, factoryContext.EndpointBuilder, factoryContext.Parameters); | |
| public static partial class RequestDelegateFactory | |
| return Expression.Block(localVariables, checkParamAndCallMethod); | |
| } | |
| - private static void PopulateBuiltInResponseTypeMetadata(Type returnType, EndpointBuilder builder) | |
| + private static void PopulateBuiltInResponseTypeMetadata(Type returnType, RequestDelegateFactoryContext factoryContext) | |
| { | |
| if (returnType.IsByRefLike) | |
| { | |
| throw GetUnsupportedReturnTypeException(returnType); | |
| } | |
| - var isAwaitable = false; | |
| if (CoercedAwaitableInfo.IsTypeAwaitable(returnType, out var coercedAwaitableInfo)) | |
| { | |
| returnType = coercedAwaitableInfo.AwaitableInfo.ResultType; | |
| - isAwaitable = true; | |
| } | |
| // Skip void returns and IResults. IResults might implement IEndpointMetadataProvider but otherwise we don't know what it might do. | |
| - if (!isAwaitable && (returnType == typeof(void) || typeof(IResult).IsAssignableFrom(returnType))) | |
| + if (returnType == typeof(void) || typeof(IResult).IsAssignableFrom(returnType)) | |
| { | |
| return; | |
| } | |
| + var builder = factoryContext.EndpointBuilder; | |
| + | |
| if (returnType == typeof(string)) | |
| { | |
| builder.Metadata.Add(ProducesResponseTypeMetadata.CreateUnvalidated(type: typeof(string), statusCode: 200, PlaintextContentType)); | |
| } | |
| - else if (returnType == typeof(void)) | |
| - { | |
| - builder.Metadata.Add(ProducesResponseTypeMetadata.CreateUnvalidated(returnType, statusCode: 200, PlaintextContentType)); | |
| - } | |
| else | |
| { | |
| builder.Metadata.Add(ProducesResponseTypeMetadata.CreateUnvalidated(returnType, statusCode: 200, DefaultAcceptsAndProducesContentType)); | |
| + | |
| + if (factoryContext.JsonRequestBodyParameter is null) | |
| + { | |
| + // Since this endpoint responds with JSON, we assume its an API endpoint not intended for browser navigation, | |
| + // but we don't want to bother adding this metadata twice if we've already inferred it based on the expected JSON request body. | |
| + builder.Metadata.Add(ApiEndpointMetadata.Instance); | |
| + } | |
| } | |
| } | |
| diff --git a/src/Http/Http.Results/src/Accepted.cs b/src/Http/Http.Results/src/Accepted.cs | |
| index 5dbc5698..d71fe907 100644 | |
| --- a/src/Http/Http.Results/src/Accepted.cs | |
| +++ b/src/Http/Http.Results/src/Accepted.cs | |
| public sealed class Accepted : IResult, IEndpointMetadataProvider, IStatusCodeHt | |
| ArgumentNullException.ThrowIfNull(builder); | |
| builder.Metadata.Add(new ProducesResponseTypeMetadata(StatusCodes.Status202Accepted, typeof(void))); | |
| + builder.Metadata.Add(ApiEndpointMetadata.Instance); | |
| } | |
| } | |
| diff --git a/src/Http/Http.Results/src/AcceptedAtRoute.cs b/src/Http/Http.Results/src/AcceptedAtRoute.cs | |
| index 74725595..900ee507 100644 | |
| --- a/src/Http/Http.Results/src/AcceptedAtRoute.cs | |
| +++ b/src/Http/Http.Results/src/AcceptedAtRoute.cs | |
| public sealed class AcceptedAtRoute : IResult, IEndpointMetadataProvider, IStatu | |
| ArgumentNullException.ThrowIfNull(builder); | |
| builder.Metadata.Add(new ProducesResponseTypeMetadata(StatusCodes.Status202Accepted, typeof(void))); | |
| + builder.Metadata.Add(ApiEndpointMetadata.Instance); | |
| } | |
| } | |
| diff --git a/src/Http/Http.Results/src/AcceptedAtRouteOfT.cs b/src/Http/Http.Results/src/AcceptedAtRouteOfT.cs | |
| index e9467dbe..b8e4304a 100644 | |
| --- a/src/Http/Http.Results/src/AcceptedAtRouteOfT.cs | |
| +++ b/src/Http/Http.Results/src/AcceptedAtRouteOfT.cs | |
| public sealed class AcceptedAtRoute<TValue> : IResult, IEndpointMetadataProvider | |
| ArgumentNullException.ThrowIfNull(builder); | |
| builder.Metadata.Add(ProducesResponseTypeMetadata.CreateUnvalidated(typeof(TValue), StatusCodes.Status202Accepted, ContentTypeConstants.ApplicationJsonContentTypes)); | |
| + builder.Metadata.Add(ApiEndpointMetadata.Instance); | |
| } | |
| } | |
| diff --git a/src/Http/Http.Results/src/AcceptedOfT.cs b/src/Http/Http.Results/src/AcceptedOfT.cs | |
| index 6777d354..34607071 100644 | |
| --- a/src/Http/Http.Results/src/AcceptedOfT.cs | |
| +++ b/src/Http/Http.Results/src/AcceptedOfT.cs | |
| public sealed class Accepted<TValue> : IResult, IEndpointMetadataProvider, IStat | |
| ArgumentNullException.ThrowIfNull(builder); | |
| builder.Metadata.Add(ProducesResponseTypeMetadata.CreateUnvalidated(typeof(TValue), StatusCodes.Status202Accepted, ContentTypeConstants.ApplicationJsonContentTypes)); | |
| + builder.Metadata.Add(ApiEndpointMetadata.Instance); | |
| } | |
| } | |
| diff --git a/src/Http/Http.Results/src/BadRequest.cs b/src/Http/Http.Results/src/BadRequest.cs | |
| index 9eef74aa..0356afed 100644 | |
| --- a/src/Http/Http.Results/src/BadRequest.cs | |
| +++ b/src/Http/Http.Results/src/BadRequest.cs | |
| public sealed class BadRequest : IResult, IEndpointMetadataProvider, IStatusCode | |
| ArgumentNullException.ThrowIfNull(builder); | |
| builder.Metadata.Add(new ProducesResponseTypeMetadata(StatusCodes.Status400BadRequest, typeof(void))); | |
| + builder.Metadata.Add(ApiEndpointMetadata.Instance); | |
| } | |
| } | |
| diff --git a/src/Http/Http.Results/src/BadRequestOfT.cs b/src/Http/Http.Results/src/BadRequestOfT.cs | |
| index 4b932786..b8fa5142 100644 | |
| --- a/src/Http/Http.Results/src/BadRequestOfT.cs | |
| +++ b/src/Http/Http.Results/src/BadRequestOfT.cs | |
| public sealed class BadRequest<TValue> : IResult, IEndpointMetadataProvider, ISt | |
| ArgumentNullException.ThrowIfNull(builder); | |
| builder.Metadata.Add(ProducesResponseTypeMetadata.CreateUnvalidated(typeof(TValue), StatusCodes.Status400BadRequest, ContentTypeConstants.ApplicationJsonContentTypes)); | |
| + builder.Metadata.Add(ApiEndpointMetadata.Instance); | |
| } | |
| } | |
| diff --git a/src/Http/Http.Results/src/Conflict.cs b/src/Http/Http.Results/src/Conflict.cs | |
| index da162ed5..f0067ba4 100644 | |
| --- a/src/Http/Http.Results/src/Conflict.cs | |
| +++ b/src/Http/Http.Results/src/Conflict.cs | |
| public sealed class Conflict : IResult, IEndpointMetadataProvider, IStatusCodeHt | |
| ArgumentNullException.ThrowIfNull(builder); | |
| builder.Metadata.Add(new ProducesResponseTypeMetadata(StatusCodes.Status409Conflict, typeof(void))); | |
| + builder.Metadata.Add(ApiEndpointMetadata.Instance); | |
| } | |
| } | |
| diff --git a/src/Http/Http.Results/src/ConflictOfT.cs b/src/Http/Http.Results/src/ConflictOfT.cs | |
| index bbfb3c6a..dda54d9d 100644 | |
| --- a/src/Http/Http.Results/src/ConflictOfT.cs | |
| +++ b/src/Http/Http.Results/src/ConflictOfT.cs | |
| public sealed class Conflict<TValue> : IResult, IEndpointMetadataProvider, IStat | |
| ArgumentNullException.ThrowIfNull(builder); | |
| builder.Metadata.Add(ProducesResponseTypeMetadata.CreateUnvalidated(typeof(TValue), StatusCodes.Status409Conflict, ContentTypeConstants.ApplicationJsonContentTypes)); | |
| + builder.Metadata.Add(ApiEndpointMetadata.Instance); | |
| } | |
| } | |
| diff --git a/src/Http/Http.Results/src/Created.cs b/src/Http/Http.Results/src/Created.cs | |
| index 7766d91a..19595562 100644 | |
| --- a/src/Http/Http.Results/src/Created.cs | |
| +++ b/src/Http/Http.Results/src/Created.cs | |
| public sealed class Created : IResult, IEndpointMetadataProvider, IStatusCodeHtt | |
| ArgumentNullException.ThrowIfNull(builder); | |
| builder.Metadata.Add(new ProducesResponseTypeMetadata(StatusCodes.Status201Created, typeof(void))); | |
| + builder.Metadata.Add(ApiEndpointMetadata.Instance); | |
| } | |
| } | |
| diff --git a/src/Http/Http.Results/src/CreatedAtRoute.cs b/src/Http/Http.Results/src/CreatedAtRoute.cs | |
| index 2fb70fa2..2ff79596 100644 | |
| --- a/src/Http/Http.Results/src/CreatedAtRoute.cs | |
| +++ b/src/Http/Http.Results/src/CreatedAtRoute.cs | |
| public sealed class CreatedAtRoute : IResult, IEndpointMetadataProvider, IStatus | |
| ArgumentNullException.ThrowIfNull(builder); | |
| builder.Metadata.Add(new ProducesResponseTypeMetadata(StatusCodes.Status201Created, typeof(void))); | |
| + builder.Metadata.Add(ApiEndpointMetadata.Instance); | |
| } | |
| } | |
| diff --git a/src/Http/Http.Results/src/CreatedAtRouteOfT.cs b/src/Http/Http.Results/src/CreatedAtRouteOfT.cs | |
| index d13d6154..a2868afd 100644 | |
| --- a/src/Http/Http.Results/src/CreatedAtRouteOfT.cs | |
| +++ b/src/Http/Http.Results/src/CreatedAtRouteOfT.cs | |
| public sealed class CreatedAtRoute<TValue> : IResult, IEndpointMetadataProvider, | |
| ArgumentNullException.ThrowIfNull(builder); | |
| builder.Metadata.Add(ProducesResponseTypeMetadata.CreateUnvalidated(typeof(TValue), StatusCodes.Status201Created, ContentTypeConstants.ApplicationJsonContentTypes)); | |
| + builder.Metadata.Add(ApiEndpointMetadata.Instance); | |
| } | |
| } | |
| diff --git a/src/Http/Http.Results/src/CreatedOfT.cs b/src/Http/Http.Results/src/CreatedOfT.cs | |
| index 58d0638b..a12e52b9 100644 | |
| --- a/src/Http/Http.Results/src/CreatedOfT.cs | |
| +++ b/src/Http/Http.Results/src/CreatedOfT.cs | |
| public sealed class Created<TValue> : IResult, IEndpointMetadataProvider, IStatu | |
| ArgumentNullException.ThrowIfNull(builder); | |
| builder.Metadata.Add(ProducesResponseTypeMetadata.CreateUnvalidated(typeof(TValue), StatusCodes.Status201Created, ContentTypeConstants.ApplicationJsonContentTypes)); | |
| + builder.Metadata.Add(ApiEndpointMetadata.Instance); | |
| } | |
| } | |
| diff --git a/src/Http/Http.Results/src/InternalServerErrorOfT.cs b/src/Http/Http.Results/src/InternalServerErrorOfT.cs | |
| index 36ec9fed..c726debf 100644 | |
| --- a/src/Http/Http.Results/src/InternalServerErrorOfT.cs | |
| +++ b/src/Http/Http.Results/src/InternalServerErrorOfT.cs | |
| public sealed class InternalServerError<TValue> : IResult, IEndpointMetadataProv | |
| ArgumentNullException.ThrowIfNull(builder); | |
| builder.Metadata.Add(ProducesResponseTypeMetadata.CreateUnvalidated(typeof(TValue), StatusCodes.Status500InternalServerError, ContentTypeConstants.ApplicationJsonContentTypes)); | |
| + builder.Metadata.Add(ApiEndpointMetadata.Instance); | |
| } | |
| } | |
| diff --git a/src/Http/Http.Results/src/JsonHttpResultOfT.cs b/src/Http/Http.Results/src/JsonHttpResultOfT.cs | |
| index 4841e675..fea23060 100644 | |
| --- a/src/Http/Http.Results/src/JsonHttpResultOfT.cs | |
| +++ b/src/Http/Http.Results/src/JsonHttpResultOfT.cs | |
| // The .NET Foundation licenses this file to you under the MIT license. | |
| using System.Diagnostics.CodeAnalysis; | |
| +using System.Reflection; | |
| using System.Text.Json; | |
| using System.Text.Json.Serialization.Metadata; | |
| +using Microsoft.AspNetCore.Builder; | |
| +using Microsoft.AspNetCore.Http.Metadata; | |
| using Microsoft.AspNetCore.Mvc; | |
| using Microsoft.Extensions.DependencyInjection; | |
| using Microsoft.Extensions.Logging; | |
| namespace Microsoft.AspNetCore.Http.HttpResults; | |
| /// <summary> | |
| /// An action result which formats the given object as JSON. | |
| /// </summary> | |
| -public sealed partial class JsonHttpResult<TValue> : IResult, IStatusCodeHttpResult, IValueHttpResult, IValueHttpResult<TValue>, IContentTypeHttpResult | |
| +public sealed partial class JsonHttpResult<TValue> : IResult, IEndpointMetadataProvider, IStatusCodeHttpResult, IValueHttpResult, IValueHttpResult<TValue>, IContentTypeHttpResult | |
| { | |
| /// <summary> | |
| /// Initializes a new instance of the <see cref="Json"/> class with the values. | |
| public sealed partial class JsonHttpResult<TValue> : IResult, IStatusCodeHttpRes | |
| typedJsonTypeInfo, | |
| contentType: ContentType); | |
| } | |
| - | |
| + | |
| return httpContext.Response.WriteAsJsonAsync( | |
| Value, | |
| JsonTypeInfo, | |
| public sealed partial class JsonHttpResult<TValue> : IResult, IStatusCodeHttpRes | |
| ContentType, | |
| JsonSerializerOptions); | |
| } | |
| + | |
| + static void IEndpointMetadataProvider.PopulateMetadata(MethodInfo method, EndpointBuilder builder) | |
| + { | |
| + builder.Metadata.Add(ApiEndpointMetadata.Instance); | |
| + } | |
| } | |
| diff --git a/src/Http/Http.Results/src/NoContent.cs b/src/Http/Http.Results/src/NoContent.cs | |
| index 987a06fc..35ffb3c5 100644 | |
| --- a/src/Http/Http.Results/src/NoContent.cs | |
| +++ b/src/Http/Http.Results/src/NoContent.cs | |
| public class NoContent : IResult, IEndpointMetadataProvider, IStatusCodeHttpResu | |
| ArgumentNullException.ThrowIfNull(builder); | |
| builder.Metadata.Add(new ProducesResponseTypeMetadata(StatusCodes.Status204NoContent, typeof(void))); | |
| + builder.Metadata.Add(ApiEndpointMetadata.Instance); | |
| } | |
| } | |
| diff --git a/src/Http/Http.Results/src/NotFoundOfT.cs b/src/Http/Http.Results/src/NotFoundOfT.cs | |
| index ad9c3a10..d37a4ee9 100644 | |
| --- a/src/Http/Http.Results/src/NotFoundOfT.cs | |
| +++ b/src/Http/Http.Results/src/NotFoundOfT.cs | |
| public sealed class NotFound<TValue> : IResult, IEndpointMetadataProvider, IStat | |
| ArgumentNullException.ThrowIfNull(builder); | |
| builder.Metadata.Add(ProducesResponseTypeMetadata.CreateUnvalidated(typeof(TValue), StatusCodes.Status404NotFound, ContentTypeConstants.ApplicationJsonContentTypes)); | |
| + builder.Metadata.Add(ApiEndpointMetadata.Instance); | |
| } | |
| } | |
| diff --git a/src/Http/Http.Results/src/Ok.cs b/src/Http/Http.Results/src/Ok.cs | |
| index 0db060d4..837a0fa7 100644 | |
| --- a/src/Http/Http.Results/src/Ok.cs | |
| +++ b/src/Http/Http.Results/src/Ok.cs | |
| public sealed class Ok : IResult, IEndpointMetadataProvider, IStatusCodeHttpResu | |
| ArgumentNullException.ThrowIfNull(builder); | |
| builder.Metadata.Add(new ProducesResponseTypeMetadata(StatusCodes.Status200OK, typeof(void))); | |
| + builder.Metadata.Add(ApiEndpointMetadata.Instance); | |
| } | |
| } | |
| diff --git a/src/Http/Http.Results/src/OkOfT.cs b/src/Http/Http.Results/src/OkOfT.cs | |
| index 108674fd..7f19598e 100644 | |
| --- a/src/Http/Http.Results/src/OkOfT.cs | |
| +++ b/src/Http/Http.Results/src/OkOfT.cs | |
| public sealed class Ok<TValue> : IResult, IEndpointMetadataProvider, IStatusCode | |
| ArgumentNullException.ThrowIfNull(builder); | |
| builder.Metadata.Add(ProducesResponseTypeMetadata.CreateUnvalidated(typeof(TValue), StatusCodes.Status200OK, ContentTypeConstants.ApplicationJsonContentTypes)); | |
| + builder.Metadata.Add(ApiEndpointMetadata.Instance); | |
| } | |
| } | |
| diff --git a/src/Http/Http.Results/src/ProblemHttpResult.cs b/src/Http/Http.Results/src/ProblemHttpResult.cs | |
| index 4fe63c42..8ac29336 100644 | |
| --- a/src/Http/Http.Results/src/ProblemHttpResult.cs | |
| +++ b/src/Http/Http.Results/src/ProblemHttpResult.cs | |
| using Microsoft.AspNetCore.Mvc; | |
| using Microsoft.Extensions.DependencyInjection; | |
| using Microsoft.Extensions.Logging; | |
| +using System.Reflection; | |
| +using Microsoft.AspNetCore.Builder; | |
| +using Microsoft.AspNetCore.Http.Metadata; | |
| namespace Microsoft.AspNetCore.Http.HttpResults; | |
| namespace Microsoft.AspNetCore.Http.HttpResults; | |
| /// An <see cref="IResult"/> that on execution will write Problem Details | |
| /// HTTP API responses based on <see href="https://tools.ietf.org/html/rfc7807"/> | |
| /// </summary> | |
| -public sealed class ProblemHttpResult : IResult, IStatusCodeHttpResult, IContentTypeHttpResult, IValueHttpResult, IValueHttpResult<ProblemDetails> | |
| +public sealed class ProblemHttpResult : IResult, IEndpointMetadataProvider, IStatusCodeHttpResult, IContentTypeHttpResult, IValueHttpResult, IValueHttpResult<ProblemDetails> | |
| { | |
| /// <summary> | |
| /// Creates a new <see cref="ProblemHttpResult"/> instance with | |
| public sealed class ProblemHttpResult : IResult, IStatusCodeHttpResult, IContent | |
| ContentType); | |
| } | |
| } | |
| + | |
| + /// <inheritdoc/> | |
| + static void IEndpointMetadataProvider.PopulateMetadata(MethodInfo method, EndpointBuilder builder) | |
| + { | |
| + ArgumentNullException.ThrowIfNull(method); | |
| + ArgumentNullException.ThrowIfNull(builder); | |
| + | |
| + builder.Metadata.Add(ApiEndpointMetadata.Instance); | |
| + } | |
| } | |
| diff --git a/src/Http/Http.Results/src/ServerSentEventsResult.cs b/src/Http/Http.Results/src/ServerSentEventsResult.cs | |
| index 448b2076..a7951acd 100644 | |
| --- a/src/Http/Http.Results/src/ServerSentEventsResult.cs | |
| +++ b/src/Http/Http.Results/src/ServerSentEventsResult.cs | |
| public sealed class ServerSentEventsResult<T> : IResult, IEndpointMetadataProvid | |
| ArgumentNullException.ThrowIfNull(builder); | |
| builder.Metadata.Add(new ProducesResponseTypeMetadata(StatusCodes.Status200OK, typeof(SseItem<T>), contentTypes: ["text/event-stream"])); | |
| + builder.Metadata.Add(ApiEndpointMetadata.Instance); | |
| } | |
| } | |
| diff --git a/src/Http/Http.Results/src/UnprocessableEntity.cs b/src/Http/Http.Results/src/UnprocessableEntity.cs | |
| index eef96dc9..49d39827 100644 | |
| --- a/src/Http/Http.Results/src/UnprocessableEntity.cs | |
| +++ b/src/Http/Http.Results/src/UnprocessableEntity.cs | |
| public sealed class UnprocessableEntity : IResult, IEndpointMetadataProvider, IS | |
| ArgumentNullException.ThrowIfNull(builder); | |
| builder.Metadata.Add(new ProducesResponseTypeMetadata(StatusCodes.Status422UnprocessableEntity, typeof(void))); | |
| + builder.Metadata.Add(ApiEndpointMetadata.Instance); | |
| } | |
| } | |
| diff --git a/src/Http/Http.Results/src/UnprocessableEntityOfT.cs b/src/Http/Http.Results/src/UnprocessableEntityOfT.cs | |
| index 475a0bc3..2d1ee494 100644 | |
| --- a/src/Http/Http.Results/src/UnprocessableEntityOfT.cs | |
| +++ b/src/Http/Http.Results/src/UnprocessableEntityOfT.cs | |
| public sealed class UnprocessableEntity<TValue> : IResult, IEndpointMetadataProv | |
| ArgumentNullException.ThrowIfNull(builder); | |
| builder.Metadata.Add(ProducesResponseTypeMetadata.CreateUnvalidated(typeof(TValue), StatusCodes.Status422UnprocessableEntity, ContentTypeConstants.ApplicationJsonContentTypes)); | |
| + builder.Metadata.Add(ApiEndpointMetadata.Instance); | |
| } | |
| } | |
| diff --git a/src/Http/Http.Results/src/ValidationProblem.cs b/src/Http/Http.Results/src/ValidationProblem.cs | |
| index ba738cfb..c4a539c9 100644 | |
| --- a/src/Http/Http.Results/src/ValidationProblem.cs | |
| +++ b/src/Http/Http.Results/src/ValidationProblem.cs | |
| public sealed class ValidationProblem : IResult, IEndpointMetadataProvider, ISta | |
| ArgumentNullException.ThrowIfNull(builder); | |
| builder.Metadata.Add(ProducesResponseTypeMetadata.CreateUnvalidated(typeof(HttpValidationProblemDetails), StatusCodes.Status400BadRequest, ContentTypeConstants.ProblemDetailsContentTypes)); | |
| + builder.Metadata.Add(ApiEndpointMetadata.Instance); | |
| } | |
| } | |
| diff --git a/src/Mvc/Mvc.ApiExplorer/src/EndpointMetadataApiDescriptionProvider.cs b/src/Mvc/Mvc.ApiExplorer/src/EndpointMetadataApiDescriptionProvider.cs | |
| index 89d45efc..2f25045d 100644 | |
| --- a/src/Mvc/Mvc.ApiExplorer/src/EndpointMetadataApiDescriptionProvider.cs | |
| +++ b/src/Mvc/Mvc.ApiExplorer/src/EndpointMetadataApiDescriptionProvider.cs | |
| internal sealed class EndpointMetadataApiDescriptionProvider : IApiDescriptionPr | |
| var defaultErrorType = errorMetadata?.Type ?? typeof(void); | |
| var contentTypes = new MediaTypeCollection(); | |
| - // If the return type is an IResult or an awaitable IResult, then we should treat it as a void return type | |
| - // since we can't infer anything without additional metadata. | |
| - if (typeof(IResult).IsAssignableFrom(responseType) || | |
| - producesResponseMetadata.Any(metadata => typeof(IResult).IsAssignableFrom(metadata.Type))) | |
| + // If the return type is an IResult or wrapped in a Task or ValueTask, then we should treat it as a void return type | |
| + // since we can't infer anything without additional metadata or requiring unreferenced code. | |
| + if (IsTaskOrValueTask(responseType) || typeof(IResult).IsAssignableFrom(responseType)) | |
| { | |
| responseType = typeof(void); | |
| } | |
| internal sealed class EndpointMetadataApiDescriptionProvider : IApiDescriptionPr | |
| return apiResponseType == metadataType || | |
| metadataType?.IsAssignableFrom(apiResponseType) == true; | |
| } | |
| + | |
| + static bool IsTaskOrValueTask(Type returnType) | |
| + { | |
| + // If this method did not need to be trim-safe, we would use CoercedAwaitableInfo.IsTypeAwaitable, but we cannot. | |
| + if (returnType.IsAssignableFrom(typeof(Task)) || returnType.IsAssignableFrom(typeof(ValueTask))) | |
| + { | |
| + return true; | |
| + } | |
| + | |
| + if (returnType.FullName is null) | |
| + { | |
| + return false; | |
| + } | |
| + | |
| + return returnType.FullName.StartsWith("System.Threading.Tasks.Task`1[", StringComparison.Ordinal) || | |
| + returnType.FullName.StartsWith("System.Threading.Tasks.ValueTask`1[", StringComparison.Ordinal); | |
| + } | |
| } | |
| private static ApiResponseType CreateDefaultApiResponseType(Type responseType) | |
| diff --git a/src/Mvc/Mvc.Core/src/ApiControllerAttribute.cs b/src/Mvc/Mvc.Core/src/ApiControllerAttribute.cs | |
| index a1b95b3e..f7179b0b 100644 | |
| --- a/src/Mvc/Mvc.Core/src/ApiControllerAttribute.cs | |
| +++ b/src/Mvc/Mvc.Core/src/ApiControllerAttribute.cs | |
| // Licensed to the .NET Foundation under one or more agreements. | |
| // The .NET Foundation licenses this file to you under the MIT license. | |
| +using Microsoft.AspNetCore.Http.Metadata; | |
| using Microsoft.AspNetCore.Mvc.Infrastructure; | |
| namespace Microsoft.AspNetCore.Mvc; | |
| namespace Microsoft.AspNetCore.Mvc; | |
| /// </para> | |
| /// </summary> | |
| [AttributeUsage(AttributeTargets.Assembly | AttributeTargets.Class, AllowMultiple = false, Inherited = true)] | |
| -public class ApiControllerAttribute : ControllerAttribute, IApiBehaviorMetadata | |
| +public class ApiControllerAttribute : ControllerAttribute, IApiBehaviorMetadata, IApiEndpointMetadata | |
| { | |
| } | |
| diff --git a/src/Security/Authentication/Cookies/src/CookieAuthenticationEvents.cs b/src/Security/Authentication/Cookies/src/CookieAuthenticationEvents.cs | |
| index be47982a..994d999a 100644 | |
| --- a/src/Security/Authentication/Cookies/src/CookieAuthenticationEvents.cs | |
| +++ b/src/Security/Authentication/Cookies/src/CookieAuthenticationEvents.cs | |
| // The .NET Foundation licenses this file to you under the MIT license. | |
| using Microsoft.AspNetCore.Http; | |
| +using Microsoft.AspNetCore.Http.Metadata; | |
| using Microsoft.Net.Http.Headers; | |
| namespace Microsoft.AspNetCore.Authentication.Cookies; | |
| public class CookieAuthenticationEvents | |
| /// </summary> | |
| public Func<RedirectContext<CookieAuthenticationOptions>, Task> OnRedirectToLogin { get; set; } = context => | |
| { | |
| - if (IsAjaxRequest(context.Request)) | |
| + if (IsAjaxRequest(context.Request) || IsApiEndpoint(context.HttpContext)) | |
| { | |
| context.Response.Headers.Location = context.RedirectUri; | |
| context.Response.StatusCode = 401; | |
| public class CookieAuthenticationEvents | |
| /// </summary> | |
| public Func<RedirectContext<CookieAuthenticationOptions>, Task> OnRedirectToAccessDenied { get; set; } = context => | |
| { | |
| - if (IsAjaxRequest(context.Request)) | |
| + if (IsAjaxRequest(context.Request) || IsApiEndpoint(context.HttpContext)) | |
| { | |
| context.Response.Headers.Location = context.RedirectUri; | |
| context.Response.StatusCode = 403; | |
| public class CookieAuthenticationEvents | |
| string.Equals(request.Headers.XRequestedWith, "XMLHttpRequest", StringComparison.Ordinal); | |
| } | |
| + private static bool IsApiEndpoint(HttpContext context) | |
| + { | |
| + var endpoint = context.GetEndpoint(); | |
| + return endpoint?.Metadata.GetMetadata<IApiEndpointMetadata>() is not null; | |
| + } | |
| + | |
| /// <summary> | |
| /// Invoked to validate the principal. | |
| /// </summary> | |
| diff --git a/src/SignalR/common/Http.Connections/src/ConnectionEndpointRouteBuilderExtensions.cs b/src/SignalR/common/Http.Connections/src/ConnectionEndpointRouteBuilderExtensions.cs | |
| index 8ed01395..f1aad864 100644 | |
| --- a/src/SignalR/common/Http.Connections/src/ConnectionEndpointRouteBuilderExtensions.cs | |
| +++ b/src/SignalR/common/Http.Connections/src/ConnectionEndpointRouteBuilderExtensions.cs | |
| using System.Diagnostics.CodeAnalysis; | |
| using Microsoft.AspNetCore.Connections; | |
| using Microsoft.AspNetCore.Http.Connections; | |
| using Microsoft.AspNetCore.Http.Connections.Internal; | |
| +using Microsoft.AspNetCore.Http.Metadata; | |
| using Microsoft.AspNetCore.Http.Timeouts; | |
| using Microsoft.AspNetCore.Routing; | |
| using Microsoft.Extensions.DependencyInjection; | |
| public static class ConnectionEndpointRouteBuilderExtensions | |
| { | |
| e.Metadata.Add(data); | |
| } | |
| + | |
| + // Add IApiEndpointMetadata to indicate this is a non-browser endpoint (SignalR) | |
| + e.Metadata.Add(ApiEndpointMetadata.Instance); | |
| }); | |
| return new ConnectionEndpointRouteBuilder(compositeConventionBuilder); | |
| public static class ConnectionEndpointRouteBuilderExtensions | |
| } | |
| } | |
| } | |
| + | |
| + private sealed class ApiEndpointMetadata : IApiEndpointMetadata | |
| + { | |
| + public static ApiEndpointMetadata Instance { get; } = new(); | |
| + } | |
| } | |