I want to add default description in schema of documentation for all input parameters with DateTime type. So the clients will understand which formats we are using etc.
Can I create my custom implementation of ISchemaFilter for this purposes?
I know that I can add description by using xml comments, but in this case I should copy and paste the same text in many places where I have filter by date.
I have tried to use MapType for this. But as far as I understand it works only for response types(at least in my case it works only for response models). I found the similar question but it still unanswered
options.MapType<DateTime> (() => new Schema {
Type = "string",
Format = "date-time",
Description = "Description"
});
I also have tried my custom DateTimeSchemaFilter, but no descriptions were added for my input parameters. I have already tried configuration without xml and/or MapType. In debug mode I see that my filter is being called but nothing happened in UI.
public class DateTimeSchemaFilter: ISchemaFilter {
public void Apply(Schema schema, SchemaFilterContext context)
{
var typeInfo = context.SystemType;
if (typeInfo == typeof(DateTime ? ))
{
schema.Description = "Description";
}
}
}
services.AddSwaggerGen(options => {
options.DescribeAllEnumsAsStrings();
var xmlFile = $ {Assembly.GetExecutingAssembly().GetName().Name}.xml";
var xmlPath = Path.Combine(AppContext.BaseDirectory, xmlFile);
options.IncludeXmlComments(xmlPath);
options.MapType<DateTime> (() => new Schema {
Type = "string",
Format = "date-time",
Description = "Description"
});
options.SchemaFilter<DateTimeSchemaFilter>();
});
public async Task<IActionResult> GetTelemetries(
string nodeId,
int offset = 0,
int limit = DEFAULT_PAGE_LIMIT,
TelemetryChannel channel = TelemetryChannel.Temperature,
DateTime? dateFrom = null,
DateTime? dateTo = null)
{
var result = await _telemetryService.GetTelemetries(nodeId, offset, limit);
return BaseResponse(result);
}