Class: ManageIQ::API::Common::OpenApi::Docs::DocV3

Inherits:
Object
  • Object
show all
Defined in:
lib/manageiq/api/common/open_api/docs/doc_v3.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(content) ⇒ DocV3

Returns a new instance of DocV3.



13
14
15
16
17
18
# File 'lib/manageiq/api/common/open_api/docs/doc_v3.rb', line 13

def initialize(content)
  spec_version = content["openapi"]
  raise "Unsupported OpenAPI Specification version #{spec_version}" unless spec_version =~ /\A3\..*\z/

  @content = content
end

Instance Attribute Details

#contentObject (readonly)

Returns the value of attribute content.



11
12
13
# File 'lib/manageiq/api/common/open_api/docs/doc_v3.rb', line 11

def content
  @content
end

Instance Method Details

#base_pathObject



59
60
61
# File 'lib/manageiq/api/common/open_api/docs/doc_v3.rb', line 59

def base_path
  @base_path ||= @content.fetch_path("servers", 0, "variables", "basePath", "default")
end

#definitionsObject



49
50
51
# File 'lib/manageiq/api/common/open_api/docs/doc_v3.rb', line 49

def definitions
  schemas
end

#example_attributes(key) ⇒ Object



53
54
55
56
57
# File 'lib/manageiq/api/common/open_api/docs/doc_v3.rb', line 53

def example_attributes(key)
  schemas[key]["properties"].each_with_object({}) do |(col, stuff), hash|
    hash[col] = stuff["example"] if stuff.key?("example")
  end
end

#parametersObject



41
42
43
# File 'lib/manageiq/api/common/open_api/docs/doc_v3.rb', line 41

def parameters
  @parameters ||= ::ManageIQ::API::Common::OpenApi::Docs::ComponentCollection.new(self, "components/parameters")
end

#pathsObject



63
64
65
# File 'lib/manageiq/api/common/open_api/docs/doc_v3.rb', line 63

def paths
  @content["paths"]
end

#routesObject



71
72
73
74
75
76
77
78
79
80
# File 'lib/manageiq/api/common/open_api/docs/doc_v3.rb', line 71

def routes
  @routes ||= begin
    paths.flat_map do |path, hash|
      hash.collect do |verb, _details|
        p = File.join(base_path, path).gsub(/{\w*}/, ":id")
        {:path => p, :verb => verb.upcase}
      end
    end
  end
end

#schemasObject



45
46
47
# File 'lib/manageiq/api/common/open_api/docs/doc_v3.rb', line 45

def schemas
  @schemas ||= ::ManageIQ::API::Common::OpenApi::Docs::ComponentCollection.new(self, "components/schemas")
end

#to_json(options = nil) ⇒ Object



67
68
69
# File 'lib/manageiq/api/common/open_api/docs/doc_v3.rb', line 67

def to_json(options = nil)
  content.to_json(options)
end

#validate!(http_method, request_path, api_version, payload, payload_content_type = 'application/json') ⇒ Object

Validates data types against OpenAPI schema

Parameters:

  • http_method (String)

    POST/PATCH/…

  • request_path (String)

    i.e. /api/sources/v1.0/sources

  • api_version (String)

    i.e. “v1.0”, has to be part of request_path

  • payload (String)

    JSON if payload_content_type == ‘application/json’

  • payload_content_type (String) (defaults to: 'application/json')

Raises:

  • OpenAPIParser::OpenAPIError



29
30
31
32
33
34
35
# File 'lib/manageiq/api/common/open_api/docs/doc_v3.rb', line 29

def validate!(http_method, request_path, api_version, payload, payload_content_type = 'application/json')
  path = request_path.split(api_version)[1]
  raise "API version not found in request_path" if path.nil?

  request_operation = validator_doc.request_operation(http_method.to_s.downcase, path)
  request_operation.validate_request_body(payload_content_type, payload)
end

#versionObject



37
38
39
# File 'lib/manageiq/api/common/open_api/docs/doc_v3.rb', line 37

def version
  @version ||= Gem::Version.new(content.fetch_path("info", "version"))
end