Class: OpenapiFirst::Operation
- Inherits:
-
Object
- Object
- OpenapiFirst::Operation
- Extended by:
- Forwardable
- Defined in:
- lib/openapi_first/operation.rb
Overview
rubocop:disable Metrics/ClassLength
Instance Attribute Summary collapse
-
#method ⇒ Object
readonly
Returns the value of attribute method.
-
#openapi_version ⇒ Object
readonly
Returns the value of attribute openapi_version.
-
#path ⇒ Object
readonly
Returns the value of attribute path.
Instance Method Summary collapse
- #all_parameters ⇒ Object
- #cookie_parameters ⇒ Object
- #cookie_parameters_schema ⇒ Object
- #header_parameters ⇒ Object
- #header_parameters_schema ⇒ Object
-
#initialize(path, request_method, path_item_object, openapi_version:) ⇒ Operation
constructor
A new instance of Operation.
- #name ⇒ Object
- #operation_id ⇒ Object
- #path_parameters ⇒ Object
-
#path_parameters_schema ⇒ Object
Return JSON Schema of for all path parameters.
- #query_parameters ⇒ Object
-
#query_parameters_schema ⇒ Object
Return JSON Schema of for all query parameters.
- #read? ⇒ Boolean
- #request_body ⇒ Object
- #request_body_schema(request_content_type) ⇒ Object
- #response_body_schema(status, content_type) ⇒ Object
- #response_for(status) ⇒ Object
- #valid_request_content_type?(request_content_type) ⇒ Boolean
- #write? ⇒ Boolean
Constructor Details
#initialize(path, request_method, path_item_object, openapi_version:) ⇒ Operation
Returns a new instance of Operation.
19 20 21 22 23 24 |
# File 'lib/openapi_first/operation.rb', line 19 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
#method ⇒ Object (readonly)
Returns the value of attribute method.
17 18 19 |
# File 'lib/openapi_first/operation.rb', line 17 def method @method end |
#openapi_version ⇒ Object (readonly)
Returns the value of attribute openapi_version.
17 18 19 |
# File 'lib/openapi_first/operation.rb', line 17 def openapi_version @openapi_version end |
#path ⇒ Object (readonly)
Returns the value of attribute path.
17 18 19 |
# File 'lib/openapi_first/operation.rb', line 17 def path @path end |
Instance Method Details
#all_parameters ⇒ Object
107 108 109 110 111 112 113 114 |
# File 'lib/openapi_first/operation.rb', line 107 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 |
#cookie_parameters ⇒ Object
103 104 105 |
# File 'lib/openapi_first/operation.rb', line 103 def @cookie_parameters ||= all_parameters.filter { |p| p['in'] == 'cookie' } end |
#cookie_parameters_schema ⇒ Object
130 131 132 |
# File 'lib/openapi_first/operation.rb', line 130 def @cookie_parameters_schema ||= build_json_schema() end |
#header_parameters ⇒ Object
99 100 101 |
# File 'lib/openapi_first/operation.rb', line 99 def header_parameters @header_parameters ||= all_parameters.filter { |p| p['in'] == 'header' && !IGNORED_HEADERS.include?(p['name']) } end |
#header_parameters_schema ⇒ Object
126 127 128 |
# File 'lib/openapi_first/operation.rb', line 126 def header_parameters_schema @header_parameters_schema ||= build_json_schema(header_parameters) end |
#name ⇒ Object
77 78 79 |
# File 'lib/openapi_first/operation.rb', line 77 def name @name ||= "#{method.upcase} #{path} (#{operation_id})" end |
#operation_id ⇒ Object
26 27 28 |
# File 'lib/openapi_first/operation.rb', line 26 def operation_id operation_object['operationId'] end |
#path_parameters ⇒ Object
92 93 94 |
# File 'lib/openapi_first/operation.rb', line 92 def path_parameters @path_parameters ||= all_parameters.filter { |p| p['in'] == 'path' } end |
#path_parameters_schema ⇒ Object
Return JSON Schema of for all path parameters
122 123 124 |
# File 'lib/openapi_first/operation.rb', line 122 def path_parameters_schema @path_parameters_schema ||= build_json_schema(path_parameters) end |
#query_parameters ⇒ Object
88 89 90 |
# File 'lib/openapi_first/operation.rb', line 88 def query_parameters @query_parameters ||= all_parameters.filter { |p| p['in'] == 'query' } end |
#query_parameters_schema ⇒ Object
Return JSON Schema of for all query parameters
117 118 119 |
# File 'lib/openapi_first/operation.rb', line 117 def query_parameters_schema @query_parameters_schema ||= build_json_schema(query_parameters) end |
#read? ⇒ Boolean
30 31 32 |
# File 'lib/openapi_first/operation.rb', line 30 def read? !write? end |
#request_body ⇒ Object
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
60 61 62 63 64 65 66 67 |
# File 'lib/openapi_first/operation.rb', line 60 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) JsonSchema.new(schema, write: write?, openapi_version:) if schema end end |
#response_body_schema(status, content_type) ⇒ Object
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/openapi_first/operation.rb', line 42 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 = "Response content type not found '#{content_type}' for '#{name}'" raise ResponseContentTypeNotFoundError, end schema = media_type['schema'] return unless schema JsonSchema.new(schema, write: false, openapi_version:) end |
#response_for(status) ⇒ Object
69 70 71 72 73 74 75 |
# File 'lib/openapi_first/operation.rb', line 69 def response_for(status) response_content = response_by_code(status) return response_content if response_content = "Response status code or default not found: #{status} for '#{name}'" raise OpenapiFirst::ResponseCodeNotFoundError, end |
#valid_request_content_type?(request_content_type) ⇒ Boolean
81 82 83 84 85 86 |
# File 'lib/openapi_first/operation.rb', line 81 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
34 35 36 |
# File 'lib/openapi_first/operation.rb', line 34 def write? WRITE_METHODS.include?(method) end |