I have configured Swagger for my asp.net webapi which is similar to one shown below
[HttpGet]
[Route("search")]
public async Task<HttpResponseMessage> Get([FromUri]SearchCriteria searchCriteria)
When i see the swagger documentation for the webapi , the parameter is displaying as
searchCriteria.sortField
searchCriteria.sortDirection
and so on... being the sortField, sortDirection are properties of SearchCriteria
How to get the parameter names without the object.propertyname format?
Can anyone help how to fix this?
Thanks
Here's an OperationFilter
I once used to remove the class name from query parameters.
public class ParameterFilter : IOperationFilter
{
private const string Pattern = @"^ # Match start of string
.*? # Lazily match any character, trying to stop when the next condition becomes true
\. # Match the dot";
public void Apply(Operation operation, SchemaRegistry schemaRegistry, ApiDescription apiDescription)
{
if (operation.parameters == null)
{
return;
}
foreach (var parameter in operation.parameters
.Where(x => x.@in == "query" && x.name.Contains(".")))
{
parameter.name = Regex.Replace(
parameter.name,
Pattern,
string.Empty,
RegexOptions.IgnorePatternWhitespace);
}
}
}
Add it to you SwaggerConfig
like this:
GlobalConfiguration.Configuration
.EnableSwagger(c =>
{
// other settings omitted
c.OperationFilter<ParameterFilter>();
});
BTW: The regex is inspired by https://stackoverflow.com/a/7794128/502395
I assume you are using Swashbuckle.
Look at DocumentFilters
and OperationFilters
. You can extend Swashbuckle to intervene at the document or operation level to modify the output.
Read through the Swashbuckle documentation, it is fairly straight forward to implement either of these two interfaces.
When you pass parameter Name = ""
to the FormUri
attribute Swashbuckle generates parameter names in unqualified form e.g. sortField
instead of searchCriteria.sortField
.
public async Task<HttpResponseMessage> Get([FromUri(Name = "")]SearchCriteria searchCriteria)