Skip to content

Added new overloads to RequestDelegateFactory #34375

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, 2021
Merged

Conversation

davidfowl
Copy link
Member

  • Throw exceptions if route parameters are specified via attributes but not declared in the list of route parameters.
  • Don't fall back to the query string if the the route parameter is specified.
  • Added tests

Fixes #33700

- Throw exceptions if route parameters are specified via attributes but not declared in the list of route parameters.
- Don't fall back to the query string if the the route parameter is specified.
- Added tests
@@ -202,6 +200,11 @@ private static Expression CreateArgument(ParameterInfo parameter, FactoryContext

if (parameterCustomAttributes.OfType<IFromRouteMetadata>().FirstOrDefault() is { } routeAttribute)
{
if (factoryContext.RouteParameters is { } routeParams && !routeParams.Contains(parameter.Name))
{
throw new InvalidOperationException($"{parameter.Name} is not a route paramter.");
Copy link
Member Author

@davidfowl davidfowl Jul 15, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is worth a look. I added some tests for this scenario as well. TL;DR If you specify [FromRoute] and but didn't specify a route parameter in the template, this will throw. In theory, you could add route values dynamically via middleware so maybe it's too aggressive but I think that's pretty rare.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not identical, but Blazor does this today and it helps alleviate the class of issues. We can always change it to be less restrictive if we users run in to in the future (particularly given that we have an option type for configure this).

@davidfowl davidfowl requested a review from pranavkm July 15, 2021 15:40
@captainsafia captainsafia added the api-approved API was approved in API review, it can be implemented label Jul 15, 2021
@@ -202,6 +200,11 @@ private static Expression CreateArgument(ParameterInfo parameter, FactoryContext

if (parameterCustomAttributes.OfType<IFromRouteMetadata>().FirstOrDefault() is { } routeAttribute)
{
if (factoryContext.RouteParameters is { } routeParams && !routeParams.Contains(parameter.Name))
{
throw new InvalidOperationException($"{parameter.Name} is not a route paramter.");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not identical, but Blazor does this today and it helps alleviate the class of issues. We can always change it to be less restrictive if we users run in to in the future (particularly given that we have an option type for configure this).

namespace Microsoft.AspNetCore.Http
{
/// <summary>
/// Options for controlling the behavior
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
/// Options for controlling the behavior
/// Options for controlling the behavior of <see cref="RequestDelegate" /> when created using <see cref="RequestDelegateFactory" />.

/// <summary>
/// Options for controlling the behavior
/// </summary>
public class RequestDelegateFactoryOptions
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
public class RequestDelegateFactoryOptions
public sealed class RequestDelegateFactoryOptions

@davidfowl davidfowl merged commit 6a09fd8 into main Jul 15, 2021
@davidfowl davidfowl deleted the davidfowl/rdf-overloads branch July 15, 2021 17:53
@ghost ghost added this to the 6.0-preview7 milestone Jul 15, 2021
{
return BindParameterFromProperty(parameter, RouteValuesExpr, parameter.Name, factoryContext);
}

return BindParameterFromRouteValueOrQueryString(parameter, parameter.Name, factoryContext);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this even attempt to bind from RouteValues anymore?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The only scenario is if the list of routes is null. But maybe if it's not null and it's not a route, the we fallback directly to query? I can add that in rc1

@amcasey amcasey added area-networking Includes servers, yarp, json patch, bedrock, websockets, http client factory, and http abstractions and removed area-runtime labels Aug 24, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
api-approved API was approved in API review, it can be implemented area-networking Includes servers, yarp, json patch, bedrock, websockets, http client factory, and http abstractions
Projects
None yet
Development

Successfully merging this pull request may close these issues.

RequestDelegateFactory should take an optional set of route pattern names or route parameter names
5 participants