[英]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?
如您所见, MyEnum
和MyEnum?
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.