Skip to content

Commit 0c6dc91

Browse files
authored
Add TypedResults.Extentensions (#47285)
Make TypedResults more consistent with Results by adding extensions support. Extensions on Results.Extensions will also work on TypedResults.
1 parent 7a4829d commit 0c6dc91

File tree

4 files changed

+39
-0
lines changed

4 files changed

+39
-0
lines changed

src/Http/Http.Extensions/test/RequestDelegateGenerator/RequestDelegateCreationTestBase.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -250,6 +250,8 @@ public static IEndpointRouteBuilder MapTestEndpoints(this IEndpointRouteBuilder
250250
{{sources}}
251251
return app;
252252
}
253+
254+
public static IResult TestResult(this IResultExtensions _) => TypedResults.Text("Hello World!");
253255
}
254256
""";
255257
private static Task<Compilation> CreateCompilationAsync(string sources)

src/Http/Http.Extensions/test/RequestDelegateGenerator/RequestDelegateCreationTests.Responses.cs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
// Licensed to the .NET Foundation under one or more agreements.
22
// The .NET Foundation licenses this file to you under the MIT license.
3+
using Microsoft.AspNetCore.Routing.Internal;
4+
using Microsoft.Extensions.ObjectPool;
5+
using Microsoft.Extensions.Primitives;
6+
using System.Text;
7+
38
namespace Microsoft.AspNetCore.Http.Generators.Tests;
49

510
public abstract partial class RequestDelegateCreationTests
@@ -103,6 +108,31 @@ public async Task MapAction_NoParam_ComplexReturn(string source)
103108
await VerifyResponseBodyAsync(httpContext, expectedBody);
104109
}
105110

111+
public static IEnumerable<object[]> MapAction_NoParam_ExtensionResult_Data => new List<object[]>()
112+
{
113+
new object[] { """app.MapGet("/", () => Results.Extensions.TestResult());""" },
114+
new object[] { """app.MapGet("/", () => TypedResults.Extensions.TestResult());""" }
115+
};
116+
117+
[Theory]
118+
[MemberData(nameof(MapAction_NoParam_ExtensionResult_Data))]
119+
public async Task MapAction_NoParam_ExtensionResult(string source)
120+
{
121+
var expectedBody = """Hello World!""";
122+
var (result, compilation) = await RunGeneratorAsync(source);
123+
var endpoint = GetEndpointFromCompilation(compilation);
124+
125+
VerifyStaticEndpointModel(result, endpointModel =>
126+
{
127+
Assert.Equal("/", endpointModel.RoutePattern);
128+
Assert.Equal("MapGet", endpointModel.HttpMethod);
129+
});
130+
131+
var httpContext = CreateHttpContext();
132+
await endpoint.RequestDelegate(httpContext);
133+
await VerifyResponseBodyAsync(httpContext, expectedBody);
134+
}
135+
106136
public static IEnumerable<object[]> MapAction_NoParam_TaskOfTReturn_Data => new List<object[]>()
107137
{
108138
new object[] { @"app.MapGet(""/"", () => Task.FromResult(""Hello world!""));", "Hello world!" },

src/Http/Http.Results/src/PublicAPI.Unshipped.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ static Microsoft.AspNetCore.Http.TypedResults.Created<TValue>(System.Uri? uri, T
3838
*REMOVED*static Microsoft.AspNetCore.Http.TypedResults.Created<TValue>(string! uri, TValue? value) -> Microsoft.AspNetCore.Http.HttpResults.Created<TValue>!
3939
static Microsoft.AspNetCore.Http.TypedResults.CreatedAtRoute(string? routeName, Microsoft.AspNetCore.Routing.RouteValueDictionary? routeValues) -> Microsoft.AspNetCore.Http.HttpResults.CreatedAtRoute!
4040
static Microsoft.AspNetCore.Http.TypedResults.CreatedAtRoute<TValue>(TValue? value, string? routeName, Microsoft.AspNetCore.Routing.RouteValueDictionary? routeValues) -> Microsoft.AspNetCore.Http.HttpResults.CreatedAtRoute<TValue>!
41+
static Microsoft.AspNetCore.Http.TypedResults.Extensions.get -> Microsoft.AspNetCore.Http.IResultExtensions!
4142
static Microsoft.AspNetCore.Http.TypedResults.Json<TValue>(TValue? data, System.Text.Json.Serialization.JsonSerializerContext! context, string? contentType = null, int? statusCode = null) -> Microsoft.AspNetCore.Http.HttpResults.JsonHttpResult<TValue>!
4243
static Microsoft.AspNetCore.Http.TypedResults.Json<TValue>(TValue? data, System.Text.Json.Serialization.Metadata.JsonTypeInfo<TValue>! jsonTypeInfo, string? contentType = null, int? statusCode = null) -> Microsoft.AspNetCore.Http.HttpResults.JsonHttpResult<TValue>!
4344
static Microsoft.AspNetCore.Http.TypedResults.RedirectToRoute(string? routeName, Microsoft.AspNetCore.Routing.RouteValueDictionary? routeValues, bool permanent = false, bool preserveMethod = false, string? fragment = null) -> Microsoft.AspNetCore.Http.HttpResults.RedirectToRouteHttpResult!

src/Http/Http.Results/src/TypedResults.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1028,4 +1028,10 @@ public static AcceptedAtRoute<TValue> AcceptedAtRoute<TValue>(TValue? value, str
10281028
/// Produces an empty result response, that when executed will do nothing.
10291029
/// </summary>
10301030
public static EmptyHttpResult Empty { get; } = EmptyHttpResult.Instance;
1031+
1032+
/// <summary>
1033+
/// Provides a container for external libraries to extend
1034+
/// the default `TypedResults` set with their own samples.
1035+
/// </summary>
1036+
public static IResultExtensions Extensions { get; } = new ResultExtensions();
10311037
}

0 commit comments

Comments
 (0)