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

#errorFailure? (readonly)

Returns the error object if validation failed.

Returns:



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

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.



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

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.



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

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.



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

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)


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

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)


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.

Returns:

  • (Boolean)

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



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.

Returns:

  • (Boolean)

    true if the path is known, false otherwise.



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.

Returns:

  • (Boolean)

    true if the request method is known, false otherwise.



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

def known_request_method?
  !!operation
end

#paramsHash

Returns the merged path and query parameters.

Returns:

  • (Hash)

    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_parametersHash

Returns the parsed path parameters of the request.

Returns:

  • (Hash)


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

#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)


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.

Parameters:

  • rack_response (Rack::Response)

    The rack response object.

Returns:



147
148
149
# File 'lib/openapi_first/runtime_request.rb', line 147

def response(rack_response)
  RuntimeResponse.new(operation, rack_response)
end

#valid?Boolean

Checks if the request is valid.

Returns:

  • (Boolean)

    true if the request is valid, false otherwise.



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

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

#validateFailure?

Validates the request.

Returns:

  • (Failure, nil)

    The Failure object if validation failed.



123
124
125
126
# File 'lib/openapi_first/runtime_request.rb', line 123

def validate
  @validated = true
  @error = RequestValidation::Validator.new(operation).validate(self)
end

#validate!Object

Validates the request and raises an error if validation fails.



129
130
131
132
# File 'lib/openapi_first/runtime_request.rb', line 129

def validate!
  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:



138
139
140
141
142
# File 'lib/openapi_first/runtime_request.rb', line 138

def validate_response(rack_response, raise_error: false)
  validated = response(rack_response).tap(&:validate)
  validated.error&.raise! if raise_error
  validated
end