spectral icon indicating copy to clipboard operation
spectral copied to clipboard

Rule array-items does not support prefixItems keyword

Open yethee opened this issue 1 year ago • 2 comments

Describe the bug OpenAPI 3.1 inherits data types from JSON Schema 2020-12, where we can use prefixItems keyword to define tuple value, instead of items keyword. After changes from #2638 we got an error, when schema contains prefixItems:

Schemas with "type: array", require a sibling "items" field

To Reproduce

test.yaml:

---
openapi: "3.1.0"
components:
  schemas:
    IssueWithPrefixItems:
      type: array
      prefixItems:
        - type: string
        - type: integer
      minItems: 2
      maxItems: 2
      additionalItems: false

Run validation:

npx @stoplight/spectral-cli lint test.yaml

5:26    error  array-items            Schemas with "type: array", require a sibling "items" field  components.schemas.IssueWithPrefixItems

Expected behavior Field items should not be required when prefixItems field is defined.

Environment (remove any that are not applicable):

npm ls @stoplight/spectral-rulesets

`-- @stoplight/[email protected]
  +-- @stoplight/[email protected]
  | `-- @stoplight/[email protected] deduped
  `-- @stoplight/[email protected]

Additional context This issue related to #2638.

yethee avatar Jun 24 '24 14:06 yethee

@mnaumanali94 this sounds like a bug. Where OAS 3.1, supports prefixItems, items, unevaluatedItems or contains (more?).

ponelat avatar Aug 23 '24 12:08 ponelat

I just encountered this on a FastAPI project, which generates OpenAPI 3.1 schemas by default and uses prefixItems for any value defined as a tuple. That means that the kind of code a Python developer would normally write like this:

SortItem = tuple[str, SortDirection]
…
    sort: list[SortItem] | None = Field(
        title="Sort Results",
        default=[("score", SortDirection.desc)],
    )

… will produce a schema definition like this:

{
  "anyOf": [
    {
      "items": {
        "prefixItems": [
          {
            "type": "string"
          },
          {
            "$ref": "#/components/schemas/SortDirection"
          }
        ],
        "type": "array",
        "maxItems": 2,
        "minItems": 2
      },
      "type": "array"
    },
    {
      "type": "null"
    }
  ],
  "title": "Sort Results",
  "default": [
    [
      "score",
      "desc"
    ]
  ],

acdha avatar Oct 04 '24 20:10 acdha