If you have a complex object that you are mapping with [FromQuery] then there doesn't seem to be a great way to make those follow camelCase convention unless you put [FromQuery(Name="camelCase"] on each property. Seems like a simple convention could be configured or an option on FromQuery, but this is usually an app setting, not something you decide to do for each API.
Let me know if there is a better way to support this. I'd be happy to help if there is obvious place for this to go.
Is there a widely accepted convention for this?
Although Microsoft suggests using camelCase for query parameters (https://github.com/Microsoft/api-guidelines/blob/master/Guidelines.md), I doubt Microsoft should be the only reference.
At the company where I'm currently empoyed, convention is to use lowercase (eg: pagesize) but I've seen other uses (eg: Facebook uses snake_case if I'm not mistaken).
I've forked your repo and I am adding an option QueryParametersCapitalization to SchemaRegistryOptions of enum type CapitalizationType:
public enum CapitalizationType
{
None,
CamelCase,
PascalCase,
SnakeCase,
KebabCase
}
The generator then converts the query parameter names.
Ofcourse, model binding would also need its custom implementation but that's outside of Swashbuckle's scope.
What's your opinion on the above?
Thanks!
EDIT:
After some more digging in your code and the mvc code, I found that the above is not the ideal solution.
Instead of extending Swashbuckle, I now altered the parameter names in the ApiExplorer that is included in MVC.
public class LowerCaseQueryParametersApiDescriptionProvider : IApiDescriptionProvider
{
public int Order
{
get
{
return 1;
}
}
public void OnProvidersExecuted(ApiDescriptionProviderContext context)
{
}
public void OnProvidersExecuting(ApiDescriptionProviderContext context)
{
foreach (var parameter in context.Results.SelectMany(x => x.ParameterDescriptions).Where(x => x.Source.Id == "Query"))
{
parameter.Name = parameter.Name.ToLower();
}
}
}
In ConfigureServices, you can then:
services.TryAddEnumerable(ServiceDescriptor.Transient<IApiDescriptionProvider, LowerCaseQueryParametersApiDescriptionProvider>());
Disclaimer: the code above is test code, some additional code (eg: null testing, readibility, ...) might be needed.
See d8ba4d35c17afa3f1d06884b9653a89ea746fe3c
@domaindrivendev be great to publish a new package for this.
Available with latest pre-release. Note the package rename to "Swashbuckle.AspNetCore.1.0.0-rc1"
Still not available in latest nuget package
The hack to put [FromQuery(Name="camelCase"] does not work with .Net Core 2.2. Is there another solution for this?
@RamunasAdamonis please provide a code example that repro's your issue. I just tested in a .Net Core 2.2 project and both the [FromQuery] annotation AND the global config setting DescribeAllParametersInCamelCase work exactly as expected.
@domaindrivendev I was not using DescribeAllParametersInCamelCase option. It works as expected now. Thank you for highlighting this.
Most helpful comment
@RamunasAdamonis please provide a code example that repro's your issue. I just tested in a .Net Core 2.2 project and both the
[FromQuery]annotation AND the global config settingDescribeAllParametersInCamelCasework exactly as expected.