Class: Minitest::OpenAPI::Document

Inherits:
Object
  • Object
show all
Defined in:
lib/minitest/openapi/document.rb

Overview

The in-memory OpenAPI document. Tests record operations into it as they run; #to_h merges those operations into the base document’s ‘paths`.

Constant Summary collapse

VERB_ORDER =

Canonical ordering, so the emitted document is identical regardless of the order tests recorded into it (minitest randomizes test order).

%w[get put post patch delete options head trace].freeze
OPERATION_KEYS =
%w[
  tags summary description operationId parameters requestBody responses
].freeze

Instance Method Summary collapse

Constructor Details

#initialize(base) ⇒ Document

Returns a new instance of Document.



15
16
17
18
# File 'lib/minitest/openapi/document.rb', line 15

def initialize(base)
  @base = base
  @paths = {}
end

Instance Method Details

#componentsObject

The base document’s reusable schemas, used to resolve $refs while validating responses.



22
23
24
# File 'lib/minitest/openapi/document.rb', line 22

def components
  @base["components"] || {}
end

#empty?Boolean

Returns:

  • (Boolean)


62
63
64
# File 'lib/minitest/openapi/document.rb', line 62

def empty?
  @paths.empty?
end

#record(verb:, path:, status:, schema: nil, summary: nil, operation_id: nil, description: nil, tags: nil, parameters: nil, request_body: nil, response_description: nil, content_type: "application/json") ⇒ Object

Records one operation/response pair. Called by the recorder.



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/minitest/openapi/document.rb', line 27

def record(verb:, path:, status:, schema: nil, summary: nil, operation_id: nil,
  description: nil, tags: nil, parameters: nil, request_body: nil,
  response_description: nil, content_type: "application/json")
  operation = ((@paths[path] ||= {})[verb.to_s.downcase] ||= {})
  operation["summary"] ||= summary if summary
  operation["operationId"] ||= operation_id if operation_id
  operation["description"] ||= description if description
  operation["tags"] ||= tags if tags && !tags.empty?
  operation["parameters"] ||= parameters if parameters && !parameters.empty?
  operation["requestBody"] ||= request_body if request_body

  responses = (operation["responses"] ||= {})
  entry = (responses[status.to_s] ||= {})
  entry["description"] ||= response_description || "#{status} response"
  if schema
    entry["content"] ||= {content_type => {"schema" => schema}}
  end
  operation
end

#to_hObject

The assembled document: the base with recorded operations merged into ‘paths`. Paths, verbs, operation keys, and response statuses are all canonically ordered, so the output is stable across test runs.



50
51
52
53
54
55
56
57
58
59
60
# File 'lib/minitest/openapi/document.rb', line 50

def to_h
  doc = deep_dup(@base)
  base_paths = doc["paths"] || {}
  paths = {}
  (base_paths.keys | @paths.keys).sort.each do |path|
    merged = (base_paths[path] || {}).merge(@paths[path] || {})
    paths[path] = canonical_path_item(merged)
  end
  doc["paths"] = paths
  doc
end