Class: OpenapiFirst::RuntimeRequest
- Inherits:
-
Object
- Object
- OpenapiFirst::RuntimeRequest
- Extended by:
- Forwardable
- Defined in:
- lib/openapi_first/runtime_request.rb
Overview
RuntimeRequest represents how an incoming request (Rack::Request) matches a request definition.
Instance Attribute Summary collapse
-
#error ⇒ Failure?
Returns the error object if validation failed.
-
#operation ⇒ Operation?
readonly
Returns the operation object.
-
#path_item ⇒ PathItem?
readonly
Returns the path_item object.
Instance Method Summary collapse
-
#body ⇒ Hash, ...
(also: #parsed_body)
Returns the parsed request body.
-
#cookies ⇒ Hash
Returns the parsed cookie parameters.
-
#headers ⇒ Hash
Returns the parsed header parameters.
-
#initialize(request:, path_item:, operation:, path_params:) ⇒ RuntimeRequest
constructor
A new instance of RuntimeRequest.
-
#known? ⇒ Boolean
Checks if the path and request method are known.
-
#known_path? ⇒ Boolean
Checks if the path is known.
-
#known_request_method? ⇒ Boolean
Checks if the request method is known.
-
#params ⇒ Hash
Returns the merged path and query parameters.
-
#path_parameters ⇒ Hash
Returns the parsed path parameters of the request.
-
#query ⇒ Hash
(also: #query_parameters)
Returns the parsed query parameters.
-
#response(rack_response) ⇒ RuntimeResponse
Creates a new RuntimeResponse object.
-
#valid? ⇒ Boolean
Checks if the request is valid.
-
#validate ⇒ Failure?
deprecated
Deprecated.
Please use Definition#validate_request instead
-
#validate! ⇒ Object
Validates the request and raises an error if validation fails.
-
#validate_response(rack_response, raise_error: false) ⇒ RuntimeResponse
Validates the response.
Constructor Details
#initialize(request:, path_item:, operation:, path_params:) ⇒ RuntimeRequest
Returns a new instance of RuntimeRequest.
14 15 16 17 18 19 20 |
# File 'lib/openapi_first/runtime_request.rb', line 14 def initialize(request:, path_item:, operation:, path_params:) @request = request @path_item = path_item @operation = operation @original_path_params = path_params @validated = false end |
Instance Attribute Details
#error ⇒ Failure?
Returns the error object if validation failed.
36 37 38 |
# File 'lib/openapi_first/runtime_request.rb', line 36 def error @error end |
#operation ⇒ Operation? (readonly)
Returns the operation object.
32 33 34 |
# File 'lib/openapi_first/runtime_request.rb', line 32 def operation @operation end |
#path_item ⇒ PathItem? (readonly)
Returns the path_item object.
28 29 30 |
# File 'lib/openapi_first/runtime_request.rb', line 28 def path_item @path_item end |
Instance Method Details
#body ⇒ Hash, ... Also known as: parsed_body
Returns the parsed request body. This returns the whole request body with default values applied as defined in the API description. This does not remove any fields that are not defined in the API description.
114 115 116 |
# File 'lib/openapi_first/runtime_request.rb', line 114 def body @body ||= BodyParser.new.parse(request, request.media_type) end |
#cookies ⇒ Hash
Returns the parsed cookie parameters. This only includes parameters that are defined in the API description.
103 104 105 106 107 108 |
# File 'lib/openapi_first/runtime_request.rb', line 103 def return {} unless operation. @cookies ||= OpenapiParameters::Cookie.new(operation.).unpack(request.env[Rack::HTTP_COOKIE]) || {} end |
#headers ⇒ Hash
Returns the parsed header parameters. This only includes parameters that are defined in the API description.
94 95 96 97 98 |
# File 'lib/openapi_first/runtime_request.rb', line 94 def headers return {} unless operation.header_parameters @headers ||= OpenapiParameters::Header.new(operation.header_parameters).unpack_env(request.env) || {} end |
#known? ⇒ Boolean
Checks if the path and request method are known.
47 48 49 |
# File 'lib/openapi_first/runtime_request.rb', line 47 def known? known_path? && known_request_method? end |
#known_path? ⇒ Boolean
Checks if the path is known.
53 54 55 |
# File 'lib/openapi_first/runtime_request.rb', line 53 def known_path? !!path_item end |
#known_request_method? ⇒ Boolean
Checks if the request method is known.
59 60 61 |
# File 'lib/openapi_first/runtime_request.rb', line 59 def known_request_method? !!operation end |
#params ⇒ Hash
Returns the merged path and query parameters.
65 66 67 |
# File 'lib/openapi_first/runtime_request.rb', line 65 def params @params ||= query.merge(path_parameters) end |
#path_parameters ⇒ Hash
Returns the parsed path parameters of the request.
71 72 73 74 75 76 |
# File 'lib/openapi_first/runtime_request.rb', line 71 def path_parameters return {} unless operation.path_parameters @path_parameters ||= OpenapiParameters::Path.new(operation.path_parameters).unpack(@original_path_params) || {} end |
#query ⇒ Hash Also known as: query_parameters
This method is aliased as query_parameters.
Returns the parsed query parameters. This only includes parameters that are defined in the API description.
82 83 84 85 86 87 |
# File 'lib/openapi_first/runtime_request.rb', line 82 def query return {} unless operation.query_parameters @query ||= OpenapiParameters::Query.new(operation.query_parameters).unpack(request.env[Rack::QUERY_STRING]) || {} end |
#response(rack_response) ⇒ RuntimeResponse
Creates a new RuntimeResponse object.
152 153 154 155 156 |
# File 'lib/openapi_first/runtime_request.rb', line 152 def response(rack_response) warn '[DEPRECATION] `response` is deprecated. Please use ' \ "`OpenapiFirst.load('openapi.yaml').validate_response(request, response, raise_error: false)` instead." RuntimeResponse.new(operation, rack_response) end |
#valid? ⇒ Boolean
Checks if the request is valid.
40 41 42 43 |
# File 'lib/openapi_first/runtime_request.rb', line 40 def valid? validate unless validated? error.nil? end |
#validate ⇒ Failure?
Please use Definition#validate_request instead
Validates the request.
123 124 125 126 127 |
# File 'lib/openapi_first/runtime_request.rb', line 123 def validate warn '[DEPRECATION] `validate` is deprecated. Please use ' \ "`OpenapiFirst.load('openapi.yaml').validate_request(rack_request)` instead." @error = RequestValidation::Validator.new(operation).validate(self) end |
#validate! ⇒ Object
Validates the request and raises an error if validation fails.
130 131 132 133 134 135 |
# File 'lib/openapi_first/runtime_request.rb', line 130 def validate! warn '[DEPRECATION] `validate!` is deprecated. Please use ' \ "`OpenapiFirst.load('openapi.yaml').validate_request(rack_request, raise_error: true)` instead." error = validate error&.raise! end |
#validate_response(rack_response, raise_error: false) ⇒ RuntimeResponse
Validates the response.
141 142 143 144 145 146 147 |
# File 'lib/openapi_first/runtime_request.rb', line 141 def validate_response(rack_response, raise_error: false) warn '[DEPRECATION] `validate_response!` is deprecated. Please use ' \ "`OpenapiFirst.load('openapi.yaml').validate_response(request, response, raise_error: false)` instead." validated = response(rack_response).tap(&:validate) validated.error&.raise! if raise_error validated end |