在 Swagger 中指示复杂输入参数对象的必需属性 UI
Indicate required properties of complex input parameter object in Swagger UI
在这个方法中
/// <summary>
/// Gets activity logs.
/// </summary>
/// <param name="locationId">Location id.</param>
/// <param name="filter">Activity log filter options.</param>
/// <response code="200">OK</response>
[ResponseType(typeof(ActivityLogResponse))]
public async Task<HttpResponseMessage> FetchActivityLogs(int locationId, ActivityLogFilterOptions filter)
{
}
ActivityLogFilterOptions
有一些必需的属性,有些是可选的。有什么方法可以在 Swagger UI API 参数中表明这一点?
ActivityLogFilterOptions Class:
/// <summary>
/// Represents an activity log filter options.
/// </summary>
public class ActivityLogFilterOptions
{
/// <summary>
/// Gets or sets the device ids to which the activity logs to be fetched.
/// </summary>
public string DeviceIds { get; set; }
/// <summary>
/// Gets or sets the start date for of the search.
/// </summary>
[DateTimeCompare("ToDate",
ValueComparison.IsLessThan, ErrorMessage = "From date must be earlier than end date.")]
public DateTime? FromDate { get; set; }
/// <summary>
/// Gets or sets the end date for the search.
/// </summary>
[DateTimeCompare("FromDate",
ValueComparison.IsGreaterThan, ErrorMessage = "To date must be later than from date.")]
public DateTime? ToDate { get; set; }
/// <summary>
/// Gets or set the page index.
/// </summary>
[Required]
[Range(0, int.MaxValue)]
public int? PageIndex { get; set; }
/// <summary>
/// Gets or sets the maximum record count per page.
/// </summary>
[Required]
[Range(1, short.MaxValue)]
public int? RecordsPerPage { get; set; }
/// <summary>
/// Gets or sets the activity log groups.
/// </summary>
public string Groups { get; set; }
}
是的,如果您使用 RequiredAttribute 修饰 API 模型的属性,那么 属性 将不会在 Swagger [=22] 中显示为 "optional" =]:
[Required]
[JsonProperty(PropertyName = "your_property")]
public string YourProperty {get; set;}
对于复杂的对象,您可以通过单击 "Model" 而不是 "Parameters" 部分的 "Data Type" 列中的 "Example Value" 来查看模型属性的可选性.
在这个方法中
/// <summary>
/// Gets activity logs.
/// </summary>
/// <param name="locationId">Location id.</param>
/// <param name="filter">Activity log filter options.</param>
/// <response code="200">OK</response>
[ResponseType(typeof(ActivityLogResponse))]
public async Task<HttpResponseMessage> FetchActivityLogs(int locationId, ActivityLogFilterOptions filter)
{
}
ActivityLogFilterOptions
有一些必需的属性,有些是可选的。有什么方法可以在 Swagger UI API 参数中表明这一点?
ActivityLogFilterOptions Class:
/// <summary>
/// Represents an activity log filter options.
/// </summary>
public class ActivityLogFilterOptions
{
/// <summary>
/// Gets or sets the device ids to which the activity logs to be fetched.
/// </summary>
public string DeviceIds { get; set; }
/// <summary>
/// Gets or sets the start date for of the search.
/// </summary>
[DateTimeCompare("ToDate",
ValueComparison.IsLessThan, ErrorMessage = "From date must be earlier than end date.")]
public DateTime? FromDate { get; set; }
/// <summary>
/// Gets or sets the end date for the search.
/// </summary>
[DateTimeCompare("FromDate",
ValueComparison.IsGreaterThan, ErrorMessage = "To date must be later than from date.")]
public DateTime? ToDate { get; set; }
/// <summary>
/// Gets or set the page index.
/// </summary>
[Required]
[Range(0, int.MaxValue)]
public int? PageIndex { get; set; }
/// <summary>
/// Gets or sets the maximum record count per page.
/// </summary>
[Required]
[Range(1, short.MaxValue)]
public int? RecordsPerPage { get; set; }
/// <summary>
/// Gets or sets the activity log groups.
/// </summary>
public string Groups { get; set; }
}
是的,如果您使用 RequiredAttribute 修饰 API 模型的属性,那么 属性 将不会在 Swagger [=22] 中显示为 "optional" =]:
[Required]
[JsonProperty(PropertyName = "your_property")]
public string YourProperty {get; set;}
对于复杂的对象,您可以通过单击 "Model" 而不是 "Parameters" 部分的 "Data Type" 列中的 "Example Value" 来查看模型属性的可选性.