Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

I want to create a spectral ruleset that checks whether 200 or 400 responses have a schema.$ref tag #2775

Open
nandeshwarshubh opened this issue Feb 4, 2025 · 0 comments
Labels
question Further information is requested

Comments

@nandeshwarshubh
Copy link

nandeshwarshubh commented Feb 4, 2025

I want to create a spectral ruleset that checks whether 200 or 400 responses have a schema.$ref tag

OPEN API YAML - myapi.yaml

openapi: 3.0.3
info:
  title: Simple API
  description: A simple OpenAPI specification with a single endpoint
  version: 1.0.0
paths:
  /example:
    post:
      summary: Example API endpoint
      description: Accepts a request DTO and returns a response DTO
      operationId: exampleOperation
      requestBody:
        required: true
        content:
          application/json:
            schema:
              $ref: '#/components/schemas/RequestDTO'
      responses:
        "200":
          description: Successful response
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/ResponseDTO'
        "400":
          description: Bad request
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/ExceptionDTO'
components:
  schemas:
    RequestDTO:
      type: object
      properties:
        id:
          type: string
          format: uuid
        name:
          type: string
      required:
        - id
        - name
    ResponseDTO:
      type: object
      properties:
        message:
          type: string
        timestamp:
          type: string
          format: date-time
      required:
        - message
        - timestamp
    ExceptionDTO:
      type: object
      properties:
        error:
          type: string
        details:
          type: string
      required:
        - error

My custom spectral ruleset - .spectral.yaml

rules:
  response-schema-ref-required:
    description: "Responses with status codes 200, 400, or 500 must have a schema with a $ref key."
    message: "Response schema must reference a schema via $ref."
    severity: error
    given: "$.paths[*][*].responses[200,400,500].content[*].schema"
    then:
      field: $ref
      function: truthy

run - npx spectral lint myapifile.yaml

Response -
/Users/myuser/Downloads/spectral-demo/myapifile.yaml
47:17 error response-schema-ref-required Response schema must reference a schema via $ref. components.schemas.ResponseDTO
59:18 error response-schema-ref-required Response schema must reference a schema via $ref. components.schemas.ExceptionDTO

Image

Why is it checking in components.schemas when we have specifically mentioned - given: "$.paths ?

@mnaumanali94 mnaumanali94 added the question Further information is requested label Feb 4, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

2 participants