Skip to content

Commit

Permalink
1. part of param type body handling
Browse files Browse the repository at this point in the history
  • Loading branch information
Peter Scholz committed Apr 7, 2016
1 parent 3c5a736 commit 41b4958
Show file tree
Hide file tree
Showing 9 changed files with 694 additions and 18 deletions.
4 changes: 3 additions & 1 deletion lib/grape-swagger/doc_methods.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
require 'grape-swagger/doc_methods/status_codes'

require 'grape-swagger/doc_methods/produces_consumes'
require 'grape-swagger/doc_methods/data_type'
require 'grape-swagger/doc_methods/extensions'
Expand All @@ -6,7 +8,7 @@
require 'grape-swagger/doc_methods/path_string'
require 'grape-swagger/doc_methods/tag_name_description'
require 'grape-swagger/doc_methods/parse_params'
# require 'grape-swagger/doc_methods/move_params'
require 'grape-swagger/doc_methods/move_params'

module GrapeSwagger
module DocMethods
Expand Down
119 changes: 119 additions & 0 deletions lib/grape-swagger/doc_methods/move_params.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
module GrapeSwagger
module DocMethods
class MoveParams
class << self
def to_definition(paths, definitions)
@definitions = definitions
find_post_put(paths) do |path|
find_definition_and_parameters(path)
end
end

def find_post_put(paths)
paths.each do |x|
found = x.last.select { |y| move_methods.include?(y) }
yield found unless found.empty?
end
end

def find_definition_and_parameters(path)
path.keys.each do |verb|
parameters = path[verb][:parameters]

next if parameters.nil?
next unless should_move?(parameters)

unify!(parameters)

status_code = GrapeSwagger::DocMethods::StatusCodes.get[verb.to_sym][:code]
response = path[verb][:responses][status_code]
referenced_definition = parse_model(response[:schema]['$ref'])

name = build_definition(verb, referenced_definition)

move_params_to_new(verb, name, parameters)
@definitions[name].delete(:required) if @definitions[name][:required].empty?
path[verb][:parameters] << build_body_parameter(response.dup, name)
end
end

def build_definition(verb, name)
name = "#{verb}Request#{name}".to_sym
@definitions[name] = { type: 'object', properties: {}, required: [] }

name
end

def move_params_to_new(_, name, parameters)
properties = {}
definition = @definitions[name]
request_parameters = parameters.dup

request_parameters.each do |param|
next unless movable?(param)
name = param[:name].to_sym
properties[name] = {}

properties[name].tap do |x|
property_keys.each do |attribute|
x[attribute] = param[attribute] unless param[attribute].nil?
end
end

properties[name][:readOnly] = true unless deletable?(param)
parameters.delete(param) if deletable?(param)
definition[:required] << name if deletable?(param) && param[:required]
end

definition[:properties] = properties
end

def build_body_parameter(response, name = false)
body_param = {}
body_param.tap do |x|
x[:name] = parse_model(response[:schema]['$ref'])
x[:in] = 'body'
x[:required] = true
x[:schema] = { '$ref' => response[:schema]['$ref'] } unless name
x[:schema] = { '$ref' => "#/definitions/#{name}" } if name
end
end

private

def unify!(params)
params.each do |param|
param[:in] = param.delete(:param_type) if param.key?(:param_type)
param[:in] = 'body' if param[:in] == 'formData'
end
end

def parse_model(ref)
ref.split('/').last
end

def move_methods
[:post, :put, :patch]
end

def property_keys
[:type, :format, :description, :minimum, :maximum, :items]
end

def movable?(param)
return true if param[:in] == 'body' || param[:in] == 'path'
false
end

def deletable?(param)
return true if movable?(param) && param[:in] == 'body'
false
end

def should_move?(parameters)
!parameters.select { |x| x[:in] == 'body' || x[:param_type] == 'body' }.empty?
end
end
end
end
end
17 changes: 17 additions & 0 deletions lib/grape-swagger/doc_methods/status_codes.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
module GrapeSwagger
module DocMethods
class StatusCodes
class << self
def get
{
get: { code: 200, message: 'get {item}(s)' },
post: { code: 201, message: 'created {item}' },
put: { code: 200, message: 'updated {item}' },
patch: { code: 200, message: 'patched {item}' },
delete: { code: 200, message: 'deleted {item}' }
}
end
end
end
end
end
15 changes: 2 additions & 13 deletions lib/grape-swagger/endpoint.rb
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,7 @@ def path_and_definition_objects(namespace_routes, options)
end

add_definitions_from options[:models]

# GrapeSwagger::DocMethods::MoveParams.to_definition(@paths, @definitions)
GrapeSwagger::DocMethods::MoveParams.to_definition(@paths, @definitions)
[@paths, @definitions]
end

Expand Down Expand Up @@ -151,7 +150,7 @@ def params_object(route)
end

def response_object(route)
default_code = default_status_codes[route.route_method.downcase.to_sym]
default_code = GrapeSwagger::DocMethods::StatusCodes.get[route.route_method.downcase.to_sym]
default_code[:model] = @entity if @entity
default_code[:message] = route.route_description || default_code[:message].sub('{item}', @item)

Expand Down Expand Up @@ -291,16 +290,6 @@ def model_name(name)
name.respond_to?(:name) ? name.name.demodulize.camelize : name.split('::').last
end

def default_status_codes
{
get: { code: 200, message: 'get {item}(s)' },
post: { code: 201, message: 'created {item}' },
put: { code: 200, message: 'updated {item}' },
patch: { code: 200, message: 'patched {item}' },
delete: { code: 200, message: 'deleted {item}' }
}
end

def could_it_be_a_model?(value)
(
value[:type].to_s.include?('Entity') || value[:type].to_s.include?('Entities')
Expand Down
Loading

0 comments on commit 41b4958

Please sign in to comment.