Swashbuckle.AspNetCore openapi 架构中未正确显示可空 属性

Nullable property is not presented in the Swashbuckle.AspNetCore openapi schema properly

asp.net core 项目中使用 Swashbuckle.AspNetCore v6.0.7 net5.0

假设我有这样的模型:

public enum MyEnum
{
  A, B
}

public class MyModel 
{
   public MyEnum MyEnum { get; set; }
   public MyEnum? MyEnum2 { get; set; }
}

并且 swagger 模式是这样生成的:

"MyEnum": {
        "enum": [
          "A",
          "B"
        ],
        "type": "string"
      },
      "MyModel": {
        "type": "object",
        "properties": {
          "myEnum": {
            "$ref": "#/components/schemas/MyEnum"
          },
          "myEnum2": {
            "$ref": "#/components/schemas/MyEnum"
          }
        },
        "additionalProperties": false
      }

如您所见,open-API JSON schema!MyEnumMyEnum? 没有区别!

似乎可为 null 的枚举未在架构中正确显示。

有人知道我该如何解决这个问题吗?

最佳

按照Yiyi You的建议,我在SwaggerGenOptions中这样调用了UseAllOfToExtendReferenceSchemas

services.AddSwaggerGen(c =>
{
       c.UseAllOfToExtendReferenceSchemas();
});

现在生成的模式如下:

"MyEnum": {
        "enum": [
          "A",
          "B"
        ],
        "type": "string"
      },
      "MyModel": {
        "type": "object",
        "properties": {
          "myEnum": {
            "allOf": [
              {
                "$ref": "#/components/schemas/MyEnum"
              }
            ]
          },
          "myEnum2": {
            "allOf": [
              {
                "$ref": "#/components/schemas/MyEnum"
              }
            ],
            "nullable": true
          }
        },
        "additionalProperties": false
      },

并且MyEnum?类型有"nullable": true

您可以找到更多信息here

感谢Yiyi You