Version 3.0.0
This works fine:
-> GET api/user/someid
[HttpGet("api/user/{id}"]
public IActionResult Foo([FromRoute] string id) {
//id == "someid"
}
SwaggerUI -> GET api/user/someid
This doesn't:
public class SomeModel {
[FromRoute]
public string NumeroDeSerie { get;set;}
}
[HttpGet("api/user/{id}"]
public IActionResult Foo(SomeModel model) {
//model.NumeroDeSerie == "{id}"
}
SwaggerUI -> GET api/user/{id}
The SwaggerUI generates the path parameter correctly and put it as required, but the actual request to the API is made with the name of the parameter instead of its value.
Workaround:
When you use an object with part "FromRoute" and part "FromQuery", the properties with "FromQuery" will work just fine as they will be camelCased.
The ones with "FromRoute" won't, so you need to force the conversion:
public class SomeModel {
[FromRoute("numeroDeSerie")]
public string NumeroDeSerie { get;set;}
}
Also, specify the object in controller as FromQuery:
[HttpGet("api/user/{id}"]
public IActionResult Foo([FromQuery] SomeModel model) {
//model.NumeroDeSerie == "someid"; \o/
}
Version 3.0.0
MVC Compatibility Version 2.1
Yes, got the same error too.
I'm furious.
Yes, i got he same error too.
Swashbuckle.AspNetCore: 4.0.1
Microsoft.AspNetCore: 2.2.1
Same error
Swashbuckle.AspNetCore: 4.0.1
Found solution
csharp
services.AddSwaggerGen(o =>
{
o.SwaggerDoc("v1", new Info { Title = "api", Version = "v1" });
o.DescribeAllParametersInCamelCase();
});
Support for models containing properties with different [From*] annotations is sorely lacking in Swashbuckle, even though ASP.NET has no problem binding to such models.
@IanKemp wonderful contribution to a free OSS project!!! Thanks for that 馃憥
This is an edge case. Swashbuckle generates the parameter name in PascalCase but the swagger-ui does not reconcile it with a path parameter of the same name but in CamelCase. The name is produced in PascalCase because that's what ApiExplorer, the meta-layer that ships with ASP .NET Core and is heavily depended on by Swashbuckle, surfaces. So, there's also an argument that the root cause is actually in ASP.NET Core despite your comments above.
Regardless, for this case there's an extremely simple and affective workaround using the suggestion posted by @ziilok. If there's other places where Swashbuckle does not handle [From*] attributes correctly please create an issue with concise repro steps as I'm not aware of them at present.
Most helpful comment
Found solution
csharp services.AddSwaggerGen(o => { o.SwaggerDoc("v1", new Info { Title = "api", Version = "v1" }); o.DescribeAllParametersInCamelCase(); });