我正在使用 ASP.NET Core 3 和 Swashbuckle,大部分是默认配置,我有一个 DTO 参数,上面有一个我希望不可为空的字符串。我怎样才能做到这一点?请注意,在 Swagger 中,Required 和 nullability 是不同的关注点。
它还使用 C#8 和不可为空的东西,因此编译器应该已经将属性注释为不可为空。可以理解的是,Swashbuckle 尚未更新以考虑到这一点(也许不能),但我希望能够以某种方式覆盖生成的元数据。
class MyDto {
[Required]
// I want this to show as non-nullable in the swagger documentation (and ideally also be non-nullable in the binding)
public string TestProp { get; set; }
}
[HttpPost]
public void Post([FromBody] MyDto requestModel) {
}
Run Code Online (Sandbox Code Playgroud)
我试过让它成为必需的。我还尝试添加 Newtonsoft 注释,但这些注释似乎都没有。
生成的 Swagger 文档的相关位:
"MyDto": {
"required": [
"testProp"
],
"type": "object",
"properties": {
"testProp": {
"type": "string",
"nullable": true
} …Run Code Online (Sandbox Code Playgroud)