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?
readonly
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 21 |
# 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 @error = nil @validated = false end |
Instance Attribute Details
#error ⇒ Failure? (readonly)
Returns the error object if validation failed.
37 38 39 |
# File 'lib/openapi_first/runtime_request.rb', line 37 def error @error end |
#operation ⇒ Operation? (readonly)
Returns the operation object.
33 34 35 |
# File 'lib/openapi_first/runtime_request.rb', line 33 def operation @operation end |
#path_item ⇒ PathItem? (readonly)
Returns the path_item object.
29 30 31 |
# File 'lib/openapi_first/runtime_request.rb', line 29 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.
115 116 117 |
# File 'lib/openapi_first/runtime_request.rb', line 115 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.
104 105 106 107 108 109 |
# File 'lib/openapi_first/runtime_request.rb', line 104 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.
95 96 97 98 99 |
# File 'lib/openapi_first/runtime_request.rb', line 95 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.
48 49 50 |
# File 'lib/openapi_first/runtime_request.rb', line 48 def known? known_path? && known_request_method? end |
#known_path? ⇒ Boolean
Checks if the path is known.
54 55 56 |
# File 'lib/openapi_first/runtime_request.rb', line 54 def known_path? !!path_item end |
#known_request_method? ⇒ Boolean
Checks if the request method is known.
60 61 62 |
# File 'lib/openapi_first/runtime_request.rb', line 60 def known_request_method? !!operation end |
#params ⇒ Hash
Returns the merged path and query parameters.
66 67 68 |
# File 'lib/openapi_first/runtime_request.rb', line 66 def params @params ||= query.merge(path_parameters) end |
#path_parameters ⇒ Hash
Returns the parsed path parameters of the request.
72 73 74 75 76 77 |
# File 'lib/openapi_first/runtime_request.rb', line 72 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.
83 84 85 86 87 88 |
# File 'lib/openapi_first/runtime_request.rb', line 83 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.
154 155 156 157 158 |
# File 'lib/openapi_first/runtime_request.rb', line 154 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.
41 42 43 44 |
# File 'lib/openapi_first/runtime_request.rb', line 41 def valid? validate unless @validated error.nil? end |
#validate ⇒ Failure?
Please use Definition#validate_request instead
Validates the request.
124 125 126 127 128 129 |
# File 'lib/openapi_first/runtime_request.rb', line 124 def validate warn '[DEPRECATION] `validate` is deprecated. Please use ' \ "`OpenapiFirst.load('openapi.yaml').validate_request(rack_request)` instead." @validated = true @error = RequestValidation::Validator.new(operation).validate(self) end |
#validate! ⇒ Object
Validates the request and raises an error if validation fails.
132 133 134 135 136 137 |
# File 'lib/openapi_first/runtime_request.rb', line 132 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.
143 144 145 146 147 148 149 |
# File 'lib/openapi_first/runtime_request.rb', line 143 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 |