Module: OpenAPISourceTools::ApiObjects

Defined in:
lib/openapi/sourcetools/apiobjects.rb

Overview

Various classes for handling objects in the API specification. Used in various programs.

Defined Under Namespace

Classes: Components, ServerAlternatives, ServerObject, ServerPath, ServerVariableObject

Class Method Summary collapse

Class Method Details

.operation_objects(path_item) ⇒ Object



177
178
179
180
181
182
183
184
185
186
187
188
189
# File 'lib/openapi/sourcetools/apiobjects.rb', line 177

def self.operation_objects(path_item)
  keys = %w[operationId requestBody responses callbacks]
  out = {}
  path_item.each do |method, op|
    next unless op.is_a?(Hash)
    keys.each do |key|
      next unless op.key?(key)
      out[method] = op
      break
    end
  end
  out
end

.ref_string(name, schema_path) ⇒ Object



21
22
23
# File 'lib/openapi/sourcetools/apiobjects.rb', line 21

def self.ref_string(name, schema_path)
  "#{schema_path}/#{name}"
end

.reference(obj, schemas, schema_path, ignored_keys = Set.new(%w[summary description]), prefix = 'Schema') ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/openapi/sourcetools/apiobjects.rb', line 25

def self.reference(obj, schemas, schema_path, ignored_keys = Set.new(%w[summary description]), prefix = 'Schema')
  # Check if identical schema has been added and if so, return the $ref string.
  schemas.keys.sort.each do |k|
    return ref_string(k, schema_path) if same(obj, schemas[k], ignored_keys)
  end
  # One of the numbers will not match existing keys. More number than keys.
  (schemas.size + 1).times do |n|
    # 'x' is to simplify find and replace (Schema1x vs Schema1 and Schema10)
    k = "#{prefix}#{n}x"
    next if schemas.key?(k)
    schemas[k] = obj.merge
    return ref_string(k, schema_path)
  end
end

.same(a, b, ignored_keys = Set.new(%w[summary description])) ⇒ Object



11
12
13
14
15
16
17
18
19
# File 'lib/openapi/sourcetools/apiobjects.rb', line 11

def self.same(a, b, ignored_keys = Set.new(%w[summary description]))
  return a == b unless a.is_a?(Hash) && b.is_a?(Hash)
  keys = Set.new(a.keys + b.keys) - ignored_keys
  keys.to_a.each do |k|
    return false unless a.key?(k) && b.key?(k)
    return false unless same(a[k], b[k], ignored_keys)
  end
  true
end