Class: Openapi3Parser::Document

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Includes:
Enumerable
Defined in:
lib/openapi3_parser/document.rb,
lib/openapi3_parser/document/reference_registry.rb

Overview

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.

Defined Under Namespace

Classes: ReferenceRegistry

Constant Summary collapse

SUPPORTED_OPENAPI_VERSIONS =

A collection of the openapi versions that are supported

%w[3.0].freeze
DEFAULT_OPENAPI_VERSION =

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

"3.0"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(source_input) ⇒ Document

Returns a new instance of Document.

Parameters:


82
83
84
85
86
87
88
89
# 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

Instance Attribute Details

#openapi_versionString (readonly)

Returns the current value of openapi_version.

Returns:

  • (String)

    the current value of openapi_version


12
13
14
# File 'lib/openapi3_parser/document.rb', line 12

def openapi_version
  @openapi_version
end

#root_sourceSource (readonly)

Returns the current value of root_source.

Returns:

  • (Source)

    the current value of root_source


12
13
14
# File 'lib/openapi3_parser/document.rb', line 12

def root_source
  @root_source
end

#warningsArray<String> (readonly)

Returns the current value of warnings.

Returns:

  • (Array<String>)

    the current value of warnings


12
13
14
# File 'lib/openapi3_parser/document.rb', line 12

def warnings
  @warnings
end

Instance Method Details

#[]Object

Look up an attribute on the root of the OpenAPI document by String or Symbol

Returns:

  • Object

See Also:


77
78
79
# File 'lib/openapi3_parser/document.rb', line 77

def_delegators :root, :openapi, :info, :servers, :paths, :components,
:security, :tags, :external_docs, :extension, :[], :each,
:keys

#componentsNode::Components

The value of the components field on the OpenAPI document


77
78
79
# File 'lib/openapi3_parser/document.rb', line 77

def_delegators :root, :openapi, :info, :servers, :paths, :components,
:security, :tags, :external_docs, :extension, :[], :each,
:keys

#eachObject

Iterate through the attributes of the root object


77
78
79
# File 'lib/openapi3_parser/document.rb', line 77

def_delegators :root, :openapi, :info, :servers, :paths, :components,
:security, :tags, :external_docs, :extension, :[], :each,
:keys

#errorsValidation::ErrorCollection

Any validation errors that are present on the OpenAPI document


115
116
117
118
119
# 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

#extensionHash, ...

Look up an extension field provided for the root object of the document

Returns:

  • (Hash, Array, Numeric, String, true, false, nil)

See Also:


77
78
79
# File 'lib/openapi3_parser/document.rb', line 77

def_delegators :root, :openapi, :info, :servers, :paths, :components,
:security, :tags, :external_docs, :extension, :[], :each,
:keys

#external_docsNode::ExternalDocumentation?

The value of the external_docs field on the OpenAPI document


77
78
79
# File 'lib/openapi3_parser/document.rb', line 77

def_delegators :root, :openapi, :info, :servers, :paths, :components,
:security, :tags, :external_docs, :extension, :[], :each,
:keys

#infoNode::Info

The value of the info field on the OpenAPI document

Returns:

See Also:


77
78
79
# File 'lib/openapi3_parser/document.rb', line 77

def_delegators :root, :openapi, :info, :servers, :paths, :components,
:security, :tags, :external_docs, :extension, :[], :each,
:keys

#inspectString

Returns:

  • (String)

156
157
158
159
# File 'lib/openapi3_parser/document.rb', line 156

def inspect
  %{#{self.class.name}(openapi_version: #{openapi_version}, } +
    %{root_source: #{root_source.inspect})}
end

#keysObject

Access keys of the root object


77
78
79
# File 'lib/openapi3_parser/document.rb', line 77

def_delegators :root, :openapi, :info, :servers, :paths, :components,
:security, :tags, :external_docs, :extension, :[], :each,
:keys

#node_at(pointer, relative_to = nil) ⇒ Object

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

Examples:

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

Parameters:

Returns:

  • anything


151
152
153
# File 'lib/openapi3_parser/document.rb', line 151

def node_at(pointer, relative_to = nil)
  look_up_pointer(pointer, relative_to, root)
end

#openapiString

The value of the openapi version field for this document

Returns:

  • (String)

See Also:


77
78
79
# File 'lib/openapi3_parser/document.rb', line 77

def_delegators :root, :openapi, :info, :servers, :paths, :components,
:security, :tags, :external_docs, :extension, :[], :each,
:keys

#pathsNode::Paths

The value of the paths field on the OpenAPI document

Returns:

See Also:


77
78
79
# File 'lib/openapi3_parser/document.rb', line 77

def_delegators :root, :openapi, :info, :servers, :paths, :components,
:security, :tags, :external_docs, :extension, :[], :each,
:keys

#reference_sourcesArray<Source>

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

Returns:


100
101
102
103
# 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) ⇒ Object

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

Parameters:

Returns:

  • anything


137
138
139
# 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

#rootNode::Openapi

Returns:


92
93
94
# File 'lib/openapi3_parser/document.rb', line 92

def root
  @root ||= factory.node(Node::Context.root(factory.context))
end

#securityNode::Array<Node::SecurityRequirement>

The value of the security field on the OpenAPI document


77
78
79
# File 'lib/openapi3_parser/document.rb', line 77

def_delegators :root, :openapi, :info, :servers, :paths, :components,
:security, :tags, :external_docs, :extension, :[], :each,
:keys

#serversNode::Array<Node::Server>

The value of the servers field on the OpenAPI document


77
78
79
# File 'lib/openapi3_parser/document.rb', line 77

def_delegators :root, :openapi, :info, :servers, :paths, :components,
:security, :tags, :external_docs, :extension, :[], :each,
:keys

#source_for_source_input(source_input) ⇒ Source?

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

Parameters:

Returns:


126
127
128
# File 'lib/openapi3_parser/document.rb', line 126

def source_for_source_input(source_input)
  sources.find { |source| source.source_input == source_input }
end

#sourcesArray<Source>

All of the sources involved in this OpenAPI document

Returns:


108
109
110
# File 'lib/openapi3_parser/document.rb', line 108

def sources
  [root_source] + reference_sources
end

#tagsNode::Array<Node::Tag>

The value of the tags field on the OpenAPI document

Returns:

See Also:


77
78
79
# File 'lib/openapi3_parser/document.rb', line 77

def_delegators :root, :openapi, :info, :servers, :paths, :components,
:security, :tags, :external_docs, :extension, :[], :each,
:keys

#valid?Boolean

Whether this OpenAPI document has any validation issues or not. See #errors to access the errors

Returns:

  • (Boolean)

30
# File 'lib/openapi3_parser/document.rb', line 30

def_delegator :factory, :valid?