Module: OpenAPIParser

Defined in:
lib/openapi_parser.rb,
lib/openapi_parser/errors.rb,
lib/openapi_parser/version.rb

Defined Under Namespace

Modules: Expandable, Findable, MediaTypeSelectable, ParameterValidatable, Parser, Schemas Classes: Config, InvalidDateFormat, InvalidDateTimeFormat, InvalidEmailFormat, InvalidPattern, InvalidUUIDFormat, LessThanExclusiveMinimum, LessThanMinItems, LessThanMinLength, LessThanMinProperties, LessThanMinimum, MissingReferenceError, MoreThanExclusiveMaximum, MoreThanMaxItems, MoreThanMaxLength, MoreThanMaxProperties, MoreThanMaximum, NotAnyOf, NotEnumInclude, NotExistContentTypeDefinition, NotExistDiscriminatorMappedSchema, NotExistDiscriminatorPropertyName, NotExistPropertyDefinition, NotExistRequiredKey, NotExistStatusCodeDefinition, NotNullError, NotOneOf, NotUniqueItems, OpenAPIError, ParameterValidator, PathItemFinder, ReferenceExpander, RequestOperation, SchemaLoader, SchemaValidator, ValidateError

Constant Summary collapse

VERSION =
'2.2.2'.freeze

Class Method Summary collapse

Class Method Details

.load(filepath, config = {}) ⇒ OpenAPIParser::Schemas::OpenAPI

Load schema in specified filepath. If file path is relative, it is resolved using working directory.



37
38
39
# 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:) ⇒ OpenAPIParser::Schemas::OpenAPI

Load schema located by the passed uri. Uri must be absolute.



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# 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.
  uri_path = uri.path
  raise "file not found" if uri_path.nil?

  content = if uri.scheme == 'file'
    open(uri_path)&.read
  elsif uri.is_a?(OpenURI::OpenRead)
    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 = {}) ⇒ OpenAPIParser::Schemas::OpenAPI

Load schema hash object. Uri is not set for returned schema.



23
24
25
# 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 = {}) ⇒ OpenAPIParser::Schemas::OpenAPI

Parameters:

  • 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.

Returns:



31
32
33
# 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