数据与 'oneOf' 中的任何模式都不匹配

Data does not match any schemas from 'oneOf'

在将我的 api 从 .netcore2.2 升级到 3.1 并尝试使用带 --v3 开关的 autorest 生成后出现此错误

WARNING: Schema violation: Data does not match any schemas from 'oneOf'

我试过使用和不使用 SerializeAsV2

我从 the Autorest docs 看到这个警告是因为支持的功能。

anyOf, oneOf are not currently supported

在services.AddSwaggerGen我有

            c.ParameterFilter<SwaggerEnumParameterFilter>();
            c.SchemaFilter<SwaggerEnumFilter>();

哪里

public void Apply(OpenApiParameter parameter, ParameterFilterContext context)
{
    var type = context.ApiParameterDescription.Type;

    if (type.IsEnum)
        parameter.Extensions.Add("x-ms-enum", new OpenApiObject
        {
            ["name"] = new OpenApiString(type.Name),
            ["modelAsString"] = new OpenApiBoolean(false)
        });
     
}

public class SwaggerEnumFilter : ISchemaFilter
{
    public void Apply(OpenApiSchema model, SchemaFilterContext context)
    {
        if (model == null)
            throw new ArgumentNullException("model");
        if (context == null)
            throw new ArgumentNullException("context");
        if (context.Type.IsEnum)
            model.Extensions.Add(
                "x-ms-enum",
                new OpenApiObject
                {
                    ["name"] = new OpenApiString(context.Type.Name),
                    ["modelAsString"] = new OpenApiBoolean(false)
                }
            );
    }
}

[更新]

升级到 Autorest 3.0.6244 后,警告已更改为错误,错误消息以

结尾
post > parameters > 0)

如果我不使用 v3 开关,我会得到错误

FATAL: swagger-document/individual/schema-validator - FAILED
FATAL: Error: [OperationAbortedException] Error occurred. Exiting.
Process() cancelled due to exception : [OperationAbortedException] Error occurred. Exiting.

我可以在 swagger.json 中看到参数 属性“名称”未正确生成。这里它包含“正文”,而之前它包含“信息”

"/api/FrameLookUp": {
    "post": {
        "tags": [
            "Frame"
        ],
        "operationId": "FrameLookup",
        "consumes": [
            "application/json-patch+json",
            "application/json",
            "text/json",
            "application/*+json"
        ],
        "produces": [
            "application/json"
        ],
        "parameters": [
            {
                "in": "header",
                "name": "Authorization",
                "description": "access token",
                "required": true,
                "type": "String"
            },
            {
                "in": "body", 
                "name": "body",
                "schema": {
                    "$ref": "#/definitions/FrameRequest"
                }
            }
        ],
        "responses": {
            "200": {
                "description": "Success",
                "schema": {
                    "$ref": "#/definitions/FrameResponse"
                }
            }
        }
    }
},

控制器是

[Produces("application/json")]
[Authorize(AuthenticationSchemes = JwtBearerDefaults.AuthenticationScheme)]
[Route("api")]

public class FrameController : MyController
{
    [ProducesResponseType(typeof(FrameResponse), StatusCodes.Status200OK)]
    [HttpPost("FrameLookUp")]
    public IActionResult FrameLookup([FromBody] FrameRequest  info)
    {
        IMyResponse MyFunc(IMyRequest x) => FrameData.FrameLookUp(info);
        return InnerMethod(MyFunc, info);
    }
 }

更新

我也尝试过使用 Swashbuckle.AspNetCore.Annotations

中的 SwaggerParameter

[更新]

我在想也许我只需要尝试发布 issue 1766

我尝试将 swashbuckle.aspnetcore 存储库克隆 运行 到 this issue

[更新]

我添加了c.GeneratePolymorphicSchemas();添加到 AddSwaggerGen 选项,但没有帮助。

[更新] 这是第一条错误信息

ERROR: Schema violation: Data does not match any schemas from 'oneOf'
    - https://localhost:44348/api-docs/v1/swagger.json:1951:8 ($.paths["/api/synchronise-management/get-product-images-Ids"].post.parameters)

正在调查第 1951 行 swagger.json

在 working swagger(从 dotnet2.2 项目生成)中,json 看起来非常相似,但是参数顺序被交换了

我能看到的另一个区别是参数的生成名称

我从this question看到错误发生在同一个地方

[更新] 当我将 --debug 开关添加到 autorest 调用时,我得到

/configuration
DEBUG: pipeline-emitter - END
DEBUG: configuration-emitter - END
DEBUG: swagger-document-override/md-override-loader - END
DEBUG: swagger-document/loader - END
DEBUG: swagger-document/individual/transform - START
DEBUG: swagger-document/individual/transform - END
DEBUG: swagger-document/individual/schema-validator - START
ERROR: Schema violation: Data does not match any schemas from 'oneOf'
    - https://localhost:44348/api/v1/swagger.json:1951:8 ($.paths["/api/synchronise-management/get-product-images-Ids"].

[更新]

这是截下来的json

{
    "swagger": "2.0",
    "info": {
        "title": "myapi API31",
        "description": "ASP.NET Core Web API",
        "version": "v1"
    },
    "host": "localhost:44348",
    "basePath": "/v1",
    "schemes": [
        "https"
    ],
    "paths": {
        "/api/Test": {
            "get": {
                "tags": [
                    "Auth"
                ],
                "operationId": "Test",
                "responses": {
                    "200": {
                        "description": "Success"
                    }
                }
            }
        },
        "/api/RequestToken": {
            "post": {
                "tags": [
                    "Auth"
                ],
                "operationId": "RequestToken",
                "consumes": [
                    "application/json-patch+json",
                    "application/json",
                    "text/json",
                    "application/*+json"
                ],
                "produces": [
                    "application/json"
                ],
                "parameters": [
                    {
                        "in": "body",
                        "name": "body",
                        "schema": {
                            "$ref": "#/definitions/TokenRequest"
                        }
                    }
                ],
                "responses": {
                    "200": {
                        "description": "Success",
                        "schema": {
                            "$ref": "#/definitions/TokenResponse"
                        }
                    }
                }
            }
        },
        "/api/FrameLookUp": {
            "post": {
                "tags": [
                    "Frame"
                ],
                "operationId": "FrameLookup",
                "consumes": [
                    "application/json-patch+json",
                    "application/json",
                    "text/json",
                    "application/*+json"
                ],
                "produces": [
                    "application/json"
                ],
                "parameters": [
                    {
                        "in": "header",
                        "name": "Authorization",
                        "description": "access token",
                        "required": true,
                        "type": "String"
                    },
                    {
                        "in": "body",
                        "name": "body",
                        "schema": {
                            "$ref": "#/definitions/FrameRequest"
                        }
                    }
                ],
                "responses": {
                    "200": {
                        "description": "Success",
                        "schema": {
                            "$ref": "#/definitions/FrameResponse"
                        }
                    }
                }
            }
        } 
    },
    "definitions": {
        "TokenRequest": {
            "required": [
                "password",
                "username"
            ],
            "type": "object",
            "properties": {
                "username": {
                    "type": "string"
                },
                "password": {
                    "type": "string"
                }
            }
        },
        "TokenResponse": {
            "type": "object",
            "properties": {
                "tokenResult": {
                    "type": "string"
                }
            }
        },
        "FramePackTypeEnum": {
            "enum": [
                "NotApplicable",
                "PipeRack",
                "LwBVan",
                "VanTray",
                "Car",
                "CarryBag"
            ],
            "type": "string",
            "x-ms-enum": {
                "name": "FramePackTypeEnum",
                "modelAsString": false
            }
        },
        "FrameRequest": {
            "type": "object",
            "properties": {
                "qCodeJobId": {
                    "format": "int32",
                    "type": "integer"
                },
                "quantity": {
                    "format": "int32",
                    "type": "integer"
                },
                "widthInMm": {
                    "format": "int32",
                    "type": "integer"
                },
                "heightInMm": {
                    "format": "int32",
                    "type": "integer"
                },
                "ePackingType": {
                    "$ref": "#/definitions/FramePackTypeEnum"
                },
                "userEmail": {
                    "type": "string"
                }
            }
        },
        "FrameCaseEnum": {
            "enum": [
                "Case0_NoBraces",
                "Case1_1Vertical_0Horizontal",
                "Case2_2Vertical_0Horizontal",
                "Case3_NVertical_0Horizontal",
                "Case4_0Vertical_1Horizontal",
                "Case5_1Vertical_1Horizontal",
                "Case6_2Vertical_1Horizontal",
                "Case7_NVertical_1Horizontal",
                "Case8_0Vertical_2Horizontal",
                "Case9_1Vertical_2Horizontal",
                "Case10_2Vertical_2Horizontal",
                "Case11_NVertical_2Horizontal",
                "Case12_0Vertical_NHorizontal",
                "Case13_1Vertical_NHorizontal",
                "Case14_2Vertical_NHorizontal",
                "Case15_NVertical_NHorizontal"
            ],
            "type": "string",
            "x-ms-enum": {
                "name": "FrameCaseEnum",
                "modelAsString": false
            }
        },
        "FrameResponse": {
            "type": "object",
            "properties": {
                "description": {
                    "type": "string"
                },
                "caseNumber": {
                    "$ref": "#/definitions/FrameCaseEnum"
                },
                "memberPriceEachExGst": {
                    "format": "double",
                    "type": "number"
                },
                "retailPriceEachExGst": {
                    "format": "double",
                    "type": "number"
                }
            }
        }
    }
}

使用 .netcore2.2 api 请求生成为

"FrameRequest": {
    "type": "object",
    "properties": {
        "qCodeJobId": {
            "format": "int32",
            "type": "integer"
        },
        "quantity": {
            "format": "int32",
            "type": "integer"
        },
        "widthInMm": {
            "format": "int32",
            "type": "integer"
        },
        "heightInMm": {
            "format": "int32",
            "type": "integer"
        },
        "ePackingType": {
            "enum": [
                "NotApplicable",
                "PipeRack",
                "LwBVan",
                "VanTray",
                "Car",
                "CarryBag"
            ],
            "type": "string",
            "x-ms-enum": {
                "name": "FramePackTypeEnum",
                "modelAsString": false
            }
        },
        "userEmail": {
            "type": "string"
        }
    }
}

这里是命令行我是运行

autorest --input-file=.\myswagger.json --output-folder=generated --csharp --namespace=DDD --debug

作者 Kirsten Greed 在评论中提出的一些链接:

从您的 swagger.json 我们可以看到验证显示:
https://validator.swagger.io/validator/debug?url=https://raw.githubusercontent.com/heldersepu/hs-scripts/master/swagger/63783800_swagger.json

{
"schemaValidationMessages": [
{
  "level": "error",
  "domain": "validation",
  "keyword": "oneOf",
  "message": "instance failed to match exactly one schema (matched 0 out of 2)",
  "schema": {
    "loadingURI": "http://swagger.io/v2/schema.json#",
    "pointer": "/definitions/parametersList/items"
  },
  "instance": {
    "pointer": "/paths/~1api~1FrameLookUp/post/parameters/0"
  }
}
]
}

引导我们找到您的代码:

type: "String" 应该是:type: "string" 全部小写错误消失了