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 = nil) ⇒ Schema

Returns a new instance of Schema.



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

def initialize(raw, pointer = nil)
  @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

#as_hObject



35
36
37
38
39
# File 'lib/openapi_contracts/doc/schema.rb', line 35

def as_h
  return @raw if pointer.nil? || pointer.empty?

  @raw.dig(*pointer)
end

#at_pointer(pointer) ⇒ Object



31
32
33
# File 'lib/openapi_contracts/doc/schema.rb', line 31

def at_pointer(pointer)
  self.class.new(raw, pointer)
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.



16
17
18
19
20
21
22
# File 'lib/openapi_contracts/doc/schema.rb', line 16

def follow_refs
  if (ref = as_h['$ref'])
    at_pointer(ref.split('/')[1..])
  else
    self
  end
end

#fragmentObject

Generates a fragment pointer for the current schema path



25
26
27
# File 'lib/openapi_contracts/doc/schema.rb', line 25

def fragment
  pointer.map { |p| p.gsub('/', '~1') }.join('/').then { |s| "#/#{s}" }
end


41
42
43
# File 'lib/openapi_contracts/doc/schema.rb', line 41

def navigate(*spointer)
  self.class.new(@raw, (pointer + Array.wrap(spointer)))
end