class OasParser::Pointer

Public Class Methods

new(raw_pointer) click to toggle source
# File lib/oas_parser/pointer.rb, line 3
def initialize(raw_pointer)
  @raw_pointer = raw_pointer
end

Public Instance Methods

circular_reference?(path) click to toggle source

Detect circular reference by checking whether the ref exists in current path.

Example: components:

schemas:
  Pet:
    type: object
    properties:
      name:
        type: string
      children:
        type: array
        items: # <--- parsing here
          - $ref: '#/components/schemas/Pet'

path: “/components/schemas/Pet/properties/children/items” raw_pointer: “#/components/schemas/Pet”

It'd return true when we're parsing the pet children items where the ref points back to itself.

# File lib/oas_parser/pointer.rb, line 34
def circular_reference?(path)
  path.include?("#{escaped_pointer}/")
end
escaped_pointer() click to toggle source
# File lib/oas_parser/pointer.rb, line 38
def escaped_pointer
  if @raw_pointer.start_with?("#")
    Addressable::URI.unencode(@raw_pointer[1..-1])
  else
    @raw_pointer
  end
end
resolve(document) click to toggle source
# File lib/oas_parser/pointer.rb, line 7
def resolve(document)
  return document if escaped_pointer == ""

  tokens.reduce(document) do |nested_doc, token|
    nested_doc.fetch(token)
  end
end

Private Instance Methods

parse_token(token) click to toggle source
# File lib/oas_parser/pointer.rb, line 48
def parse_token(token)
  token.gsub("~0", "~").gsub("~1", "/")
end
tokens() click to toggle source
# File lib/oas_parser/pointer.rb, line 52
def tokens
  tokens = escaped_pointer[1..-1].split("/")

  if @raw_pointer.end_with?("/")
    tokens << ""
  end

  tokens.map do |token|
    parse_token(token)
  end
end