Class: OpenAPISourceTools::ApiObjects::Components

Inherits:
Object
  • Object
show all
Defined in:
lib/openapi/sourcetools/apiobjects.rb

Overview

A component in the API specification for reference and anchor handling.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path, prefix, ignored_keys = %w[summary description examples example $anchor])) ⇒ Components

Returns a new instance of Components.



45
46
47
48
49
50
51
52
53
54
# File 'lib/openapi/sourcetools/apiobjects.rb', line 45

def initialize(path, prefix, ignored_keys = %w[summary description examples example $anchor])
  path = "#/#{path.join('/')}/" if path.is_a?(Array)
  path = "#{path}/" unless path.end_with?('/')
  @path = path
  @prefix = prefix
  @anchor2ref = {}
  @schema_names = Set.new
  @items = {}
  @ignored_keys = Set.new(ignored_keys)
end

Instance Attribute Details

#anchor2refObject (readonly)

Returns the value of attribute anchor2ref.



42
43
44
# File 'lib/openapi/sourcetools/apiobjects.rb', line 42

def anchor2ref
  @anchor2ref
end

#ignored_keysObject

Returns the value of attribute ignored_keys.



43
44
45
# File 'lib/openapi/sourcetools/apiobjects.rb', line 43

def ignored_keys
  @ignored_keys
end

#itemsObject

Returns the value of attribute items.



43
44
45
# File 'lib/openapi/sourcetools/apiobjects.rb', line 43

def items
  @items
end

#pathObject (readonly)

Returns the value of attribute path.



42
43
44
# File 'lib/openapi/sourcetools/apiobjects.rb', line 42

def path
  @path
end

#prefixObject (readonly)

Returns the value of attribute prefix.



42
43
44
# File 'lib/openapi/sourcetools/apiobjects.rb', line 42

def prefix
  @prefix
end

#schema_namesObject (readonly)

Returns the value of attribute schema_names.



42
43
44
# File 'lib/openapi/sourcetools/apiobjects.rb', line 42

def schema_names
  @schema_names
end

Instance Method Details

#add_options(opts) ⇒ Object



56
57
58
59
60
61
62
63
# File 'lib/openapi/sourcetools/apiobjects.rb', line 56

def add_options(opts)
  opts.on('--use FIELD', 'Use FIELD in comparisons.') do |f|
    @ignored_keys.delete(f)
  end
  opts.on('--ignore FIELD', 'Ignore FIELD in comparisons.') do |f|
    @ignored_keys.add(f)
  end
end

#add_schema_name(name) ⇒ Object



69
70
71
# File 'lib/openapi/sourcetools/apiobjects.rb', line 69

def add_schema_name(name)
  @schema_names.add(name)
end

#alter_anchorsObject



102
103
104
105
106
107
108
109
110
111
112
# File 'lib/openapi/sourcetools/apiobjects.rb', line 102

def alter_anchors
  replacements = {}
  @anchor2ref.each_key do |a|
    next if @schema_names.member?(a)
    replacements[a] = ref_string(a)
    @schema_names.add(a)
  end
  replacements.each do |a, r|
    @anchor2ref[a] = r
  end
end

#anchor_ref_replacement(ref) ⇒ Object



114
115
116
# File 'lib/openapi/sourcetools/apiobjects.rb', line 114

def anchor_ref_replacement(ref)
  @anchor2ref[ref[1...ref.size]] || ref
end

#helpObject



65
66
67
# File 'lib/openapi/sourcetools/apiobjects.rb', line 65

def help
  %(All fields are used in object equality comparisons except:\n#{@ignored_keys.to_a.sort!.join("\n")})
end

#ref_string(name) ⇒ Object



73
74
75
76
# File 'lib/openapi/sourcetools/apiobjects.rb', line 73

def ref_string(name)
  return nil if name.nil?
  "#{@path}#{name}"
end

#reference(obj) ⇒ Object



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/openapi/sourcetools/apiobjects.rb', line 78

def reference(obj)
  # Check if identical schema has been added. If so, return the $ref string.
  @items.each do |k, v|
    return ref_string(k) if ApiObjects.same(obj, v, @ignored_keys)
  end
  # One of the numbers will not match existing keys. More number than keys.
  (@items.size + 1).times do |n|
    # 'x' is to simplify find and replace (Schema1x vs Schema1 and Schema10)
    cand = "#{@prefix}#{n}x"
    next if @items.key?(cand)
    @items[cand] = obj.merge
    @schema_names.add(cand)
    return ref_string(cand)
  end
end

#store_anchor(obj, ref = nil) ⇒ Object

Raises:

  • (StandardError)


94
95
96
97
98
99
100
# File 'lib/openapi/sourcetools/apiobjects.rb', line 94

def store_anchor(obj, ref = nil)
  anchor_name = obj['$anchor']
  return if anchor_name.nil?
  ref = obj['$ref'] if ref.nil?
  raise StandardError, 'ref is nil and no $ref or it is nil' if ref.nil?
  @anchor2ref[anchor_name] = ref
end