Class: OpenapiRuby::Middleware::SchemaResolver

Inherits:
Object
  • Object
show all
Defined in:
lib/openapi_ruby/middleware/schema_resolver.rb

Instance Method Summary collapse

Constructor Details

#initialize(spec_path: nil, document: nil, strict_reference_validation: true) ⇒ SchemaResolver

Returns a new instance of SchemaResolver.



6
7
8
9
10
11
12
# File 'lib/openapi_ruby/middleware/schema_resolver.rb', line 6

def initialize(spec_path: nil, document: nil, strict_reference_validation: true)
  @spec_path = spec_path
  @document = document
  @strict_reference_validation = strict_reference_validation
  @path_matcher = nil
  @schemer = nil
end

Instance Method Details

#documentObject



14
15
16
# File 'lib/openapi_ruby/middleware/schema_resolver.rb', line 14

def document
  @document ||= load_document.tap { |doc| validate_document!(doc) }
end

#find_operation(method, request_path) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/openapi_ruby/middleware/schema_resolver.rb', line 26

def find_operation(method, request_path)
  result = path_matcher.match(request_path)
  return nil unless result

  template, path_params = result
  operation = document.dig("paths", template, method.downcase)
  return nil unless operation

  {
    operation: operation,
    template: template,
    path_params: path_params
  }
end

#path_matcherObject



22
23
24
# File 'lib/openapi_ruby/middleware/schema_resolver.rb', line 22

def path_matcher
  @path_matcher ||= PathMatcher.new(document.fetch("paths", {}).keys)
end

#schemerObject



18
19
20
# File 'lib/openapi_ruby/middleware/schema_resolver.rb', line 18

def schemer
  @schemer ||= JSONSchemer.openapi(document)
end