有没有办法在 OpenApi v3 规范属性中显式设置 nullable : false ?

ash*_*baz 3 c# swagger openapi nswag

我正在尝试将 OpenAPI 规范中的一个属性设置为必需且可空:如我提供的 C# 中指定的 false。

我正在使用 NSwag v13.1.3 和 NewtonSoft.Json v12.0.2 以及 .Net Core 2.2

我尝试过使用 NewtonSoft.Json 和 NJsonSchema.Annotations 传递各种组合来强制 NotNull 到字段上,但是 nullable : false 在任何组合中似乎都不可能。

我还尝试使用 NSwagStudio 生成与下面相同的代码,但规范中的 nullable 未设置为 false。

using Newtonsoft.Json;

public class Test {

[JsonProperty("test", Required = Required.Always)]
     public string Test { get; set; }

[JsonProperty("testnullable", Required = Required.AllowNull)]
    public string TestNullable { get; set; }

}
Run Code Online (Sandbox Code Playgroud)

我希望这能为 OpenApi 规范提供 nullable : false 和 nullable : true ,但这就是所呈现的:-

"required": [          
      "test",
      "testnullable"
          ],
"properties": {
               "test": {
                  "type": "string"
                        },
               "testnullable": {
                  "type": "string",
                  "nullable": true
              }
Run Code Online (Sandbox Code Playgroud)

Ale*_*ets 5

您可以使用[Required]属性来表明该数据是有保证的。

[DisplayName("Location")]
    public class LocationDtoOut {

        [Required]
        public decimal lat { get; set; }

        [Required]
        public decimal lng { get; set; }

        public LocationDtoOut(decimal lat, decimal lng) {
            this.lat = lat;
            this.lng = lng;
        }
    }
Run Code Online (Sandbox Code Playgroud)