Class: OpenapiFirst::Definition::Operation

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

Overview

Represents an operation object in the OpenAPI 3.X specification. Use this class to access information about the operation. Use #[key] to read the raw data. When using the middleware you can access the operation object via env[OpenapiFirst::REQUEST].operation.

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
26
# File 'lib/openapi_first/definition/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
  @operation_object = @path_item_object[request_method]
end

Instance Attribute Details

#methodString (readonly) Also known as: request_method

Returns the (downcased) request method of the operation. Example: “get”

Returns:

  • (String)

    The request method of the operation.



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

def method
  @method
end

#openapi_versionObject (readonly)

:nodoc:



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

def openapi_version
  @openapi_version
end

#pathString (readonly)

Returns the path of the operation as in the API description.

Returns:

  • (String)

    The path of the operation.



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

def path
  @path
end

Instance Method Details

Returns the cookie parameters of the operation. Returns parameters defined on the path and in the operation.

Returns:

  • (Array<Hash>)

    The cookie parameters of the operation.



124
125
126
# File 'lib/openapi_first/definition/operation.rb', line 124

def cookie_parameters
  all_parameters['cookie']
end

#header_parametersArray<Hash>

Returns the header parameters of the operation. Returns parameters defined on the path and in the operation.

Returns:

  • (Array<Hash>)

    The header parameters of the operation.



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

def header_parameters
  all_parameters['header']
end

#operation_idString?

Returns the operation ID as defined in the API description.

Returns:

  • (String, nil)


42
43
44
# File 'lib/openapi_first/definition/operation.rb', line 42

def operation_id
  operation_object['operationId']
end

#path_parametersArray<Hash>

Returns the path parameters of the operation.

Returns:

  • (Array<Hash>)

    The path parameters of the operation.



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

def path_parameters
  all_parameters['path']
end

#query_parametersArray<Hash>

Returns the query parameters of the operation. Returns parameters defined on the path and in the operation.

Returns:

  • (Array<Hash>)

    The query parameters of the operation.



110
111
112
# File 'lib/openapi_first/definition/operation.rb', line 110

def query_parameters
  all_parameters['query']
end

#read?Boolean

Checks if the operation is a read operation. This is the case for all request methods except POST, PUT, PATCH and DELETE.

Returns:

  • (Boolean)

    true if the operation is a read operation, false otherwise.



49
50
51
# File 'lib/openapi_first/definition/operation.rb', line 49

def read?
  !write?
end

#request_bodyRequestBody?

Returns the request body definition if defined in the API description.

Returns:

  • (RequestBody, nil)

    The request body of the operation, or nil if not present.



63
64
65
# File 'lib/openapi_first/definition/operation.rb', line 63

def request_body
  @request_body ||= RequestBody.new(operation_object['requestBody'], self) if operation_object['requestBody']
end

#response_for(status, content_type) ⇒ Response?

Returns the response object for a given status.

Parameters:

  • status (Integer, String)

    The response status.

  • content_type (String)

    Content-Type of the current response.

Returns:

  • (Response, nil)

    The response object for the given status, or nil if not found.



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

def response_for(status, content_type)
  responses.response_for(status, content_type)
end

#response_status_defined?(status) ⇒ Boolean

Checks if a response status is defined for this operation.

Parameters:

  • status (Integer, String)

    The response status to check.

Returns:

  • (Boolean)

    true if the response status is defined, false otherwise.



70
71
72
# File 'lib/openapi_first/definition/operation.rb', line 70

def response_status_defined?(status)
  responses.status_defined?(status)
end

#schema_for(content_type) ⇒ Schema?

Returns the schema for a given content type.

Parameters:

  • content_type (String)

    The content type.

Returns:

  • (Schema, nil)

    The schema for the given content type, or nil if not found.



85
86
87
88
89
90
91
92
93
# File 'lib/openapi_first/definition/operation.rb', line 85

def schema_for(content_type)
  content = @request_body_object['content']
  return unless content&.any?

  content_schemas&.fetch(content_type) do
    type = content_type.split(';')[0]
    content_schemas[type] || content_schemas["#{type.split('/')[0]}/*"] || content_schemas['*/*']
  end
end

#write?Boolean

Deprecated.

Use #write? instead.

Checks if the operation is a write operation. This is the case for POST, PUT, PATCH and DELETE request methods.

Returns:

  • (Boolean)

    true if the operation is a write operation, false otherwise.



57
58
59
# File 'lib/openapi_first/definition/operation.rb', line 57

def write?
  WRITE_METHODS.include?(method)
end