OpenAPITools / openapi-generator

OpenAPI Generator allows generation of API client libraries (SDK generation), server stubs, documentation and configuration automatically given an OpenAPI Spec (v2, v3)
https://openapi-generator.tech
Apache License 2.0
21.75k stars 6.56k forks source link

[BUG] Erroneous error expecting `items` field on component schema #11142

Open Xavientois opened 2 years ago

Xavientois commented 2 years ago

Bug Report Checklist

Description

I am trying to generate client code for a schema, but it gives me an error saying

attribute components.schemas.Config.items is missing

This is confusing to me, as the 3.0.3 spec says:

items - Value MUST be an object and not an array. Inline or referenced schema MUST be of a Schema Object and not a standard JSON Schema. items MUST be present if the type is array. The type is not array, so I do not understand why it claims it is missing.

openapi-generator version
openapi-generator-cli 5.3.0
  commit : bb124e1
  built  : 2021-10-24T14:50:41Z
  source : https://github.com/openapitools/openapi-generator
  docs   : https://openapi-generator.tech/
OpenAPI declaration file content or url
openapi: '3.0.3'
info:
  version: 1.0.0
  title: Minimal Repro
paths:
  /ping:
    get:
      responses:
        '200':
          description: OK
components:
  schemas:
    Config:
      title: Config
      type: object
      properties:
        myProperty:
          type: array
          default: [a, b, c]
Steps to reproduce
chebureki commented 2 years ago

Same issue here

gwenaskell commented 2 years ago

I got the same issue (tested on v5.4.0, 6.0.0-beta and 4.3.1).

This is due to having a "type": "array" property without specifying the "items" schema for this property.

So the schema is indeed wrong but the error is misleading.

bennidhamma commented 1 year ago

I'm seeing this as well, and my type is an object, not an array, as it is for the OP ("The type is not array, so I do not understand why it claims it is missing")

fpinatares commented 1 year ago

It was happening to me as well... the only thing I found out is that it is only happening when you have an array within the object causing the issue. As in the example above, the Config object has a property called myProperty which is an array.

To fix this, I added the items property under the array within the object. So in the case of the example, the items property should go under myProperty like this:

openapi: '3.0.3'
info:
  version: 1.0.0
  title: Minimal Repro
paths:
  /ping:
    get:
      responses:
        '200':
          description: OK
components:
  schemas:
    Config:
      title: Config
      type: object
      properties:
        myProperty:
          type: array
          default: [a, b, c]
          items:
            type: string
sziem commented 1 year ago

I also ran into this with v7.0.0.

Putting together what Xavientois quoted from the specs

items - Value MUST be an object and not an array. Inline or referenced schema MUST be of a Schema Object and not a standard JSON Schema. items MUST be present if the type is array.

and what fpinatares mentioned about his array missing items, I believe that the actual bug is that the error points to the wrong object. It is the array that is missing items, not the object.

It should probably read something like

attribute components.schemas.Config.myProperty.items is missing

instead of

attribute components.schemas.Config.items is missing
evan-cxnpl commented 5 months ago

I've also run into this issue, and wondering if openapi-generator supports adding this missing items on an array through the mappings feature? (Or anything else to fix the issue without directly editing the spec?)