class Openapi3Parser::Document

Document is the root construct of a created OpenAPI Document and can be used to navigate the contents of a document or to check it's validity.

@attr_reader [String] openapi_version @attr_reader [Source] root_source @attr_reader [Array<String>] warnings

Constants

DEFAULT_OPENAPI_VERSION

The version of OpenAPI that will be used by default for validation/construction

SUPPORTED_OPENAPI_VERSIONS

A collection of the openapi versions that are supported

Attributes

build_in_progress[R]
built[R]
openapi_version[R]
reference_registry[R]
root_source[R]
warnings[R]

Public Class Methods

new(source_input) click to toggle source

@param [SourceInput] source_input

# File lib/openapi3_parser/document.rb, line 82
def initialize(source_input)
  @reference_registry = ReferenceRegistry.new
  @root_source = Source.new(source_input, self, reference_registry)
  @warnings = []
  @openapi_version = determine_openapi_version(root_source.data["openapi"])
  @build_in_progress = false
  @built = false
end

Public Instance Methods

errors() click to toggle source

Any validation errors that are present on the OpenAPI document

@return [Validation::ErrorCollection]

# File lib/openapi3_parser/document.rb, line 115
def errors
  reference_factories.inject(factory.errors) do |memo, f|
    Validation::ErrorCollection.combine(memo, f.errors)
  end
end
inspect() click to toggle source

@return [String]

# File lib/openapi3_parser/document.rb, line 156
def inspect
  %{#{self.class.name}(openapi_version: #{openapi_version}, } +
    %{root_source: #{root_source.inspect})}
end
node_at(pointer, relative_to = nil) click to toggle source

Look up a node at a particular location in the OpenAPI document

Examples:

document.node_at(“#/components/schemas”) document.node_at(%w[components schemas])

@param [Source::Pointer, String, Array] pointer @param [Source::Pointer, String, Array, nil] relative_to @return anything

# File lib/openapi3_parser/document.rb, line 151
def node_at(pointer, relative_to = nil)
  look_up_pointer(pointer, relative_to, root)
end
reference_sources() click to toggle source

All the additional sources that have been referenced as part of loading the OpenAPI document

@return [Array<Source>]

# File lib/openapi3_parser/document.rb, line 100
def reference_sources
  build unless built
  reference_registry.sources.reject(&:root?)
end
resolved_input_at(pointer, relative_to = nil) click to toggle source

Look up the resolved input for an address in the OpenAPI document, resolved_input refers to the input with references resolevd and all optional fields existing

@param [Source::Pointer, String, Array] pointer @param [Source::Pointer, String, Array, nil] relative_to @return anything

# File lib/openapi3_parser/document.rb, line 137
def resolved_input_at(pointer, relative_to = nil)
  look_up_pointer(pointer, relative_to, factory.resolved_input)
end
root() click to toggle source

@return [Node::Openapi]

# File lib/openapi3_parser/document.rb, line 92
def root
  @root ||= factory.node(Node::Context.root(factory.context))
end
source_for_source_input(source_input) click to toggle source

Look up whether an instance of SourceInput is already a known source for this document.

@param [SourceInput] source_input @return [Source, nil]

# File lib/openapi3_parser/document.rb, line 126
def source_for_source_input(source_input)
  sources.find { |source| source.source_input == source_input }
end
sources() click to toggle source

All of the sources involved in this OpenAPI document

@return [Array<Source>]

# File lib/openapi3_parser/document.rb, line 108
def sources
  [root_source] + reference_sources
end

Private Instance Methods

add_warning(text) click to toggle source
# File lib/openapi3_parser/document.rb, line 171
def add_warning(text)
  @warnings << text
end
build() click to toggle source
# File lib/openapi3_parser/document.rb, line 175
def build
  return if build_in_progress || built

  @build_in_progress = true
  context = NodeFactory::Context.root(root_source.data, root_source)
  @factory = NodeFactory::Openapi.new(context)
  reference_registry.freeze
  @warnings.freeze
  @build_in_progress = false
  @built = true
end
determine_openapi_version(version) click to toggle source
# File lib/openapi3_parser/document.rb, line 187
def determine_openapi_version(version)
  minor_version = (version || "").split(".").first(2).join(".")

  if SUPPORTED_OPENAPI_VERSIONS.include?(minor_version)
    minor_version
  elsif version
    add_warning(
      "Unsupported OpenAPI version (#{version}), treating as a " \
      "#{DEFAULT_OPENAPI_VERSION} document"
    )
    DEFAULT_OPENAPI_VERSION
  else
    add_warning(
      "Unspecified OpenAPI version, treating as a " \
      "#{DEFAULT_OPENAPI_VERSION} document"
    )
    DEFAULT_OPENAPI_VERSION
  end
end
factory() click to toggle source
# File lib/openapi3_parser/document.rb, line 207
def factory
  build unless built
  @factory
end
look_up_pointer(pointer, relative_pointer, subject) click to toggle source
# File lib/openapi3_parser/document.rb, line 165
def look_up_pointer(pointer, relative_pointer, subject)
  merged_pointer = Source::Pointer.merge_pointers(relative_pointer,
                                                  pointer)
  CautiousDig.call(subject, *merged_pointer.segments)
end
reference_factories() click to toggle source
# File lib/openapi3_parser/document.rb, line 212
def reference_factories
  build unless built
  reference_registry.factories.reject { |f| f.context.source.root? }
end