Sha256: c0541dfbcc2cff4a2ff694ee63d536ab3b17e2fbaeaaba2fc66f5251aa0b643a

Contents?: true

Size: 1.29 KB

Versions: 1

Compression:

Stored size: 1.29 KB

Contents

require 'uri'

module OpenAPIParser::Findable
  # @param [String] reference
  # @return [OpenAPIParser::Findable]
  def find_object(reference)
    reference = URI.unescape(reference)
    return self if object_reference == reference
    remote_reference = !reference.start_with?('#')
    return find_remote_object(reference) if remote_reference
    return nil unless reference.start_with?(object_reference)

    @find_object_cache = {} unless defined? @find_object_cache
    if (obj = @find_object_cache[reference])
      return obj
    end

    if (child = _openapi_all_child_objects[reference])
      @find_object_cache[reference] = child
      return child
    end

    _openapi_all_child_objects.values.each do |c|
      if (obj = c.find_object(reference))
        @find_object_cache[reference] = obj
        return obj
      end
    end

    nil
  end

  def purge_object_cache
    @purged = false unless defined? @purged

    return if @purged

    @find_object_cache = {}
    @purged = true

    _openapi_all_child_objects.values.each(&:purge_object_cache)
  end

  private

    def find_remote_object(reference)
      reference_uri = URI(reference)
      fragment = reference_uri.fragment
      reference_uri.fragment = nil
      root.load_another_schema(reference_uri)&.find_object("##{fragment}")
    end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
openapi_parser-0.12.0 lib/openapi_parser/concerns/findable.rb