指示Swagger UI中复杂输入参数对象的必需属性 [英] Indicate required properties of complex input parameter object in Swagger UI

查看:30
本文介绍了指示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 has some required properties and some are optional. Is there any way to indicate this in Swagger UI API Parameters?

ActivityLogFilterOptions类:

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 ,则该属性将不会在Swagger用户界面中显示为可选":

Yes, if you decorate the properties of your API model with the RequiredAttribute then the property will not be displayed as "optional" in the Swagger UI:

[Required]
[JsonProperty(PropertyName = "your_property")]
 public string YourProperty {get; set;}

对于复杂的对象,您可以通过单击参数"部分的数据类型"列中的模型"而不是示例值"来查看模型上属性的可选性.

For complex objects you can see the optionality of the properties on the model by clicking on "Model" rather than "Example Value" in the "Data Type" column of the "Parameters" section.

这篇关于指示Swagger UI中复杂输入参数对象的必需属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆