Skip to the content.

Task 117 — Spectral lint для OpenAPI документа

Phase: 8 Status: pending Owner: — Slug: spectral-openapi-lint Studio Stage: 0 (pre-req для качества API контракта)

Goal

Настроить Spectral (Stoplight) для линтинга OpenAPI документа Hercules.WebApi. Spectral проверяет:

Linting запускается в CI на openapi.json (build-time generated, task_109).

Acceptance criteria

.spectral.yaml (пример)

extends:
  - spectral:oas
  # - owasp:api3:2019  # опционально, может быть шумно

rules:
  # Обязательные для Hercules codegen pipeline
  oas3-operation-operationId:
    description: Каждый operation должен иметь operationId (для Orval TS codegen)
    severity: error
    given: $.paths.*[get,post,put,patch,delete]
    then:
      function: truthy
      field: operationId

  oas3-operation-tags:
    description: Каждый operation должен иметь хотя бы один tag (для Orval tags-split)
    severity: error
    given: $.paths.*[get,post,put,patch,delete]
    then:
      function: truthy
      field: tags

  # Custom: PascalCase operationId
  hercules-operation-id-pascalcase:
    description: operationId должен быть в PascalCase (конвенция Hercules)
    severity: warn
    given: $.paths.*[get,post,put,patch,delete].operationId
    then:
      function: pattern
      field: operationId
      options:
        match: "^[A-Z][a-zA-Z0-9]*$"

  # Custom: response schema не пустой для success responses
  hercules-has-response-schema:
    description: Success responses (2xx) должны иметь схему (Produces<T>)
    severity: warn
    given: $.paths.*[get,post,put,patch,delete].responses[2*].content.application/json.schema
    then:
      function: truthy

Setup

Вариант 1: Global spectral (проще)

npm install -g @stoplight/spectral-cli
cd src/agent/Hercules.WebApi
spectral lint openapi.json

Вариант 2: В solution root (для CI)

# В корне репозитория
npm init -y  # если нет package.json
npm install --save-dev @stoplight/spectral-cli
// package.json (solution root)
{
  "scripts": {
    "lint:openapi": "spectral lint src/agent/Hercules.WebApi/openapi.json"
  }
}

CI integration

# .github/workflows/studio-ci.yml (или отдельный workflow)
- name: Build agent (generates openapi.json)
  run: dotnet build src/agent/Hercules.WebApi/Hercules.WebApi.csproj

- name: Lint OpenAPI
  run: npx @stoplight/spectral-cli lint src/agent/Hercules.WebApi/openapi.json

Dependencies

Scope / Likely files

Notes

Reference