Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

namespace Microsoft.AspNetCore.Routing.Constraints;

/// <summary>
/// Defines a constraint on an optional parameter whose inner constraint also implements
/// <see cref="IOutboundParameterTransformer"/>. This preserves the transformer capability
/// that would otherwise be lost when wrapping in <see cref="OptionalRouteConstraint"/>.
/// </summary>
internal sealed class OptionalOutboundParameterTransformerRouteConstraint : OptionalRouteConstraint, IOutboundParameterTransformer

Check failure on line 11 in src/Http/Routing/src/Constraints/OptionalOutboundParameterTransformerRouteConstraint.cs

View check run for this annotation

Azure Pipelines / aspnetcore-ci (Build Build: macOS x64)

src/Http/Routing/src/Constraints/OptionalOutboundParameterTransformerRouteConstraint.cs#L11

src/Http/Routing/src/Constraints/OptionalOutboundParameterTransformerRouteConstraint.cs(11,102): error CS0246: (NETCORE_ENGINEERING_TELEMETRY=Build) The type or namespace name 'IOutboundParameterTransformer' could not be found (are you missing a using directive or an assembly reference?)

Check failure on line 11 in src/Http/Routing/src/Constraints/OptionalOutboundParameterTransformerRouteConstraint.cs

View check run for this annotation

Azure Pipelines / aspnetcore-ci (Build Build: Linux x64)

src/Http/Routing/src/Constraints/OptionalOutboundParameterTransformerRouteConstraint.cs#L11

src/Http/Routing/src/Constraints/OptionalOutboundParameterTransformerRouteConstraint.cs(11,102): error CS0246: (NETCORE_ENGINEERING_TELEMETRY=Build) The type or namespace name 'IOutboundParameterTransformer' could not be found (are you missing a using directive or an assembly reference?)

Check failure on line 11 in src/Http/Routing/src/Constraints/OptionalOutboundParameterTransformerRouteConstraint.cs

View check run for this annotation

Azure Pipelines / aspnetcore-ci (Build Source-Build (Managed))

src/Http/Routing/src/Constraints/OptionalOutboundParameterTransformerRouteConstraint.cs#L11

src/Http/Routing/src/Constraints/OptionalOutboundParameterTransformerRouteConstraint.cs(11,102): error CS0246: (NETCORE_ENGINEERING_TELEMETRY=Build) The type or namespace name 'IOutboundParameterTransformer' could not be found (are you missing a using directive or an assembly reference?)

Check failure on line 11 in src/Http/Routing/src/Constraints/OptionalOutboundParameterTransformerRouteConstraint.cs

View check run for this annotation

Azure Pipelines / aspnetcore-ci (Build Build: macOS arm64)

src/Http/Routing/src/Constraints/OptionalOutboundParameterTransformerRouteConstraint.cs#L11

src/Http/Routing/src/Constraints/OptionalOutboundParameterTransformerRouteConstraint.cs(11,102): error CS0246: (NETCORE_ENGINEERING_TELEMETRY=Build) The type or namespace name 'IOutboundParameterTransformer' could not be found (are you missing a using directive or an assembly reference?)

Check failure on line 11 in src/Http/Routing/src/Constraints/OptionalOutboundParameterTransformerRouteConstraint.cs

View check run for this annotation

Azure Pipelines / aspnetcore-quarantined-pr (Tests: Ubuntu x64)

src/Http/Routing/src/Constraints/OptionalOutboundParameterTransformerRouteConstraint.cs#L11

src/Http/Routing/src/Constraints/OptionalOutboundParameterTransformerRouteConstraint.cs(11,102): error CS0246: (NETCORE_ENGINEERING_TELEMETRY=Build) The type or namespace name 'IOutboundParameterTransformer' could not be found (are you missing a using directive or an assembly reference?)

Check failure on line 11 in src/Http/Routing/src/Constraints/OptionalOutboundParameterTransformerRouteConstraint.cs

View check run for this annotation

Azure Pipelines / aspnetcore-ci (Build Test: Ubuntu x64)

src/Http/Routing/src/Constraints/OptionalOutboundParameterTransformerRouteConstraint.cs#L11

src/Http/Routing/src/Constraints/OptionalOutboundParameterTransformerRouteConstraint.cs(11,102): error CS0246: (NETCORE_ENGINEERING_TELEMETRY=Build) The type or namespace name 'IOutboundParameterTransformer' could not be found (are you missing a using directive or an assembly reference?)

Check failure on line 11 in src/Http/Routing/src/Constraints/OptionalOutboundParameterTransformerRouteConstraint.cs

View check run for this annotation

Azure Pipelines / aspnetcore-ci (Build Test: macOS)

src/Http/Routing/src/Constraints/OptionalOutboundParameterTransformerRouteConstraint.cs#L11

src/Http/Routing/src/Constraints/OptionalOutboundParameterTransformerRouteConstraint.cs(11,102): error CS0246: (NETCORE_ENGINEERING_TELEMETRY=Build) The type or namespace name 'IOutboundParameterTransformer' could not be found (are you missing a using directive or an assembly reference?)

Check failure on line 11 in src/Http/Routing/src/Constraints/OptionalOutboundParameterTransformerRouteConstraint.cs

View check run for this annotation

Azure Pipelines / aspnetcore-quarantined-pr (Tests: macOS)

src/Http/Routing/src/Constraints/OptionalOutboundParameterTransformerRouteConstraint.cs#L11

src/Http/Routing/src/Constraints/OptionalOutboundParameterTransformerRouteConstraint.cs(11,102): error CS0246: (NETCORE_ENGINEERING_TELEMETRY=Build) The type or namespace name 'IOutboundParameterTransformer' could not be found (are you missing a using directive or an assembly reference?)

Check failure on line 11 in src/Http/Routing/src/Constraints/OptionalOutboundParameterTransformerRouteConstraint.cs

View check run for this annotation

Azure Pipelines / aspnetcore-quarantined-pr

src/Http/Routing/src/Constraints/OptionalOutboundParameterTransformerRouteConstraint.cs#L11

src/Http/Routing/src/Constraints/OptionalOutboundParameterTransformerRouteConstraint.cs(11,102): error CS0246: (NETCORE_ENGINEERING_TELEMETRY=Build) The type or namespace name 'IOutboundParameterTransformer' could not be found (are you missing a using directive or an assembly reference?)

Check failure on line 11 in src/Http/Routing/src/Constraints/OptionalOutboundParameterTransformerRouteConstraint.cs

View check run for this annotation

Azure Pipelines / aspnetcore-ci

src/Http/Routing/src/Constraints/OptionalOutboundParameterTransformerRouteConstraint.cs#L11

src/Http/Routing/src/Constraints/OptionalOutboundParameterTransformerRouteConstraint.cs(11,102): error CS0246: (NETCORE_ENGINEERING_TELEMETRY=Build) The type or namespace name 'IOutboundParameterTransformer' could not be found (are you missing a using directive or an assembly reference?)

Check failure on line 11 in src/Http/Routing/src/Constraints/OptionalOutboundParameterTransformerRouteConstraint.cs

View check run for this annotation

Azure Pipelines / aspnetcore-ci

src/Http/Routing/src/Constraints/OptionalOutboundParameterTransformerRouteConstraint.cs#L11

src/Http/Routing/src/Constraints/OptionalOutboundParameterTransformerRouteConstraint.cs(11,102): error CS0246: (NETCORE_ENGINEERING_TELEMETRY=Build) The type or namespace name 'IOutboundParameterTransformer' could not be found (are you missing a using directive or an assembly reference?)

Check failure on line 11 in src/Http/Routing/src/Constraints/OptionalOutboundParameterTransformerRouteConstraint.cs

View check run for this annotation

Azure Pipelines / aspnetcore-ci

src/Http/Routing/src/Constraints/OptionalOutboundParameterTransformerRouteConstraint.cs#L11

src/Http/Routing/src/Constraints/OptionalOutboundParameterTransformerRouteConstraint.cs(11,102): error CS0246: (NETCORE_ENGINEERING_TELEMETRY=Build) The type or namespace name 'IOutboundParameterTransformer' could not be found (are you missing a using directive or an assembly reference?)

Check failure on line 11 in src/Http/Routing/src/Constraints/OptionalOutboundParameterTransformerRouteConstraint.cs

View check run for this annotation

Azure Pipelines / aspnetcore-ci

src/Http/Routing/src/Constraints/OptionalOutboundParameterTransformerRouteConstraint.cs#L11

src/Http/Routing/src/Constraints/OptionalOutboundParameterTransformerRouteConstraint.cs(11,102): error CS0246: (NETCORE_ENGINEERING_TELEMETRY=Build) The type or namespace name 'IOutboundParameterTransformer' could not be found (are you missing a using directive or an assembly reference?)
{
/// <summary>
/// Creates a new <see cref="OptionalOutboundParameterTransformerRouteConstraint"/> instance.
/// </summary>
/// <param name="innerConstraint">The inner constraint that also implements <see cref="IOutboundParameterTransformer"/>.</param>
public OptionalOutboundParameterTransformerRouteConstraint(IRouteConstraint innerConstraint)
: base(innerConstraint)
{
}

/// <inheritdoc />
public string? TransformOutbound(object? value)
{
return ((IOutboundParameterTransformer)InnerConstraint).TransformOutbound(value);
}
}
4 changes: 3 additions & 1 deletion src/Http/Routing/src/DefaultParameterPolicyFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,9 @@ private static IParameterPolicy InitializeRouteConstraint(
{
if (optional)
{
routeConstraint = new OptionalRouteConstraint(routeConstraint);
routeConstraint = routeConstraint is IOutboundParameterTransformer
? new OptionalOutboundParameterTransformerRouteConstraint(routeConstraint)
: new OptionalRouteConstraint(routeConstraint);
}

return routeConstraint;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,26 @@ public void Create_CreatesParameterPolicy_FromRoutePattern_Constraint_Optional()
Assert.IsType<IntRouteConstraint>(optionalConstraint.InnerConstraint);
}

[Fact]
public void Create_CreatesParameterPolicy_FromRoutePattern_Constraint_Optional_PreservesOutboundTransformer()
{
// Regression test for https://github.com/dotnet/aspnetcore/issues/23063
var factory = GetParameterPolicyFactory();

var parameter = RoutePatternFactory.ParameterPart(
"id",
@default: null,
parameterKind: RoutePatternParameterKind.Optional,
parameterPolicies: new[] { RoutePatternFactory.ParameterPolicy(new TransformingRouteConstraint()), });

var parameterPolicy = factory.Create(parameter, parameter.ParameterPolicies[0]);

var optionalConstraint = Assert.IsType<OptionalOutboundParameterTransformerRouteConstraint>(parameterPolicy);
Assert.IsType<TransformingRouteConstraint>(optionalConstraint.InnerConstraint);
var transformer = Assert.IsAssignableFrom<IOutboundParameterTransformer>(parameterPolicy);
Assert.Equal("hello", transformer.TransformOutbound("HELLO"));
}

[Fact]
public void Create_CreatesParameterPolicy_FromRoutePattern_ParameterPolicy()
{
Expand Down Expand Up @@ -445,6 +465,24 @@ public bool Match(
return false;
}
}

private class TransformingRouteConstraint : IRouteConstraint, IOutboundParameterTransformer
{
public bool Match(
HttpContext httpContext,
IRouter route,
string routeKey,
RouteValueDictionary values,
RouteDirection routeDirection)
{
return true;
}

public string TransformOutbound(object value)
{
return value?.ToString()?.ToLowerInvariant();
}
}
}

public class CustomParameterPolicy : IParameterPolicy
Expand Down
Loading