Is OpenAPI v.3 incomplete or convertor is wrong?

Tags: , , ,



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?

Answer

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.



Source: stackoverflow