Class: OpenapiFirst::RuntimeRequest

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

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

#errorFailure?

Returns the error object if validation failed.

Returns:



36
37
38
# File 'lib/openapi_first/runtime_request.rb', line 36

def error
  @error
end

#operationOperation? (readonly)

Returns the operation object.

Returns:

  • (Operation, nil)

    The operation object or nil if this request method is not known.



32
33
34
# File 'lib/openapi_first/runtime_request.rb', line 32

def operation
  @operation
end

#path_itemPathItem? (readonly)

Returns the path_item object.

Returns:

  • (PathItem, nil)

    The path_item object or nil if this request path is not known.



28
29
30
# File 'lib/openapi_first/runtime_request.rb', line 28

def path_item
  @path_item
end

Instance Method Details

#bodyHash, ... 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.

Returns:

  • (Hash, Array, String, nil)

    The parsed body of the request.



114
115
116
# File 'lib/openapi_first/runtime_request.rb', line 114

def body
  @body ||= BodyParser.new.parse(request, request.media_type)
end

#cookiesHash

Returns the parsed cookie parameters. This only includes parameters that are defined in the API description.

Returns:

  • (Hash)


103
104
105
106
107
108
# File 'lib/openapi_first/runtime_request.rb', line 103

def cookies
  return {} unless operation.cookie_parameters

  @cookies ||=
    OpenapiParameters::Cookie.new(operation.cookie_parameters).unpack(request.env[Rack::HTTP_COOKIE]) || {}
end

#headersHash

Returns the parsed header parameters. This only includes parameters that are defined in the API description.

Returns:

  • (Hash)


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.

Returns:

  • (Boolean)

    true if the path and request method are known, false otherwise.



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.

Returns:

  • (Boolean)

    true if the path is known, false otherwise.



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.

Returns:

  • (Boolean)

    true if the request method is known, false otherwise.



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

def known_request_method?
  !!operation
end

#paramsHash

Returns the merged path and query parameters.

Returns:

  • (Hash)

    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_parametersHash

Returns the parsed path parameters of the request.

Returns:

  • (Hash)


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

#queryHash Also known as: query_parameters

Note:

This method is aliased as query_parameters.

Returns the parsed query parameters. This only includes parameters that are defined in the API description.

Returns:

  • (Hash)


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.

Parameters:

  • rack_response (Rack::Response)

    The rack response object.

Returns:



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.

Returns:

  • (Boolean)

    true if the request is valid, false otherwise.



40
41
42
43
# File 'lib/openapi_first/runtime_request.rb', line 40

def valid?
  validate unless validated?
  error.nil?
end

#validateFailure?

Deprecated.

Please use Definition#validate_request instead

Validates the request.

Returns:

  • (Failure, nil)

    The Failure object if validation failed.



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.

Parameters:

  • rack_response (Rack::Response)

    The rack response object.

  • raise_error (Boolean) (defaults to: false)

    Whether to raise an error if validation fails.

Returns:



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