Using $ref for path parameter in openapi spec yaml causes linting issues in Spectral linter

183 Views Asked by At

I get this error when running spectral lint ./openapi/api.yaml

path-params Operation must define parameter "{itemUuid}" as expected by path "/item/{itemUuid}/childResource"

This is an example structure of my schema.

openapi/paths/item.yaml

get:
  summary: Retrieve a specific child resource of an item
  operationId: getItemChildResource
  description: Retrieves a specific child resource under an item. This operation can be used by customers to access the child resource associated with the item.
  tags:
    - Items
  parameters:
    - $ref: '../api.yaml#/components/parameters/VersionHeader'
    - $ref: '../api.yaml#/components/parameters/ItemUuidPathParam'
  responses:
    '200':
      description: Successfully retrieved the child resource
      content:
        application/json:
          schema:
            $ref: '../api.yaml#/components/schemas/Item'

openapi/schemas/item.yaml

type: object
properties:
  uuid:
    type: string
  name:
    type: string
  description:
    type: string

openapi/api.yaml

openapi: 3.1.0
info:
  title: Salable API
  description: |
    An example schema
version: v1

servers:
  - url: https://api.example.com

tags:
  - name: Items
    description: Operations related to products.

components:
  parameters:
    VersionHeader:
      in: header
      name: version
      required: true
      schema:
        type: string
        enum:
          - v1
    ItemUuidPathParam:
      in: path
      name: itemUuid
      schema:
        type: string
      required: true
      description: The unique identifier for the item.
  schemas:
    Item:
      $ref: './schemas/item.yaml'

paths:
  /items/{itemUuid}/childResource:
    $ref: './paths/item.yaml'

Reading the openapi documentation I can't see anything I'm doing wrong.

It's possibly a bug with Spectral linter, but I want to be sure I've not overlooked some error or another before raising an issue there. I'm new to writing openapi spec.

update

Actually, running this trimmed down example lints fine. I'm investigating further.

Adding a second resource using the same path parameter works too.

Looking over my actual schema I see that the first seven or so path parameters work fine, but all subsequent ones error.

update two

Seems to be an issue with the number of paths/refs in the schema. I get errors on certain schemas when paths are present, but if I comment out the other paths the linting errors vanish.

Does anyone have any suggestions for other ways to lint/validate my openapi schema?

update three

Moving all the paths and schemas to the api.yaml file resolved the issue. It's a shame but I can deal with it.

0

There are 0 best solutions below