Skip to content

Object Schema element with items where item is an object and object has enum, it this representation is a wrong representation? #1989

@zhang435

Description

@zhang435

Having the schema defined like this

{
  "openapi": "3.0.0",
  "servers": [
    {
      "url": "/"
    }
  ],
  "info": {
    "title": "Foo",
    "version": "1.0.0",
    "description": ":)\n"
  },
  "tags": [
    {
      "name": "foo",
      "description": "Operations related to foo"
    }
  ],
  "paths": {
    "/Foo": {
      "post": {
        "requestBody": {
          "content": {
            "application/json": {
              "schema": {
                "additionalProperties": false,
                "properties": {
                  "apples": {
                    "$ref": "#/components/schemas/apples"
                  }
                },
                "type": "object"
              }
            }
          }
        },
        "responses": {
          "200": {
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/whatever"
                }
              }
            },
            "description": "The new legal entity"
          }
        }
      }
    }
  },
  "components": {
    "schemas": {



      "apples": {
        "items": {
          "additionalProperties": false,
          "properties": {
            "type": {
              "$ref": "#/components/schemas/apple"
            }
          },
          "type": "object"
        },
        "type": "array",
        "nullable": true,
        "x-label": "apples"
      },

      "apple": {
        "enum": [
          "good","bad"
        ],
        "type": "string",
        "x-label": "rate"
      }
    }
  }
}

It is an array of object and objetc has enum in there.
In the schema defined above, the enum reference won't properly be resolved.

if I change the schema to

{
  "openapi": "3.0.0",
  "servers": [
    {
      "url": "/"
    }
  ],
  "info": {
    "title": "Foo",
    "version": "1.0.0",
    "description": ":)\n"
  },
  "tags": [
    {
      "name": "foo",
      "description": "Operations related to foo"
    }
  ],
  "paths": {
    "/Foo": {
      "post": {
        "requestBody": {
          "content": {
            "application/json": {
              "schema": {
                "additionalProperties": false,
                "properties": {
                  "Apple": {
                    "$ref": "#/components/schemas/Apples"
                  }
                },
                "type": "object"
              }
            }
          }
        },
        "responses": {
          "200": {
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Whatever"
                }
              }
            },
            "description": "The new legal entity"
          }
        }
      }
    }
  },
  "components": {
    "schemas": {
      "Apples": {
        "items": {
          "$ref": "#/components/schemas/Apple"
        },
        "type": "array",
        "nullable": true,
        "x-label": "Apples"
      },
      "Apple": {
        "additionalProperties": false,
        "properties": {
          "type": {
            "$ref": "#/components/schemas/rate"
          }
        },
        "type": "object"
      },

      "rate": {
        "enum": [
          "good", "bad"
        ],
        "type": "string",
        "x-label": "rate"
      }
    }
  }
}

Then it will resolve.

The major difference is, rather than define the object in the items directly, I $ref it. As many examples mentioned.

However, I don't think the schema I had in the first one is wrong. As what I can find about the restriction for items
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 an array.

Please help me to verify that the first schema is valid.
I already know the line that resolves the reference, if the first one is valid. I will send out the fix on swagger-parser right away.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions