Swagger中的数据注释 [英] Data annotations in Swagger

查看:347
本文介绍了Swagger中的数据注释的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用ASP.NET和Swagger,它公开了接受POST的复杂类型.它具有许多具有不同限制长度的字符串字段.如何在Swagger用户界面中反映出来?

I am using ASP.NET and Swagger that exposes a complex type that accepts a POST. It has a number of string fields that have different restricted lengths. How can I reflect that in the Swagger UI?

推荐答案

您可以使用System.ComponentModel.DataAnnotations中的StringLengthAttribute注释属性.

You can annotate the properties with the StringLengthAttribute from System.ComponentModel.DataAnnotations.

例如:

[StringLength(10)]
public String Name {get;set;}

将变为:

"name": {
    "minLength": 0,
    "maxLength": 10,
    "type": "string"
}

这:

[StringLength(10, MinimumLength = 5)]
public String Name {get;set;}

成为:

"name": {
    "minLength": 5,
    "maxLength": 10,
    "type": "string"
}

除了StringLength Swashbuckle还支持RangeRegularExpression属性.

Besides StringLength Swashbuckle also supports the Range and RegularExpression attributes.

更新

MaxLength不起作用. StringLength可以.但是,在Swagger UI中发现此信息有点笨拙.您必须导航到对象的Model,然后将鼠标悬停在属性上:

MaxLength does not work. StringLength does. However, discovering this information in Swagger UI is a bit clumsy. You have to navigate to the Model of your object and then hover over the property:

这篇关于Swagger中的数据注释的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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