Class: OpenapiContracts::Doc::Schema

Inherits:
Object
  • Object
show all
Defined in:
lib/openapi_contracts/doc/schema.rb

Overview

Represents a part or the whole schema Generally even parts of the schema contain the whole schema, but store the pointer to their position in the overall schema. This allows even small sub-schemas to resolve links to any other part of the schema

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(raw, pointer = Doc::Pointer[]) ⇒ Schema

Returns a new instance of Schema.

Raises:

  • (ArgumentError)


9
10
11
12
13
14
# File 'lib/openapi_contracts/doc/schema.rb', line 9

def initialize(raw, pointer = Doc::Pointer[])
  raise ArgumentError unless pointer.is_a?(Doc::Pointer)

  @raw = raw
  @pointer = pointer.freeze
end

Instance Attribute Details

#pointerObject (readonly)

Returns the value of attribute pointer.



7
8
9
# File 'lib/openapi_contracts/doc/schema.rb', line 7

def pointer
  @pointer
end

#rawObject (readonly)

Returns the value of attribute raw.



7
8
9
# File 'lib/openapi_contracts/doc/schema.rb', line 7

def raw
  @raw
end

Instance Method Details

#at_pointer(pointer) ⇒ Object



63
64
65
# File 'lib/openapi_contracts/doc/schema.rb', line 63

def at_pointer(pointer)
  self.class.new(raw, pointer)
end

#eachObject

rubocop:disable Metrics/MethodLength



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/openapi_contracts/doc/schema.rb', line 16

def each # rubocop:disable Metrics/MethodLength
  data = resolve
  case data
  when Array
    enum = data.each_with_index
    Enumerator.new(enum.size) do |yielder|
      loop do
        _item, index = enum.next
        yielder << navigate(index.to_s)
      end
    end
  when Hash
    enum = data.each_key
    Enumerator.new(enum.size) do |yielder|
      loop do
        key = enum.next
        yielder << [key, navigate(key)]
      end
    end
  end
end

#follow_refsObject

Resolves Schema ref pointers links like “$ref: #/some/path” and returns new sub-schema at the target if the current schema is only a ref link.



46
47
48
49
50
51
52
53
# File 'lib/openapi_contracts/doc/schema.rb', line 46

def follow_refs
  data = resolve
  if data.is_a?(Hash) && data.key?('$ref')
    at_pointer Doc::Pointer.from_json_pointer(data['$ref'])
  else
    self
  end
end

#fragmentObject

Generates a fragment pointer for the current schema path



56
57
58
# File 'lib/openapi_contracts/doc/schema.rb', line 56

def fragment
  pointer.to_json_schemer_pointer
end

#inspectObject

:nocov:



39
40
41
# File 'lib/openapi_contracts/doc/schema.rb', line 39

def inspect
  "<#{self.class.name} @pointer=#{@pointer.inspect}>"
end


80
81
82
# File 'lib/openapi_contracts/doc/schema.rb', line 80

def navigate(*segments)
  self.class.new(@raw, pointer.navigate(segments)).follow_refs
end

#openapi_versionObject



67
68
69
# File 'lib/openapi_contracts/doc/schema.rb', line 67

def openapi_version
  @raw['openapi']&.then { |v| Gem::Version.new(v) }
end

#presenceObject



71
72
73
# File 'lib/openapi_contracts/doc/schema.rb', line 71

def presence
  resolve.present? ? self : nil
end

#resolveObject

Returns the actual sub-specification contents at the pointer of this Specification



76
77
78
# File 'lib/openapi_contracts/doc/schema.rb', line 76

def resolve
  @pointer.walk(@raw)
end