Data annotations in Swagger

五迷三道 提交于 2019-11-29 09:15:41

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

For instance:

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

will become:

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

And this:

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

becomes:

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

Besides StringLength Swashbuckle also supports the Range and RegularExpression attributes.

Update

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:

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!