在ASP.NET Core 3.0 Swagger中将字符串标记为不可为空 [英] Mark strings as non-nullable in ASP.NET Core 3.0 Swagger

查看:248
本文介绍了在ASP.NET Core 3.0 Swagger中将字符串标记为不可为空的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的ASP.NET Core 3和Swashbuckle大多数都是默认配置,我有一个DTO参数,上面带有一个字符串,我希望它不能为空.我怎样才能做到这一点?请注意,必需和可空性是Swagger中单独的关注点.

I'm using ASP.NET Core 3 and Swashbuckle with mostly default configuration and I have a DTO parameter with a string on it that I want to be non-nullable. How can I achieve this? Note, Required and nullability are separate concerns in Swagger.

它也使用C#8和不可为空的东西,因此编译器应将属性注释为不可为空.可以理解的是,Swashbuckle尚未进行更新以考虑到这一点(也许不能),但是我希望能够以某种方式覆盖生成的元数据.

It's also using C#8 and the non-nullable stuff, so the compiler should be annotating the property as non-nullable already. It's understandable that Swashbuckle hasn't been updated to take that into account (and maybe can't) but I would like to be able to override the generated metadata somehow.

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) {
}

我尝试将其设置为必填.我还尝试添加Newtonsoft批注,但这些似乎都没有.

I have tried making it Required. I also tried adding the Newtonsoft annotations, but none of these seemed to do it.

生成的Swagger文档的相关位:

Relevant bit of Swagger doc that is generated:

    "MyDto": {
      "required": [
        "testProp"
      ],
      "type": "object",
      "properties": {
        "testProp": {
          "type": "string",
          "nullable": true
        }
      },
      "additionalProperties": false
     }

请注意,直接将字符串参数作为参数不会生成可为空的属性.例如

Note that having a string parameter directly as a parameter doesn't generate the nullable attribute. E.g.

[HttpPost("testPost")]
public void Post([FromBody] [Required] string testProp) {
}

将生成

"/api/test/testPost": {
  "post": {
    "tags": [
      "Test"
    ],
    "requestBody": {
      "content": {
        "application/json": {
          "schema": {
            "type": "string"
          }
        },
        "text/json": {
          "schema": {
            "type": "string"
          }
        },
        "application/*+json": {
          "schema": {
            "type": "string"
          }
        }
      },
      "required": true
    },
    "responses": {
      "200": {
        "description": "Success"
      }
    }
  }
},

推荐答案

直到v4.01 nullable:true 都发出了可选字符串.这在第一个5.0 RC版本中出现了问题,对于可选字符串,根本没有发出 nullable:true .显然,这是错误的.

Until v4.01 nullable:true was emitted for optional strings. This broke in the first 5.0 RC versions and nullable:true wasn't emitted at all for optional strings. That, obviously, is wrong.

从5.0 RC3开始可选字符串再次可为空.

Starting with 5.0 RC3 optional strings are nullable once again.

要指定可选字符串不能为 可为空,您需要在属性中添加 [JsonProperty(Required = Required.DisallowNull)] .从一个复制Swashbuckle的单元测试,这是:

To specify that an optional string is not nullable, you need to add [JsonProperty(Required = Required.DisallowNull)] to the property. Copying from one of Swashbuckle's unit tests, this :

    [JsonProperty(Required = Required.DisallowNull)]
    public string StringWithRequiredDisallowNull { get; set; }

应该设置属性的Nullable标志:

Assert.False(schema.Properties["StringWithRequiredDisallowNull"].Nullable);

并发出 nullable:true .

这篇关于在ASP.NET Core 3.0 Swagger中将字符串标记为不可为空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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