RicoSuter/NSwag

Custom route constraints are not seen a separate routes in ASP.NET Core Swagger Generator throwing an error

Open

#2,004 opened on Mar 6, 2019

View on GitHub
 (1 comment) (4 reactions) (0 assignees)C# (1,189 forks)batch import
help wantedtype: bug

Repository metrics

Stars
 (6,291 stars)
PR merge metrics
 (No merged PRs in 30d)

Description

Given the two routes:

[HttpGet("~/api/{versionNumber:v1}/customer/{customerId}", Name = "GetCustomerV1")]
public IActionResult GetCaseResultV1(string customerId)
{
}

and

[HttpGet("~/api/{versionNumber:v2}/customer/{customerId}", Name = "GetCustomerV2")]
public IActionResult GetCaseResultV2(string customerId)
{
}

And the custom route constraints:

public class VersionTwoRouteConstraint : IRouteConstraint
{
	public bool Match(HttpContext httpContext, IRouter route, string routeKey, RouteValueDictionary values,
		RouteDirection routeDirection)
	{
		object value;
		if (values.TryGetValue(routeKey, out value) && value != null)
		{
			var regex = "^v2.[0-9]{1,2}$";
			if (Regex.IsMatch(value.ToString(), regex))
			{
				return true;
			}

		}

		return false;
	}
}

public class VersionOneRouteConstraint : IRouteConstraint
{
	public bool Match(HttpContext httpContext, IRouter route, string routeKey, RouteValueDictionary values,
		RouteDirection routeDirection)
	{
		object value;
		if (values.TryGetValue(routeKey, out value) && value != null)
		{
			var regex = "^v[^2].[0-9]{1,2}$";
			if (Regex.IsMatch(value.ToString(), regex))
			{
				return true;
			}

		}

		return false;
	}
}

And their subsequent registration:

services.AddRouting(options =>
	options.ConstraintMap.Add("v2", typeof(VersionTwoRouteConstraint)));
services.AddRouting(options =>
	options.ConstraintMap.Add("v1", typeof(VersionTwoRouteConstraint)));

The AspNetCoreToSwaggerGenerator produces an exception:

The method 'Get' on path '/api/{versionNumber}/customer/{customerId}' is registered multiple times. The versionNumber parameter contraints are not evaluated correctly.

This is using NSwag.AspNetCore 12.0.0.

Is there a way around this? Some kind of SwaggerAttribute that can be used for this route constraint?

Contributor guide