Class: OpenapiFirst::Operation

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

Overview

rubocop:disable Metrics/ClassLength

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path, request_method, path_item_object, openapi_version:) ⇒ Operation

Returns a new instance of Operation.



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

def initialize(path, request_method, path_item_object, openapi_version:)
  @path = path
  @method = request_method
  @path_item_object = path_item_object
  @openapi_version = openapi_version
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

#openapi_versionObject (readonly)

Returns the value of attribute openapi_version.



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

def openapi_version
  @openapi_version
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



108
109
110
111
112
113
114
115
# File 'lib/openapi_first/operation.rb', line 108

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


104
105
106
# File 'lib/openapi_first/operation.rb', line 104

def cookie_parameters
  @cookie_parameters ||= all_parameters.filter { |p| p['in'] == 'cookie' }
end

#header_parametersObject



100
101
102
# File 'lib/openapi_first/operation.rb', line 100

def header_parameters
  @header_parameters ||= all_parameters.filter { |p| p['in'] == 'header' && !IGNORED_HEADERS.include?(p['name']) }
end

#nameObject



78
79
80
# File 'lib/openapi_first/operation.rb', line 78

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

#operation_idObject



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

def operation_id
  operation_object['operationId']
end

#path_parametersObject



93
94
95
# File 'lib/openapi_first/operation.rb', line 93

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

#query_parametersObject



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

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

#read?Boolean

Returns:

  • (Boolean)


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

def read?
  !write?
end

#request_bodyObject



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

def request_body
  operation_object['requestBody']
end

#request_body_schema(request_content_type) ⇒ Object



61
62
63
64
65
66
67
68
# File 'lib/openapi_first/operation.rb', line 61

def request_body_schema(request_content_type)
  (@request_body_schema ||= {})[request_content_type] ||= begin
    content = operation_object.dig('requestBody', 'content')
    media_type = find_content_for_content_type(content, request_content_type)
    schema = media_type&.fetch('schema', nil)
    SchemaValidation.new(schema, write: write?, openapi_version:) if schema
  end
end

#response_body_schema(status, content_type) ⇒ Object

Raises:



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

def response_body_schema(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']
  return unless schema

  SchemaValidation.new(schema, write: false, openapi_version:)
end

#response_for(status) ⇒ Object



70
71
72
73
74
75
76
# File 'lib/openapi_first/operation.rb', line 70

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

#schemasObject

visibility: private



118
119
120
# File 'lib/openapi_first/operation.rb', line 118

def schemas
  @schemas ||= OperationSchemas.new(self)
end

#valid_request_content_type?(request_content_type) ⇒ Boolean

Returns:

  • (Boolean)


82
83
84
85
86
87
# File 'lib/openapi_first/operation.rb', line 82

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

  !!find_content_for_content_type(content, request_content_type)
end

#write?Boolean

Returns:

  • (Boolean)


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

def write?
  WRITE_METHODS.include?(method)
end