module OpenAPIParser

Constants

VERSION

Public Class Methods

load(filepath, config = {}) click to toggle source

Load schema in specified filepath. If file path is relative, it is resolved using working directory. @return [OpenAPIParser::Schemas::OpenAPI]

# File lib/openapi_parser.rb, line 37
def load(filepath, config = {})
  load_uri(file_uri(filepath), config: Config.new(config), schema_registry: {})
end
load_uri(uri, config:, schema_registry:) click to toggle source

Load schema located by the passed uri. Uri must be absolute. @return [OpenAPIParser::Schemas::OpenAPI]

# File lib/openapi_parser.rb, line 43
def load_uri(uri, config:, schema_registry:)
  # Open-uri doesn't open file scheme uri, so we try to open file path directly
  # File scheme uri which points to a remote file is not supported.
  content = if uri.scheme == 'file'
    open(uri.path, &:read)
  else
    uri.open(&:read)
  end

  extension = Pathname.new(uri.path).extname
  load_hash(parse_file(content, extension), config: config, uri: uri, schema_registry: schema_registry)
end
parse(schema, config = {}) click to toggle source

Load schema hash object. Uri is not set for returned schema. @return [OpenAPIParser::Schemas::OpenAPI]

# File lib/openapi_parser.rb, line 23
def parse(schema, config = {})
  load_hash(schema, config: Config.new(config), uri: nil, schema_registry: {})
end
parse_with_filepath(schema, filepath, config = {}) click to toggle source

@param filepath [String] Path of the file containing the passed schema.

Used for resolving remote $ref if provided.
If file path is relative, it is resolved using working directory.

@return [OpenAPIParser::Schemas::OpenAPI]

# File lib/openapi_parser.rb, line 31
def parse_with_filepath(schema, filepath, config = {})
  load_hash(schema, config: Config.new(config), uri: filepath && file_uri(filepath), schema_registry: {})
end

Private Class Methods

file_uri(filepath) click to toggle source
# File lib/openapi_parser.rb, line 58
def file_uri(filepath)
  path = Pathname.new(filepath)
  path = Pathname.getwd + path if path.relative?
  URI.join("file:///",  path.to_s)
end
load_hash(hash, config:, uri:, schema_registry:) click to toggle source
# File lib/openapi_parser.rb, line 91
def load_hash(hash, config:, uri:, schema_registry:)
  root = Schemas::OpenAPI.new(hash, config, uri: uri, schema_registry: schema_registry)

  OpenAPIParser::ReferenceExpander.expand(root) if config.expand_reference

  # TODO: use callbacks
  root.paths&.path&.values&.each do | path_item |
    path_item.set_path_item_to_operation
  end

  root
end
parse_file(content, extension) click to toggle source
# File lib/openapi_parser.rb, line 64
def parse_file(content, extension)
  case extension.downcase
  when '.yaml', '.yml'
    parse_yaml(content)
  when '.json'
    parse_json(content)
  else
    # When extension is something we don't know, try to parse as json first. If it fails, parse as yaml
    begin
      parse_json(content)
    rescue JSON::ParserError
      parse_yaml(content)
    end
  end
end
parse_json(content) click to toggle source
# File lib/openapi_parser.rb, line 87
def parse_json(content)
  JSON.parse(content)
end
parse_yaml(content) click to toggle source
# File lib/openapi_parser.rb, line 80
def parse_yaml(content)
  # FIXME: when drop ruby 2.5, we should use permitted_classes
  (Gem::Version.create(RUBY_VERSION) < Gem::Version.create("2.6.0")) ? 
    Psych.safe_load(content, [Date, Time]) : 
    Psych.safe_load(content, permitted_classes: [Date, Time])
end