Nswag: "Route only" parameters ignored in paths

Created on 6 Jan 2017  Â·  6Comments  Â·  Source: RicoSuter/NSwag

Please forgive the poorly worded issue title, this is one of those cases that is easier to explain with code. For architectural reasons, I have a base controller defined like this:

public abstract class RegionalItemController : Controller
{
    public int RegionId => GetRouteValue<int>(ControllerContext, "regionId");

    protected static TValue GetRouteValue<TValue>(ControllerContext context, string name)
    {
        return (TValue)Convert.ChangeType(context.RouteData.Values[name], typeof(TValue));
    }
}

The RegionId parameter is then used in derived controllers like this:

[Route("api/region/{regionId:int}/food")]
public class FoodsController : RegionalItemController
{
    [HttpGet]
    public Food[] Get()
    {
        return RegionId == 2 ? new[] {new Food {Name = "Haggis"}} : new Food[0];
    }
}

In Swashbuckle,AspNetCore, the operation is recognized as GET /api/region/{regionId}/food.

In NSwag.AspNetCore (tested on 8.5.0), the outcome is the invalid operation GET /api/region//food.

Most helpful comment

Have you tried the option "AddMissingPathParameters":

image

?

All 6 comments

I've added a unit test and it works for me... however this is full .NET and not .NET Core. Maybe there is some problem with that...

I made a typo in the original submission. The signature of FoodsController should look like this:

[System.Web.Http.HttpGet]
public string[] Get()

I added the regionId parameter to verify the behavior and forgot to remove it when submitting the code sample. Sorry for the inconvenience.

Have you tried the option "AddMissingPathParameters":

image

?

I missed that option, sorry. I added AddMissingPathParameters = true to my initialization code, and the paths now work as intended. Thank you for your quick response, and sorry for the issue turning out to be bogus.

Hmm, maybe we should use "true" as default for this option..

Personally I think having a default of true gives the most expected
behavior.

Den fre. 6. jan. 2017, 16.00 skrev Rico Suter notifications@github.com:

Hmm, maybe we should use "true" as default for this option..

—
You are receiving this because you modified the open/close state.

Reply to this email directly, view it on GitHub
https://github.com/NSwag/NSwag/issues/510#issuecomment-270920655, or mute
the thread
https://github.com/notifications/unsubscribe-auth/AAQnWQrorJLXL17-vUO0kGTIh6LRDMfyks5rPlcJgaJpZM4Lcwlx
.

Was this page helpful?
0 / 5 - 0 ratings