Swashbuckle.aspnetcore: GET route parameters not getting passed (v2.2.0)

Created on 4 Mar 2018  Â·  13Comments  Â·  Source: domaindrivendev/Swashbuckle.AspNetCore

Lets assume I have a controller action like this one:

[ProducesResponseType(typeof(ServerResponse<IEnumerable<Province>>), 200)]
[HttpGet("provinces/{countryCode}")]
public async Task<IActionResult> ProvincesByCountryCode(string countryCode)
{
        var result = await _location.GetProvinceByCountryCode(countryCode);
        return Reply(result);
}

Using the SwaggerUI to test and populating the variable with "US", the following URL gets called:
http://localhost:2391/api/v1/data/provinces/%7Bcountrycode%7D instead of
http://localhost:2391/api/v1/data/provinces/US

It is not passing the variable on the route, {countryCode} gets sent.
This worked perfectly on the 1.x release.

Most helpful comment

As per @vaustral, the casing mismatch is definitely the issue.

If you want to keep the property in PascalCase, to adhere to c# naming conventions, but still use camelCase in your route templates, you can apply the following setting when you configure Swashbuckle.

c.DescribeAllParametersInCamelCase();

All 13 comments

I can't repro this issue and therefore it's impossible to troubleshoot:

swagger

Can you try create a new project from scratch and see if it repros?

I have the same issue, but the problems arise whenever I use capitalization, so countryCode doesn't work, but countrycode does work.

Bizarre. Any chance someone can provide repro steps starting from a blank app?

I've got similar behavior but as mentioned, web API routing has always had
issues with case sensitivity with parameters... Or so I thought, been doing
that since web API 1

On Wed., 14 Mar. 2018, 12:05 pm domaindrivendev, notifications@github.com
wrote:

Bizarre. Any chance someone can provide repro steps starting from a blank
app?

—
You are receiving this because you are subscribed to this thread.
Reply to this email directly, view it on GitHub
https://github.com/domaindrivendev/Swashbuckle.AspNetCore/issues/634#issuecomment-373120056,
or mute the thread
https://github.com/notifications/unsubscribe-auth/AFIxOmV0jltnEppRGdLaHksdx_rnla5Fks5teVvMgaJpZM4Sbeeb
.

Also httpcontext matching, any matching ties can invalidate it.

I just ran into this issue as well.

Got [HttpGet("{id}")] on my action and the UI picks it up as a path parameter as expected but when I try it out it's calling this

curl -X GET "http://localhost:5000/v1/Entity/{id}"

The {id} in the url doesn't get replaced with the entered value in the textbox.

Here's a minimal reproduction

image

Controller

namespace WebApplication5.Controllers
{
    public class Query
    {
        public int Id { get; set; }
    }

    [Route("api/[controller]")]
    public class ValuesController : Controller
    {
        // GET api/values/5
        [HttpGet("{id}")]
        public string Get(Query query)
        {
            return "value";
        }
    }
}

Startup

public void ConfigureServices(IServiceCollection services)
{
    services.AddSwaggerGen(c =>
    {
        c.SwaggerDoc("v1", new Info { Title = "My API", Version = "v1" });
    });

    services.AddMvc();
}

// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
    if (env.IsDevelopment())
    {
        app.UseDeveloperExceptionPage();
    }

    app.UseSwagger();
    app.UseSwaggerUI(c =>
    {
        c.SwaggerEndpoint("/swagger/v1/swagger.json", "My API V1");
    });

    app.UseMvc();
}

Id and id don't match in your query class, make the upper case one lower
case.

That work?

On Fri., 16 Mar. 2018, 9:30 am snebjorn, notifications@github.com wrote:

Here's a minimal reproduction

[image: image]
https://user-images.githubusercontent.com/1266245/37529060-6be1f5a0-2936-11e8-9bb6-3752de4300f8.png

Controller

namespace WebApplication5.Controllers
{
public class Query
{
public int Id { get; set; }
}

[Route("api/[controller]")]
public class ValuesController : Controller
{
    // GET api/values/5
    [HttpGet("{id}")]
    public string Get(Query query)
    {
        return "value";
    }
}

}

Startup

public void ConfigureServices(IServiceCollection services)
{
services.AddSwaggerGen(c =>
{
c.SwaggerDoc("v1", new Info { Title = "My API", Version = "v1" });
});

services.AddMvc();

}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}

app.UseSwagger();
app.UseSwaggerUI(c =>
{
    c.SwaggerEndpoint("/swagger/v1/swagger.json", "My API V1");
});

app.UseMvc();

}

—
You are receiving this because you commented.
Reply to this email directly, view it on GitHub
https://github.com/domaindrivendev/Swashbuckle.AspNetCore/issues/634#issuecomment-373749744,
or mute the thread
https://github.com/notifications/unsubscribe-auth/AFIxOp7fKzdUfOAwgPqyEnI86IBzy_cDks5te9p6gaJpZM4Sbeeb
.

As per @vaustral, the casing mismatch is definitely the issue.

If you want to keep the property in PascalCase, to adhere to c# naming conventions, but still use camelCase in your route templates, you can apply the following setting when you configure Swashbuckle.

c.DescribeAllParametersInCamelCase();

Wouldn't it be sensible to ignore casing when matching query/route parameters against the model?
The previous version of Swashbuckle didn't care about casing. Neither does the current version of ASPNET Core. The following works

[HttpGet("id")]
public IActionResult Get(int ID)

I would argue that Swashbuckle should follow the expected behavior of the ASP NET Core framework.

I appreciate the c.DescribeAllParametersInCamelCase(); option, but isn't that just supposed to change how you want your parameters displayed in the UI and not actually have an impact on the ability to call the API.

@domaindrivendev @vaustral the c.DescribeAllParametersInCamelCase(); fix isn't ideal. See above post.
I'm not sure if this is a swashbuckle issue or a swagger ui issue. But that fact that parameter name displayed in swagger must match the exact casing of the parameter (not the route) is error prone.

| Route | Action | DescribeAllParametersInCamelCase | Working |
| ------------- | ------------- | ------------- | ------------- |
| [HttpGet("id")] | Get(int ID) | :black_square_button: | :x: |
| [HttpGet("Id")] | Get(int ID) | :black_square_button: | :x: |
| [HttpGet("Id")] | Get(int Id) | :black_square_button: | :heavy_check_mark: |
| [HttpGet("Id")] | Get(int Id) | :white_square_button: | :x: |
| [HttpGet("Id")] | Get(int id) | :black_square_button: | :x: |
| [HttpGet("Id")] | Get(int id) | :white_square_button: | :heavy_check_mark: |

I just got this issue when upgrading to 2.3.0 from 1.0.0. In 1.0.0 this was not an issue. Just for info.

I could be wrong, but casing is a typical problem in web api rest parameters, for as long as i can remember - i may be missing a basic thing that could make my life easier, but I've just gotten used to it.

If i had ID and iD, a request from postman would fail if the request had ID and the parameter was iD unless manual intervention was required.

Impacts can occur also between windows/Linux hosting as well as .net core 1.0->2.1. I'm very much in the practice of using your 3rd example @snebjorn not even factoring in Swagger/Swashbuckle.

Granted this link mentions OpenAPI 3.0 but it does say in a few places that things are case sensitive.
http://docs.swagger.io/spec.html without more investigation i can say its similar behaviors in 2.0 as well.

Any proposed changes should obviously go through @domaindrivendev but in my opinion there is nothing to change as it's behaving as i would expect.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

flipchart picture flipchart  Â·  4Comments

JoelAdamWeiss picture JoelAdamWeiss  Â·  4Comments

tibitoth picture tibitoth  Â·  3Comments

engelhardtda picture engelhardtda  Â·  3Comments

jluqueba picture jluqueba  Â·  4Comments