Module: Scorpio::OpenAPI::Reference

Included in:
V2::JsonReference, V3::Reference
Defined in:
lib/scorpio/openapi/reference.rb

Instance Method Summary collapse

Instance Method Details

#[](token, **kw) ⇒ Object

overrides JSI::Base#[] to implicitly dereference this Reference, except when the given token is present in this Reference's instance (this should usually only apply to the token '$ref')



9
10
11
12
13
14
15
16
# File 'lib/scorpio/openapi/reference.rb', line 9

def [](token, **kw)
  if respond_to?(:to_hash) && !key?(token)
    deref do |deref_jsi|
      return(deref_jsi[token, **kw])
    end
  end
  return super
end

#deref {|JSI::Base| ... } ⇒ JSI::Base

yields or returns the target of this reference

Yields:

  • (JSI::Base)

    if a block is given

Returns:

  • (JSI::Base)


21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/scorpio/openapi/reference.rb', line 21

def deref
  return unless respond_to?(:to_hash) && self['$ref'].respond_to?(:to_str)

  ref_uri = Addressable::URI.parse(self['$ref'])
  ref_uri_nofrag = ref_uri.merge(fragment: nil)

  if !ref_uri_nofrag.empty? || ref_uri.fragment.nil?
    raise(NotImplementedError,
      "Scorpio currently only supports fragment URIs as OpenAPI references. cannot find reference by uri: #{self['$ref']}"
    )
  end

  ptr = JSI::Ptr.from_fragment(ref_uri.fragment)
  deref_jsi = ptr.evaluate(jsi_root_node)

  # TODO type check deref_jsi

  yield deref_jsi if block_given?

  deref_jsi
end