Minor fixes for MatchProcessorFactory

Fixing minor issues found doing DFA integration.
This commit is contained in:
Ryan Nowak 2018-07-16 20:29:41 -07:00
parent 5f29e8b062
commit 02e1d78319
4 changed files with 28 additions and 9 deletions

View File

@ -3,7 +3,6 @@
using System; using System;
using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options; using Microsoft.Extensions.Options;
namespace Microsoft.AspNetCore.Routing.Matchers namespace Microsoft.AspNetCore.Routing.Matchers
@ -11,16 +10,13 @@ namespace Microsoft.AspNetCore.Routing.Matchers
internal class DefaultMatchProcessorFactory : MatchProcessorFactory internal class DefaultMatchProcessorFactory : MatchProcessorFactory
{ {
private readonly RouteOptions _options; private readonly RouteOptions _options;
private readonly ILogger<DefaultMatchProcessorFactory> _logger;
private readonly IServiceProvider _serviceProvider; private readonly IServiceProvider _serviceProvider;
public DefaultMatchProcessorFactory( public DefaultMatchProcessorFactory(
IOptions<RouteOptions> options, IOptions<RouteOptions> options,
ILogger<DefaultMatchProcessorFactory> logger,
IServiceProvider serviceProvider) IServiceProvider serviceProvider)
{ {
_options = options.Value; _options = options.Value;
_logger = logger;
_serviceProvider = serviceProvider; _serviceProvider = serviceProvider;
} }
@ -67,13 +63,16 @@ namespace Microsoft.AspNetCore.Routing.Matchers
{ {
throw new InvalidOperationException( throw new InvalidOperationException(
Resources.FormatDefaultInlineConstraintResolver_TypeNotConstraint( Resources.FormatDefaultInlineConstraintResolver_TypeNotConstraint(
constraintType, constraintName, typeof(IRouteConstraint).Name)); constraintType,
constraintName,
typeof(IRouteConstraint).Name));
} }
try try
{ {
return CreateMatchProcessorFromRouteConstraint( return CreateMatchProcessorFromRouteConstraint(
matchProcessorReference.ParameterName, matchProcessorReference.ParameterName,
matchProcessorReference.Optional,
constraintType, constraintType,
constraintArgument); constraintArgument);
} }
@ -91,11 +90,20 @@ namespace Microsoft.AspNetCore.Routing.Matchers
private MatchProcessor CreateMatchProcessorFromRouteConstraint( private MatchProcessor CreateMatchProcessorFromRouteConstraint(
string parameterName, string parameterName,
bool optional,
Type constraintType, Type constraintType,
string constraintArgument) string constraintArgument)
{ {
var routeConstraint = DefaultInlineConstraintResolver.CreateConstraint(constraintType, constraintArgument); var routeConstraint = DefaultInlineConstraintResolver.CreateConstraint(constraintType, constraintArgument);
return (new MatchProcessorReference(parameterName, routeConstraint)).MatchProcessor; var matchProcessor = new MatchProcessorReference(parameterName, routeConstraint).MatchProcessor;
if (optional)
{
matchProcessor = new OptionalMatchProcessor(matchProcessor);
}
matchProcessor.Initialize(parameterName, constraintArgument);
return matchProcessor;
} }
private MatchProcessor ResolveMatchProcessor( private MatchProcessor ResolveMatchProcessor(

View File

@ -801,7 +801,6 @@ namespace Microsoft.AspNetCore.Routing
return new DefaultLinkGenerator( return new DefaultLinkGenerator(
new DefaultMatchProcessorFactory( new DefaultMatchProcessorFactory(
Options.Create(new RouteOptions()), Options.Create(new RouteOptions()),
NullLogger<DefaultMatchProcessorFactory>.Instance,
Mock.Of<IServiceProvider>()), Mock.Of<IServiceProvider>()),
new DefaultObjectPool<UriBuildingContext>(new UriBuilderContextPooledObjectPolicy()), new DefaultObjectPool<UriBuildingContext>(new UriBuilderContextPooledObjectPolicy()),
NullLogger<DefaultLinkGenerator>.Instance); NullLogger<DefaultLinkGenerator>.Instance);

View File

@ -56,6 +56,20 @@ namespace Microsoft.AspNetCore.Routing.Matchers
Assert.False(isMatch); Assert.False(isMatch);
} }
[Fact]
public void Create_CreatesMatchProcessor_FromConstraintText_AndRouteConstraint_Optional()
{
// Arrange
var factory = GetMatchProcessorFactory();
var matchProcessorReference = new MatchProcessorReference("id", true, "int");
// Act
var processor = factory.Create(matchProcessorReference);
// Assert
Assert.IsType<OptionalMatchProcessor>(processor);
}
[Fact] [Fact]
public void Create_CreatesMatchProcessor_FromConstraintText_AndCustomMatchProcessor() public void Create_CreatesMatchProcessor_FromConstraintText_AndCustomMatchProcessor()
{ {
@ -143,7 +157,6 @@ namespace Microsoft.AspNetCore.Routing.Matchers
return new DefaultMatchProcessorFactory( return new DefaultMatchProcessorFactory(
Options.Create(options), Options.Create(options),
NullLogger<DefaultMatchProcessorFactory>.Instance,
services.BuildServiceProvider()); services.BuildServiceProvider());
} }

View File

@ -33,7 +33,6 @@ namespace Microsoft.AspNetCore.Routing.Matchers
var compositeDataSource = new CompositeEndpointDataSource(new[] { endpointDataSource }); var compositeDataSource = new CompositeEndpointDataSource(new[] { endpointDataSource });
var defaultInlineConstraintResolver = new DefaultMatchProcessorFactory( var defaultInlineConstraintResolver = new DefaultMatchProcessorFactory(
Options.Create(new RouteOptions()), Options.Create(new RouteOptions()),
NullLogger<DefaultMatchProcessorFactory>.Instance,
Mock.Of<IServiceProvider>()); Mock.Of<IServiceProvider>());
var endpointSelector = new EndpointSelector( var endpointSelector = new EndpointSelector(
compositeDataSource, compositeDataSource,