lib/grape-swagger/endpoint.rb in grape-swagger-0.21.0 vs lib/grape-swagger/endpoint.rb in grape-swagger-0.22.0

- old
+ new

@@ -22,10 +22,11 @@ { info: info_object(options[:info].merge(version: options[:doc_version])), swagger: '2.0', produces: content_types_for(target_class), authorizations: options[:authorizations], + securityDefinitions: options[:security_definitions], host: GrapeSwagger::DocMethods::OptionalObject.build(:host, options, request), basePath: GrapeSwagger::DocMethods::OptionalObject.build(:base_path, options, request), tags: GrapeSwagger::DocMethods::TagNameDescription.build(options), schemes: options[:schemes].is_a?(String) ? [options[:schemes]] : options[:schemes] }.delete_if { |_, value| value.blank? } @@ -69,11 +70,10 @@ routes = namespace_routes[key] path_item(routes, options) end add_definitions_from options[:models] - GrapeSwagger::DocMethods::MoveParams.to_definition(@paths, @definitions) [@paths, @definitions] end def add_definitions_from(models) return if models.nil? @@ -107,11 +107,11 @@ method[:description] = description_object(route, options[:markdown]) method[:produces] = produces_object(route, options[:produces] || options[:format]) method[:consumes] = consumes_object(route, options[:format]) method[:parameters] = params_object(route) method[:responses] = response_object(route, options[:markdown]) - method[:tags] = tag_object(route, options[:version].to_s) + method[:tags] = tag_object(route) method[:operationId] = GrapeSwagger::DocMethods::OperationId.build(route, path) method.delete_if { |_, value| value.blank? } [route.request_method.downcase.to_sym, method] end @@ -152,15 +152,21 @@ mime_types end def params_object(route) - partition_params(route).map do |param, value| + parameters = partition_params(route).map do |param, value| value = { required: false }.merge(value) if value.is_a?(Hash) _, value = default_type([[param, value]]).first if value == '' GrapeSwagger::DocMethods::ParseParams.call(param, value, route) end + + if GrapeSwagger::DocMethods::MoveParams.can_be_moved?(parameters, route.request_method) + parameters = GrapeSwagger::DocMethods::MoveParams.to_definition(parameters, route, @definitions) + end + + parameters end def response_object(route, markdown) default_code = GrapeSwagger::DocMethods::StatusCodes.get[route.request_method.downcase.to_sym] default_code[:model] = @entity if @entity @@ -192,20 +198,20 @@ { '$ref' => "#/definitions/#{response_model}" } end end end - def tag_object(route, version) - Array(route.path.split('{')[0].split('/').reject(&:empty?).delete_if { |i| ((i == route.prefix.to_s) || (i == version)) }.first) + def tag_object(route) + Array(route.path.split('{')[0].split('/').reject(&:empty?).delete_if { |i| ((i == route.prefix.to_s) || (i == route.version)) }.first) end private def partition_params(route) declared_params = route.settings[:declared_params] if route.settings[:declared_params].present? required, exposed = route.params.partition { |x| x.first.is_a? String } - required.concat GrapeSwagger::DocMethods::Headers.parse(route) unless route.headers.nil? + required = GrapeSwagger::DocMethods::Headers.parse(route) + required unless route.headers.nil? default_type(required) default_type(exposed) unless declared_params.nil? && route.headers.nil? request_params = parse_request_params(required) @@ -219,24 +225,29 @@ params.each do |param| param[-1] = param.last == '' ? { required: true, type: 'Integer' } : param.last end end - def parse_request_params(required) - required.each_with_object({}) do |param, memo| - @array_key = param.first.to_s.gsub('[', '[][') if param.last[:type] == 'Array' - possible_key = param.first.to_s.gsub('[', '[][') - if @array_key && possible_key.start_with?(@array_key) - key = possible_key - param.last[:is_array] = true - else - key = param.first - end - memo[key] = param.last unless param.last[:type] == 'Hash' || param.last[:type] == 'Array' && !param.last.key?(:documentation) + def parse_request_params(params) + array_key = nil + params.select { |param| public_parameter?(param) }.each_with_object({}) do |param, memo| + name, options = *param + array_key = name.to_s if param_type_is_array?(options[:type]) + options[:is_array] = true if array_key && name.start_with?(array_key) + memo[param.first] = options unless (options[:type] == 'Hash' || options[:type] == 'Array') && !options.key?(:documentation) end end + def param_type_is_array?(param_type) + return false unless param_type + return true if param_type == 'Array' + param_types = param_type.match(/\[(.*)\]$/) + return false unless param_types + param_types = param_types[0].split(',') if param_types + param_types.size == 1 + end + def expose_params_from_model(model) model_name = model_name(model) return model_name if @definitions.key?(model_name) @definitions[model_name] = nil @@ -266,8 +277,16 @@ def hidden?(route) route_hidden = route.options[:hidden] route_hidden = route_hidden.call if route_hidden.is_a?(Proc) route_hidden + end + + def public_parameter?(param) + param_options = param.last + return true unless param_options.key?(:documentation) && !param_options[:required] + param_hidden = param_options[:documentation].fetch(:hidden, false) + param_hidden = param_hidden.call if param_hidden.is_a?(Proc) + !param_hidden end end end