Skip to content

Commit d919168

Browse files
authored
RDF test migration (part 3) (#48553)
* Fix handling of struct types in JSON body * Migrate tests related to special types * Migrate endpoint filters tests to shared infra * Add explicit asserts for filter invocation
1 parent 6fb6465 commit d919168

File tree

24 files changed

+1138
-1116
lines changed

24 files changed

+1138
-1116
lines changed

src/Http/Http.Extensions/gen/RequestDelegateGenerator.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,9 @@ public void Initialize(IncrementalGeneratorInitializationContext context)
9999
codeWriter.StartBlock();
100100
codeWriter.WriteLine("if (ic.HttpContext.Response.StatusCode == 400)");
101101
codeWriter.StartBlock();
102-
codeWriter.WriteLine("return ValueTask.FromResult<object?>(Results.Empty);");
102+
codeWriter.WriteLine(endpoint.Response?.IsAwaitable == true
103+
? "return (object?)Results.Empty;"
104+
: "return ValueTask.FromResult<object?>(Results.Empty);");
103105
codeWriter.EndBlock();
104106
endpoint.EmitFilteredInvocation(codeWriter);
105107
codeWriter.EndBlockWithComma();

src/Http/Http.Extensions/gen/RequestDelegateGeneratorSources.cs

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,8 @@ private static void PopulateMetadataForParameter<T>(ParameterInfo parameter, End
101101
private static async ValueTask<(bool, T?)> TryResolveBodyAsync<T>(HttpContext httpContext, LogOrThrowExceptionHelper logOrThrowExceptionHelper, bool allowEmpty, string parameterTypeName, string parameterName, bool isInferred = false)
102102
{
103103
var feature = httpContext.Features.Get<Microsoft.AspNetCore.Http.Features.IHttpRequestBodyDetectionFeature>();
104+
T? bodyValue = default;
105+
var bodyValueSet = false;
104106
105107
if (feature?.CanHaveBody == true)
106108
{
@@ -112,21 +114,8 @@ private static void PopulateMetadataForParameter<T>(ParameterInfo parameter, End
112114
}
113115
try
114116
{
115-
var bodyValue = await httpContext.Request.ReadFromJsonAsync<T>();
116-
if (!allowEmpty && bodyValue == null)
117-
{
118-
if (!isInferred)
119-
{
120-
logOrThrowExceptionHelper.RequiredParameterNotProvided(parameterTypeName, parameterName, "body");
121-
}
122-
else
123-
{
124-
logOrThrowExceptionHelper.ImplicitBodyNotProvided(parameterName);
125-
}
126-
httpContext.Response.StatusCode = StatusCodes.Status400BadRequest;
127-
return (false, bodyValue);
128-
}
129-
return (true, bodyValue);
117+
bodyValue = await httpContext.Request.ReadFromJsonAsync<T>();
118+
bodyValueSet = bodyValue != null;
130119
}
131120
catch (BadHttpRequestException badHttpRequestException)
132121
{
@@ -147,12 +136,22 @@ private static void PopulateMetadataForParameter<T>(ParameterInfo parameter, End
147136
return (false, default);
148137
}
149138
}
150-
else if (!allowEmpty)
139+
140+
if (!allowEmpty && !bodyValueSet)
151141
{
142+
if (!isInferred)
143+
{
144+
logOrThrowExceptionHelper.RequiredParameterNotProvided(parameterTypeName, parameterName, "body");
145+
}
146+
else
147+
{
148+
logOrThrowExceptionHelper.ImplicitBodyNotProvided(parameterName);
149+
}
152150
httpContext.Response.StatusCode = StatusCodes.Status400BadRequest;
151+
return (false, bodyValue);
153152
}
154153
155-
return (allowEmpty, default);
154+
return (true, bodyValue);
156155
}
157156
""";
158157

src/Http/Http.Extensions/gen/StaticRouteHandlerModel/StaticRouteHandlerModel.Emitter.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -338,7 +338,9 @@ public static void EmitFilteredInvocation(this Endpoint endpoint, CodeWriter cod
338338
codeWriter.WriteLine(endpoint.Response?.IsAwaitable == true
339339
? $"await handler({endpoint.EmitFilteredArgumentList()});"
340340
: $"handler({endpoint.EmitFilteredArgumentList()});");
341-
codeWriter.WriteLine("return ValueTask.FromResult<object?>(Results.Empty);");
341+
codeWriter.WriteLine(endpoint.Response?.IsAwaitable == true
342+
? "return (object?)Results.Empty;"
343+
: "return ValueTask.FromResult<object?>(Results.Empty);");
342344
}
343345
else if (endpoint.Response?.IsAwaitable == true)
344346
{

0 commit comments

Comments
 (0)