1

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

警告:架构违规:数据与“oneOf”中的任何架构都不匹配

我尝试过使用和不使用 SerializeAsV2

我从Autorest 文档中看到此警告是由于受支持的功能所致。

目前不支持 anyOf、oneOf

在 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 中看到参数属性“名称”没有正确生成。这里它包含“body”,而之前它包含“info”

"/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

[更新]

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

我尝试克隆 swashbuckle.aspnetcore 存储库,但遇到了这个问题

[更新]

我添加了 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)

调查 swagger.json 中的第 1951 行

导致错误的行

在工作大摇大摆(从 dotnet2.2 项目生成)中,json 看起来非常相似,但是交换了参数顺序 工作的json

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

我从这个问题中看到错误发生在同一个地方

[更新]当我将 --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 在评论中添加的一些链接:

4

1 回答 1

2

从您的 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"所有小写的错误消失了

于 2020-09-13T02:47:12.670 回答