-
Notifications
You must be signed in to change notification settings - Fork 10.4k
Infer that interface parameters are services #31658
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,7 +3,6 @@ | |
|
||
using System; | ||
using System.Collections.Concurrent; | ||
using System.Diagnostics; | ||
using System.IO; | ||
using System.Linq; | ||
using System.Linq.Expressions; | ||
|
@@ -203,15 +202,7 @@ private static Expression CreateArgument(ParameterInfo parameter, FactoryContext | |
} | ||
else if (parameterCustomAttributes.OfType<IFromBodyMetadata>().FirstOrDefault() is { } bodyAttribute) | ||
{ | ||
if (factoryContext.JsonRequestBodyType is not null) | ||
{ | ||
throw new InvalidOperationException("Action cannot have more than one FromBody attribute."); | ||
} | ||
|
||
factoryContext.JsonRequestBodyType = parameter.ParameterType; | ||
factoryContext.AllowEmptyRequestBody = bodyAttribute.AllowEmpty; | ||
|
||
return Expression.Convert(BodyValueExpr, parameter.ParameterType); | ||
return BindParameterFromBody(parameter.ParameterType, bodyAttribute.AllowEmpty, factoryContext); | ||
} | ||
else if (parameter.CustomAttributes.Any(a => typeof(IFromServiceMetadata).IsAssignableFrom(a.AttributeType))) | ||
{ | ||
|
@@ -229,10 +220,14 @@ private static Expression CreateArgument(ParameterInfo parameter, FactoryContext | |
{ | ||
return BindParameterFromRouteValueOrQueryString(parameter, parameter.Name, factoryContext); | ||
} | ||
else | ||
else if (parameter.ParameterType.IsInterface) | ||
{ | ||
return Expression.Call(GetRequiredServiceMethod.MakeGenericMethod(parameter.ParameterType), RequestServicesExpr); | ||
} | ||
else | ||
{ | ||
return BindParameterFromBody(parameter.ParameterType, allowEmpty: false, factoryContext); | ||
} | ||
} | ||
|
||
private static Expression CreateMethodCall(MethodInfo methodInfo, Expression? target, Expression[] arguments) => | ||
|
@@ -428,7 +423,7 @@ private static Expression AddResponseWritingToMethodCall(Expression methodCall, | |
var invoker = Expression.Lambda<Func<object?, HttpContext, object?, Task>>( | ||
responseWritingMethodCall, TargetExpr, HttpContextExpr, BodyValueExpr).Compile(); | ||
|
||
var bodyType = factoryContext.JsonRequestBodyType!; | ||
var bodyType = factoryContext.JsonRequestBodyType; | ||
object? defaultBodyValue = null; | ||
|
||
if (factoryContext.AllowEmptyRequestBody && bodyType.IsValueType) | ||
|
@@ -627,6 +622,19 @@ private static Expression BindParameterFromRouteValueOrQueryString(ParameterInfo | |
return BindParameterFromValue(parameter, Expression.Coalesce(routeValue, queryValue), factoryContext); | ||
} | ||
|
||
private static Expression BindParameterFromBody(Type parameterType, bool allowEmpty, FactoryContext factoryContext) | ||
{ | ||
if (factoryContext.JsonRequestBodyType is not null) | ||
halter73 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
{ | ||
throw new InvalidOperationException("Action cannot have more than one FromBody attribute."); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This error is kinda jank for the case where there's no explicit attribute. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Does this code path execute in the context of executing the delegate (as opposed to whenever we build the route table)? If it's the latter, we should include more details about what method produced this error. It might be good to include that regardless. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is build time, but I agree this error message is terrible especially in light of the new convention-based inference of parameter sources. I want to track more information about the parameters when building up the RequestDelegate arguments which will lead to better errors, but I've already started on the culture stuff so I think I'll do this as part of this PR. |
||
} | ||
|
||
factoryContext.JsonRequestBodyType = parameterType; | ||
factoryContext.AllowEmptyRequestBody = allowEmpty; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Interesting that inference means you lose this feature. Can we use nullability to determine this? Thoughts @pranavkm? |
||
|
||
return Expression.Convert(BodyValueExpr, parameterType); | ||
} | ||
|
||
private static MethodInfo GetMethodInfo<T>(Expression<T> expr) | ||
{ | ||
var mc = (MethodCallExpression)expr.Body; | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, I think 👍🏽 @davidfowl 's comment - could we rely on the presence of a default value to determine if this is optional? e.g.
If we were absolutely bonkers like MVC, we could also rely on nullability of the parameter to determine optionality:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yep. I'm a huge fain of using nullability to determine
AllowEmpty
behavior. I think we should also use it when the[FromBody]
attribute is applied without an explicit parameter.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Behold ye what needs to be done for nullability: https://github.com/dotnet/aspnetcore/blob/main/src/Mvc/Mvc.DataAnnotations/src/DataAnnotationsMetadataProvider.cs#L465-L568. It would be nice to put this in a shared space.
Btw, it would be a lot easier to infer nullability once we have a source generator.