当前位置: 首页 > 知识库问答 >
问题:

数据与“oneOfs”中的任何模式不匹配

咸星波
2023-03-14

在将api从.netcore2.2升级到3.1并尝试使用带有--v3开关的autorest生成后,我得到了这个错误

警告:架构冲突:数据与“Of之一”中的任何架构都不匹配

我尝试过使用和不使用序列化AsV2

我从Autorest文档中看到此警告是因为支持的功能。

anyOf, one Of当前不支持

服务业。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.

我可以从大摇大摆中看出。参数属性“name”未正确生成的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 行

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

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

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

  • https://github.com/domaindrivendev/Swashbuckle.AspNetCore#schema-filters
  • https://github.com/domaindrivendev/Swashbuckle.AspNetCore/pull/1766
  • https://stackoverflow.com/questions/63857310/could-not-find-a-part-of-the-path-d-dev-swashbuckle-aspnetcore-src-swashbuckle

共有1个答案

籍英叡
2023-03-14

从您的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"
  }
}
]
}

引导我们找到你的代码:

< code>type: "String"应为:< code>type: "string"全小写,错误消失

 类似资料:
  • 问题内容: 如何找到具有匹配模式的所有键的计数。 例如,有两个键和。常见的模式是。因此,这里的计数是2。 如何在Redis中做到这一点? 问题答案: 免责声明 我希望这个旧答案不会损坏任何具有数百万把钥匙的生产系统。如果出于某种原因仍要在生产中仍然计算redis的匹配键,最好使用具有匹配模式的scan。 如果仅使用KEYS进行搜索,并使用Redis客户端,您将获得所有匹配密钥的数字列表,对吗? 例

  • 问题内容: 我收到以下错误: 但是我看不到哪个参数错误? 这是我使用的代码。 我已经添加并删除了,但出现了相同的错误。 问题答案: 您将月份和日期交换了: 否则将永远不会适合month参数的范围。 随着并以正确的顺序解析的工作原理: 您无需添加;可以正确解析较短的数字:

  • 这是我的明显错误,我没有任何映射的TREX,*jamend,EOF模式。因此,它抛出以下异常: 我看了许多例子,这一个匹配接近,并改变了我的步骤如下,但仍然是同样的问题。 查看这里的Spring.io文档中的跳过记录(5.1.5配置跳过逻辑),也不起作用。 请让我知道绕过这个问题的理想方法。难道不应该有一种简单的方法来指定不匹配特定情况的跳过记录吗?请指教。谢了。 --- 我有一个'*'的模式映射

  • 模式匹配与匿名函数 上一章总结了模式在 Scala 中的几种用法,最后提到了匿名函数。 这一章,我们具体的去学习如何在匿名函数中使用模式。 如果你参与过 Coursera 上的 那门 Scala 课程 , 或者写过 Scala 代码,那很可能你已经熟悉匿名函数。 比如说,将一组歌名转换成小写格式,你可能会定义一个匿名函数传递给 map 方法: val songTitles = List("The

  • 函数组合 让我们创建两个函数: scala> def f(s: String) = "f(" + s + ")" f: (String)java.lang.String scala> def g(s: String) = "g(" + s + ")" g: (String)java.lang.String compose compose 组合其他函数形成一个新的函数 f(g(x)) scala>