Class: OpenapiFirst::Operation

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/openapi_first/operation.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path, request_method, path_item_object) ⇒ Operation

Returns a new instance of Operation.



20
21
22
23
24
# File 'lib/openapi_first/operation.rb', line 20

def initialize(path, request_method, path_item_object)
  @path = path
  @method = request_method
  @path_item_object = path_item_object
end

Instance Attribute Details

#methodObject (readonly)

Returns the value of attribute method.



18
19
20
# File 'lib/openapi_first/operation.rb', line 18

def method
  @method
end

#pathObject (readonly)

Returns the value of attribute path.



18
19
20
# File 'lib/openapi_first/operation.rb', line 18

def path
  @path
end

Instance Method Details

#all_parametersObject



94
95
96
97
98
99
100
101
# File 'lib/openapi_first/operation.rb', line 94

def all_parameters
  @all_parameters ||= begin
    parameters = @path_item_object['parameters']&.dup || []
    parameters_on_operation = operation_object['parameters']
    parameters.concat(parameters_on_operation) if parameters_on_operation
    parameters
  end
end

#nameObject



75
76
77
# File 'lib/openapi_first/operation.rb', line 75

def name
  "#{method.upcase} #{path} (#{operation_id})"
end

#operation_idObject



26
27
28
# File 'lib/openapi_first/operation.rb', line 26

def operation_id
  operation_object['operationId']
end

#path_parametersObject



90
91
92
# File 'lib/openapi_first/operation.rb', line 90

def path_parameters
  @path_parameters ||= all_parameters.filter { |p| p['in'] == 'path' }
end

#query_parametersObject



86
87
88
# File 'lib/openapi_first/operation.rb', line 86

def query_parameters
  @query_parameters ||= all_parameters.filter { |p| p['in'] == 'query' }
end

#read?Boolean

Returns:

  • (Boolean)


30
31
32
# File 'lib/openapi_first/operation.rb', line 30

def read?
  !write?
end

#request_bodyObject



38
39
40
# File 'lib/openapi_first/operation.rb', line 38

def request_body
  operation_object['requestBody']
end

#request_body_schema(request_content_type) ⇒ Object



58
59
60
61
62
63
64
65
# File 'lib/openapi_first/operation.rb', line 58

def request_body_schema(request_content_type)
  content = operation_object.dig('requestBody', 'content')
  media_type = find_content_for_content_type(content, request_content_type)
  schema = media_type&.fetch('schema', nil)
  return unless schema

  SchemaValidation.new(schema, write: write?)
end

#response_for(status) ⇒ Object



67
68
69
70
71
72
73
# File 'lib/openapi_first/operation.rb', line 67

def response_for(status)
  response_content = response_by_code(status)
  return response_content if response_content

  message = "Response status code or default not found: #{status} for '#{name}'"
  raise OpenapiFirst::ResponseCodeNotFoundError, message
end

#response_schema_for(status, content_type) ⇒ Object

Raises:



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/openapi_first/operation.rb', line 42

def response_schema_for(status, content_type)
  content = response_for(status)['content']
  return if content.nil? || content.empty?

  raise ResponseInvalid, "Response has no content-type for '#{name}'" unless content_type

  media_type = find_content_for_content_type(content, content_type)

  unless media_type
    message = "Response content type not found '#{content_type}' for '#{name}'"
    raise ResponseContentTypeNotFoundError, message
  end
  schema = media_type['schema']
  SchemaValidation.new(schema, write: false) if schema
end

#valid_request_content_type?(request_content_type) ⇒ Boolean

Returns:

  • (Boolean)


79
80
81
82
83
84
# File 'lib/openapi_first/operation.rb', line 79

def valid_request_content_type?(request_content_type)
  content = operation_object.dig('requestBody', 'content')
  return unless content

  !!find_content_for_content_type(content, request_content_type)
end

#write?Boolean

Returns:

  • (Boolean)


34
35
36
# File 'lib/openapi_first/operation.rb', line 34

def write?
  WRITE_METHODS.include?(method)
end