class OpenapiFirst::Operation

Constants

WRITE_METHODS

Attributes

method[R]
path[R]

Public Class Methods

new(path, request_method, path_item_object) click to toggle source
# File lib/openapi_first/operation.rb, line 21
def initialize(path, request_method, path_item_object)
  @path = path
  @method = request_method
  @path_item_object = path_item_object
end

Public Instance Methods

content_type_for(status) click to toggle source
# File lib/openapi_first/operation.rb, line 50
def content_type_for(status)
  content = response_for(status)['content']
  content.keys[0] if content
end
name() click to toggle source
# File lib/openapi_first/operation.rb, line 88
def name
  "#{method.upcase} #{path} (#{operation_id})"
end
operation_id() click to toggle source
# File lib/openapi_first/operation.rb, line 27
def operation_id
  operation_object['operationId']
end
parameters_schema() click to toggle source
# File lib/openapi_first/operation.rb, line 43
def parameters_schema
  @parameters_schema ||= begin
    parameters_json_schema = build_parameters_json_schema
    parameters_json_schema && SchemaValidation.new(parameters_json_schema)
  end
end
read?() click to toggle source
# File lib/openapi_first/operation.rb, line 31
def read?
  !write?
end
request_body() click to toggle source
# File lib/openapi_first/operation.rb, line 39
def request_body
  operation_object['requestBody']
end
request_body_schema(request_content_type) click to toggle source
# File lib/openapi_first/operation.rb, line 71
def request_body_schema(request_content_type)
  content = operation_object.dig('requestBody', 'content')
  media_type = find_content_for_content_type(content, request_content_type)
  schema = media_type&.fetch('schema', nil)
  return unless schema

  SchemaValidation.new(schema, write: write?)
end
response_for(status) click to toggle source
# File lib/openapi_first/operation.rb, line 80
def response_for(status)
  response_content = response_by_code(status)
  return response_content if response_content

  message = "Response status code or default not found: #{status} for '#{name}'"
  raise OpenapiFirst::ResponseCodeNotFoundError, message
end
response_schema_for(status, content_type) click to toggle source
# File lib/openapi_first/operation.rb, line 55
def response_schema_for(status, content_type)
  content = response_for(status)['content']
  return if content.nil? || content.empty?

  raise ResponseInvalid, "Response has no content-type for '#{name}'" unless content_type

  media_type = find_content_for_content_type(content, content_type)

  unless media_type
    message = "Response content type not found '#{content_type}' for '#{name}'"
    raise ResponseContentTypeNotFoundError, message
  end
  schema = media_type['schema']
  SchemaValidation.new(schema, write: false) if schema
end
write?() click to toggle source
# File lib/openapi_first/operation.rb, line 35
def write?
  WRITE_METHODS.include?(method)
end

Private Instance Methods

all_parameters() click to toggle source
# File lib/openapi_first/operation.rb, line 120
def all_parameters
  parameters = @path_item_object['parameters'] || []
  parameters_on_operation = operation_object['parameters']
  parameters.concat(parameters_on_operation) if parameters_on_operation
  parameters
end
build_parameters_json_schema() click to toggle source
# File lib/openapi_first/operation.rb, line 110
def build_parameters_json_schema
  parameters = all_parameters
  return unless parameters&.any?

  parameters.each_with_object(new_node) do |parameter, schema|
    params = Rack::Utils.parse_nested_query(parameter['name'])
    generate_schema(schema, params, parameter)
  end
end
find_content_for_content_type(content, request_content_type) click to toggle source
# File lib/openapi_first/operation.rb, line 103
def find_content_for_content_type(content, request_content_type)
  content.fetch(request_content_type) do |_|
    type = request_content_type.split(';')[0]
    content[type] || content["#{type.split('/')[0]}/*"] || content['*/*']
  end
end
generate_schema(schema, params, parameter) click to toggle source
# File lib/openapi_first/operation.rb, line 127
def generate_schema(schema, params, parameter)
  required = Set.new(schema['required'])
  params.each do |key, value|
    required << key if parameter['required']
    if value.is_a? Hash
      property_schema = new_node
      generate_schema(property_schema, value, parameter)
      Utils.deep_merge!(schema['properties'], { key => property_schema })
    else
      schema['properties'][key] = parameter['schema']
    end
  end
  schema['required'] = required.to_a
end
new_node() click to toggle source
# File lib/openapi_first/operation.rb, line 142
def new_node
  {
    'type' => 'object',
    'required' => [],
    'properties' => {}
  }
end
operation_object() click to toggle source
# File lib/openapi_first/operation.rb, line 99
def operation_object
  @path_item_object[method]
end
response_by_code(status) click to toggle source
# File lib/openapi_first/operation.rb, line 94
def response_by_code(status)
  operation_object.dig('responses', status.to_s) ||
    operation_object.dig('responses', 'default')
end