简体   繁体   English

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

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

Using Swashbuckle.AspNetCore v6.0.7 in an asp.net core project net5.0 .asp.net core项目net5.0中使用Swashbuckle.AspNetCore v6.0.7 v6.0.7。

Let say I have Models Like these:假设我有这样的模型:

public enum MyEnum
{
  A, B
}

and

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

and the swagger schema is generated like this:并且 swagger 模式生成如下:

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

As you can see, there is no difference between MyEnum and MyEnum?如您所见, MyEnumMyEnum? in the open-API JSON schema!在开放 API JSON 架构中!

And seems nullable enum is not presented in the schema properly.并且似乎可以为空的枚举未正确显示在架构中。

Does anyone have any idea how can I fix this?有谁知道我该如何解决这个问题?

Best最好的

As Yiyi You suggested, I called UseAllOfToExtendReferenceSchemas in SwaggerGenOptions like this:正如Yiyi You建议的那样,我在SwaggerGenOptions中调用了UseAllOfToExtendReferenceSchemas ,如下所示:

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

and now the schema is generated like:现在生成的模式如下:

"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
      },

and there is "nullable": true for the type MyEnum?并且有"nullable": true MyEnum? . .

You can find more information here .您可以在此处找到更多信息。

Thanks to Yiyi You .感谢Yiyi You

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用 Swashbuckle.AspNetCore 在 Swagger 模式中将自定义泛型类型公开为字符串 - How to expose a custom generic type as a string in Swagger schema using Swashbuckle.AspNetCore 可选参数在Swashbuckle.AspNetCore中导致空异常 - Optional parameter causes null exception in Swashbuckle.AspNetCore Swashbuckle.AspNetCore-无法为继承基本控制器的控制器生成json - Swashbuckle.AspNetCore - Unable to generate json for controllers that inherit base controllers 迁移到 Swashbuckle.AspNetCore 版本 5 时,Swagger UI 中的不记名身份验证 - Bearer authentication in Swagger UI, when migrating to Swashbuckle.AspNetCore version 5 无法解析 .net5.0 的“Swashbuckle.AspNetCore (>= 5.6.3)” - Unable to resolve 'Swashbuckle.AspNetCore (>= 5.6.3)' for 'net5.0' 多版本控制 URL 在 Swashbuckle.AspNetCore 5.0.0rc 中不起作用 - Multiple versioning URL not working in Swashbuckle.AspNetCore 5.0.0rc Swashbuckle.AspNetCore (5.0.0-rc5) 忽略公共字段 - Swashbuckle.AspNetCore (5.0.0-rc5) ignoring public fields 如何使用Swashbuckle.AspNetCore隐藏响应代码200? - How can I hide response code 200 with Swashbuckle.AspNetCore? swashbuckle.aspnetcore 是否支持 asp.net core 3.0? - Is swashbuckle.aspnetcore supporting asp.net core 3.0? Swashbuckle.AspNetCore是否支持FluentValidation而不是DataAnnotations? - Does Swashbuckle.AspNetCore support FluentValidation instead of DataAnnotations?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM