Module: DocsKit::OpenApi

Defined in:
lib/docs_kit/open_api.rb,
lib/docs_kit/open_api/schema.rb,
lib/docs_kit/open_api/document.rb,
lib/docs_kit/open_api/operation.rb

Overview

The OpenAPI-bridge entry point. Loads an OpenAPI 3.x spec (a file path or an already-parsed Hash) into a narrow, gem-owned object model — a DocsKit::OpenApi::Document yielding DocsKit::OpenApi::Operation objects — that exposes ONLY what the existing render targets consume (FieldTable rows, ErrorTable rows, example bodies, code samples). No runtime parser dependency: YAML via Psych, JSON via the stdlib.

doc = DocsKit::OpenApi.load("openapi.yaml")
op  = doc.operation("createInvoice")   # or doc.operation(:post, "/v1/invoices")
op.body_rows      # => [{ name:, type:, required:, description: }, ...]  (FieldTable)
op.error_rows     # => [{ scenario:, status:, type: }, ...]             (ErrorTable)
op.success_example# => a Hash → JsonResponse

DocsUI::OpenApiOperation renders one Operation through the kit; the operation page helper looks it up on DocsKit.configuration.openapi_document.

Defined Under Namespace

Classes: Document, Operation, OperationNotFound, Schema, UnsupportedRef

Constant Summary collapse

PERMITTED_YAML_CLASSES =

YAML types a real-world spec might legitimately carry (dates in examples).

[Date, Time].freeze

Class Method Summary collapse

Class Method Details

.load(source) ⇒ Object

Load a spec from a String/Pathname path (.json parsed as JSON, anything else as YAML) or from an already-parsed Hash. Returns a Document.



39
40
41
# File 'lib/docs_kit/open_api.rb', line 39

def load(source)
  Document.new(source.is_a?(Hash) ? source : parse_file(source))
end

.parse_file(path) ⇒ Object

Parse a file by extension: JSON for .json, YAML (with alias support) otherwise. Kept module-level so Document#load-style reloads share it.



45
46
47
48
49
50
51
52
53
# File 'lib/docs_kit/open_api.rb', line 45

def parse_file(path)
  pathname = Pathname.new(path)
  contents = pathname.read
  if pathname.extname.casecmp?(".json")
    JSON.parse(contents)
  else
    YAML.safe_load(contents, aliases: true, permitted_classes: PERMITTED_YAML_CLASSES)
  end
end