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.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.



23
24
25
26
27
28
29
# File 'lib/openapi_first/definition/operation.rb', line 23

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.



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

def method
  @method
end

#openapi_versionObject (readonly)

:nodoc:



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

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.



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

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.



126
127
128
# File 'lib/openapi_first/definition/operation.rb', line 126

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.



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

def header_parameters
  all_parameters['header']
end

#operation_idString?

Returns the operation ID as defined in the API description.

Returns:

  • (String, nil)


45
46
47
# File 'lib/openapi_first/definition/operation.rb', line 45

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.



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

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.



112
113
114
# File 'lib/openapi_first/definition/operation.rb', line 112

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.



52
53
54
# File 'lib/openapi_first/definition/operation.rb', line 52

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.



65
66
67
# File 'lib/openapi_first/definition/operation.rb', line 65

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.



80
81
82
# File 'lib/openapi_first/definition/operation.rb', line 80

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.



72
73
74
# File 'lib/openapi_first/definition/operation.rb', line 72

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.



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

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

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.



59
60
61
# File 'lib/openapi_first/definition/operation.rb', line 59

def write?
  WRITE_METHODS.include?(method)
end