Class: Committee::SchemaValidator::OpenAPI3::OperationWrapper

Inherits:
Object
  • Object
show all
Defined in:
lib/committee/schema_validator/open_api_3/operation_wrapper.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(request_operation) ⇒ OperationWrapper

# @param request_operation [OpenAPIParser::RequestOperation]



8
9
10
# File 'lib/committee/schema_validator/open_api_3/operation_wrapper.rb', line 8

def initialize(request_operation)
  @request_operation = request_operation
end

Instance Attribute Details

#request_operationOpenAPIParser::RequestOperation (readonly)

Expose request_operation for parameter deserialization

Returns:

  • (OpenAPIParser::RequestOperation)


76
77
78
# File 'lib/committee/schema_validator/open_api_3/operation_wrapper.rb', line 76

def request_operation
  @request_operation
end

Instance Method Details

#coerce_path_parameter(validator_option) ⇒ Object



24
25
26
27
28
29
30
31
32
# File 'lib/committee/schema_validator/open_api_3/operation_wrapper.rb', line 24

def coerce_path_parameter(validator_option)
  options = build_openapi_parser_path_option(validator_option)
  validated_path_params = request_operation.validate_path_params(options)
  return {} unless options.coerce_value

  validated_path_params
rescue OpenAPIParser::OpenAPIError => e
  raise Committee::InvalidRequest.new(e.message, original_error: e)
end

#http_methodObject



20
21
22
# File 'lib/committee/schema_validator/open_api_3/operation_wrapper.rb', line 20

def http_method
  request_operation.http_method
end

#optional_body?Boolean

Returns:

  • (Boolean)


56
57
58
# File 'lib/committee/schema_validator/open_api_3/operation_wrapper.rb', line 56

def optional_body?
  !request_operation.operation_object&.request_body&.required
end

#original_pathObject



16
17
18
# File 'lib/committee/schema_validator/open_api_3/operation_wrapper.rb', line 16

def original_path
  request_operation.original_path
end

#path_paramsObject



12
13
14
# File 'lib/committee/schema_validator/open_api_3/operation_wrapper.rb', line 12

def path_params
  request_operation.path_params
end

#query_parameter_namesArray<String>

Returns names of query parameters defined in the schema.

Returns:

  • (Array<String>)

    names of query parameters defined in the schema



79
80
81
82
83
# File 'lib/committee/schema_validator/open_api_3/operation_wrapper.rb', line 79

def query_parameter_names
  return [] unless request_operation.operation_object&.parameters

  request_operation.operation_object.parameters.select { |p| p.in == 'query' }.map(&:name)
end

#request_content_typesObject



70
71
72
# File 'lib/committee/schema_validator/open_api_3/operation_wrapper.rb', line 70

def request_content_types
  request_operation.operation_object&.request_body&.content&.keys || []
end

#valid_request_content_type?(content_type) ⇒ Boolean

Returns:

  • (Boolean)


60
61
62
63
64
65
66
67
68
# File 'lib/committee/schema_validator/open_api_3/operation_wrapper.rb', line 60

def valid_request_content_type?(content_type)
  if (request_body = request_operation.operation_object&.request_body)
    !request_body.select_media_type(content_type).nil?
  else
    # if not exist request body object, all content_type allow.
    # because request body object required content field so when it exists there're content type definition.
    true
  end
end

#validate_request_params(path_params, query_params, body_params, headers, validator_option) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/committee/schema_validator/open_api_3/operation_wrapper.rb', line 43

def validate_request_params(path_params, query_params, body_params, headers, validator_option)
  ret, err = case request_operation.http_method
        when 'get', 'delete', 'head'
          validate_get_request_params(path_params, query_params, headers, validator_option)
        when 'post', 'put', 'patch', 'options'
          validate_post_request_params(path_params, query_params, body_params, headers, validator_option)
        else
          raise "Committee OpenAPI3 not support #{request_operation.http_method} method"
        end
  raise err if err
  ret
end

#validate_response_params(status_code, headers, response_data, strict, check_header, validator_options: {}) ⇒ Object

Parameters:

  • strict (Boolean)

    when not content_type or status code definition, raise error



35
36
37
38
39
40
41
# File 'lib/committee/schema_validator/open_api_3/operation_wrapper.rb', line 35

def validate_response_params(status_code, headers, response_data, strict, check_header, validator_options: {})
  response_body = OpenAPIParser::RequestOperation::ValidatableResponseBody.new(status_code, response_data, headers)

  return request_operation.validate_response_body(response_body, response_validate_options(strict, check_header, validator_options: validator_options))
rescue OpenAPIParser::OpenAPIError => e
  raise Committee::InvalidResponse.new(e.message, original_error: e)
end