开放API v.3不完整还是转换器错误?

huangapple go评论77阅读模式
英文:

Is OpenAPI v.3 incomplete or convertor is wrong?

问题

以下是翻译好的部分:

我正在尝试使用 Swagger 描述来验证 REST API 的一些数据。
我已经使用 Swagger 编辑器的转换选项将 swagger.json 转换成了 OpenAPI 3.0.1,现在正尝试使用 ObjectMapper 将其读取到 OpenAPI 对象中,代码如下:

File schemaFile = new File(path);
if (schemaFile.exists() && schemaFile.canRead()) {
    this.api = MAPPER.readValue(schemaFile, OpenAPI.class);
}

首先,我遇到了一个错误,因为它无法识别转换器生成的 x-codegen-request-body-name 字段。好的,这对于我的目的不重要,所以我已经删除了这个字段。
但接着我遇到了以下错误:

无法识别字段 "items"(类 io.swagger.v3.oas.models.media.Schema),未标记为可忽略的(已知的34个属性:...)\n 在 [来源:(BufferedInputStream);行:186,列:23] 处(通过引用链:io.swagger.v3.oas.models.OpenAPI["components"]->io.swagger.v3.oas.models.Components["schemas"]->java.util.LinkedHashMap["searchAttrList"]->io.swagger.v3.oas.models.media.Schema["properties"]->java.util.LinkedHashMap["merchantCategoryCode"]->io.swagger.v3.oas.models.media.Schema["items"])

在服务的请求中,有十多个属性,其中两个是 JSON 数组。
在我从转换器获得的 openapi.json 文件中,它们显示如下:

"merchantCategoryCode": {
    "type": "array",
    "description": "Merchant Category Codes of the Merchant",
    "items": {
        "type": "string"
    }
},

所以,是 OpenAPI 规范缺少数组定义,还是转换器做错了,我需要用其他什么东西替换 items 标记?如果是这样,应该用什么替换呢?

英文:

I am trying to validate some data for the REST API using swagger description.
I have converted swagger.json into OpenAPI 3.0.1 using swagger editor conversion option, and now trying to read it into OpenAPI object with ObjectMapper with following lines of code:

                File schemaFile = new File(path);
                if (schemaFile.exists() && schemaFile.canRead()) {
                    this.api = MAPPER.readValue(schemaFile, OpenAPI.class);
                }

First, I got an error as it could not recognized the field x-codegen-request-body-name generated by the converter. Fine, this is not important for my purposes, so I have deleted this field.
But then I got the following error:

> Unrecognized field "items" (class io.swagger.v3.oas.models.media.Schema), not marked as ignorable (34 known properties: "default", "multipleOf", "minimum", "exclusiveMinimum", "not", "extensions", "xml", "title", "discriminator", "required", "maximum", "nullable", "exclusiveMaximum", "exampleSetFlag", "minProperties", "externalDocs", "maxLength", "writeOnly", "uniqueItems", "properties", "maxProperties", "type", "maxItems", "enum", "minItems", "pattern", "minLength", "readOnly", "example", "$ref", "deprecated", "format", "additionalProperties", "description"])\n at [Source: (BufferedInputStream); line: 186, column: 23] (through reference chain: io.swagger.v3.oas.models.OpenAPI["components"]->io.swagger.v3.oas.models.Components["schemas"]->java.util.LinkedHashMap["searchAttrList"]->io.swagger.v3.oas.models.media.Schema["properties"]->java.util.LinkedHashMap["merchantCategoryCode"]->io.swagger.v3.oas.models.media.Schema["items"])

In the request to the service there are more than a dozen attributes and two of them are JSON arrays.
in the openapi.json file I got from the convertor they are shown like

      "merchantCategoryCode": {
        "type": "array",
        "description": "Merchant Category Codes of the Merchant",
        "items": {
          "type": "string"
        }
      },

So, is OpenAPI specification missing arrays definition, or is it converter doing it wrong and I need to replace items token with something else? If so, which one?

答案1

得分: 1

我找到了解决方法。
最初,我正在创建一个ObjectMapper来使用默认构造函数解析OpenAPI JSON文件:

private static final ObjectMapper MAPPER = new ObjectMapper();

这就是我遇到这个错误的地方。
我已将这行代码替换为

private static final ObjectMapper MAPPER = Json.mapper();

所有错误都消失了。显然,由io.swagger.v3.core.util.Json生成的对象映射器在某种程度上配置为处理所有OpenAPI结构。

英文:

I found the solution.
Originally I was creating an ObjectMapper to parse the OpenAPI JSON dile with a default constructor:

private static final ObjectMapper MAPPER = new ObjectMapper();

That's where I was getting this error.
I have replaced this line with

private static final ObjectMapper MAPPER = Json.mapper();

and all errors disappeared. Apparently, Object mapper produced by io.swagger.v3.core.util.Json is somehow configured to handle all OpenAPI constructs.

huangapple
  • 本文由 发表于 2020年10月17日 10:44:02
  • 转载请务必保留本文链接:https://go.coder-hub.com/64398525.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定