lib/grape-swagger/endpoint.rb in grape-swagger-0.20.2 vs lib/grape-swagger/endpoint.rb in grape-swagger-0.20.3

- old
+ new

@@ -18,11 +18,11 @@ # swagger spec2.0 related parts # # required keys for SwaggerObject def swagger_object(target_class, request, options) { - info: info_object(options[:info].merge(version: options[:api_version])), + info: info_object(options[:info].merge(version: options[:doc_version])), swagger: '2.0', produces: content_types_for(target_class), authorizations: options[:authorizations], host: GrapeSwagger::DocMethods::OptionalObject.build(:host, options, request.host_with_port), basePath: GrapeSwagger::DocMethods::OptionalObject.build(:base_path, options, request.env['SCRIPT_NAME']), @@ -84,62 +84,63 @@ # path object def path_item(routes, options) routes.each do |route| next if hidden?(route) - @item, path = GrapeSwagger::DocMethods::PathString.build(route.route_path, options) - @entity = route.route_entity || route.route_success + @item, path = GrapeSwagger::DocMethods::PathString.build(route.path, options) + @entity = route.entity || route.options[:success] - method = route.route_method.downcase.to_sym - request_params = method_object(route, options, path) + verb, method_object = method_object(route, options, path) - if @paths.key?(path.to_sym) - @paths[path.to_sym][method] = request_params + if @paths.key?(path.to_s) + @paths[path.to_s][verb] = method_object else - @paths[path.to_sym] = { method => request_params } + @paths[path.to_s] = { verb => method_object } end - GrapeSwagger::DocMethods::Extensions.add(@paths[path.to_sym], @definitions, route) + GrapeSwagger::DocMethods::Extensions.add(@paths[path.to_s], @definitions, route) end end def method_object(route, options, path) method = {} 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]) - method[:operationId] = GrapeSwagger::DocMethods::OperationId.build(route.route_method, path) + method[:tags] = tag_object(route, options[:version].to_s) + method[:operationId] = GrapeSwagger::DocMethods::OperationId.build(route, path) method.delete_if { |_, value| value.blank? } + + [route.request_method.downcase.to_sym, method] end def description_object(route, markdown) - description = route.route_desc if route.route_desc.present? - description = route.route_description if route.route_description.present? + description = route.options[:desc] if route.options.key?(:desc) + description = route.description if route.description.present? description = "# #{description} " if markdown - description += "\n #{route.route_detail}" if route.route_detail + description += "\n #{route.options[:detail]}" if route.options.key?(:detail) description = markdown.markdown(description.to_s).chomp if markdown description end def produces_object(route, format) mime_types = GrapeSwagger::DocMethods::ProducesConsumes.call(format) - route_mime_types = [:route_formats, :route_content_types, :route_produces].map do |producer| - possible = route.send(producer) + route_mime_types = [:formats, :content_types, :produces].map do |producer| + possible = route.options[producer] GrapeSwagger::DocMethods::ProducesConsumes.call(possible) if possible.present? end.flatten.compact.uniq route_mime_types.present? ? route_mime_types : mime_types end def consumes_object(route, format) - method = route.route_method.downcase.to_sym - format = route.route_settings[:description][:consumes] if route.route_settings[:description] && route.route_settings[:description][:consumes] + method = route.request_method.downcase.to_sym + format = route.settings[:description][:consumes] if route.settings[:description] && route.settings[:description][:consumes] mime_types = GrapeSwagger::DocMethods::ProducesConsumes.call(format) if [:post, :put].include?(method) mime_types end @@ -150,72 +151,60 @@ GrapeSwagger::DocMethods::ParseParams.call(param, value, route) end end def response_object(route, markdown) - default_code = GrapeSwagger::DocMethods::StatusCodes.get[route.route_method.downcase.to_sym] + default_code = GrapeSwagger::DocMethods::StatusCodes.get[route.request_method.downcase.to_sym] default_code[:model] = @entity if @entity - default_code[:message] = route.route_description || default_code[:message].sub('{item}', @item) + default_code[:message] = route.description || default_code[:message].sub('{item}', @item) - codes = [default_code] + (route.route_http_codes || route.route_failure || []) + codes = [default_code] + (route.http_codes || route.options[:failure] || []) codes.map! { |x| x.is_a?(Array) ? { code: x[0], message: x[1], model: x[2] } : x } codes.each_with_object({}) do |value, memo| memo[value[:code]] = { description: value[:message] } response_model = @item response_model = expose_params_from_model(value[:model]) if value[:model] - if memo.key?(200) && route.route_method == 'DELETE' && value[:model].nil? + if memo.key?(200) && route.request_method == 'DELETE' && value[:model].nil? memo[204] = memo.delete(200) value[:code] = 204 end next if memo.key?(204) next unless !response_model.start_with?('Swagger_doc') && ((@definitions[response_model] && value[:code].to_s.start_with?('2')) || value[:model]) @definitions[response_model][:description] = description_object(route, markdown) # TODO: proof that the definition exist, if model isn't specified - memo[value[:code]][:schema] = if route.route_is_array + memo[value[:code]][:schema] = if route.options[:is_array] { 'type' => 'array', 'items' => { '$ref' => "#/definitions/#{response_model}" } } else { '$ref' => "#/definitions/#{response_model}" } end end end def tag_object(route, version) - Array(route.route_path.split('{')[0].split('/').reject(&:empty?).delete_if { |i| ((i == route.route_prefix.to_s) || (i == version)) }.first) + Array(route.path.split('{')[0].split('/').reject(&:empty?).delete_if { |i| ((i == route.prefix.to_s) || (i == version)) }.first) end private def partition_params(route) - declared_params = route.route_settings[:declared_params] if route.route_settings[:declared_params].present? - required, exposed = route.route_params.partition { |x| x.first.is_a? String } - required.concat GrapeSwagger::DocMethods::Headers.parse(route) unless route.route_headers.nil? + 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? default_type(required) default_type(exposed) - unless declared_params.nil? && route.route_headers.nil? + unless declared_params.nil? && route.headers.nil? request_params = parse_request_params(required) end - if !exposed.empty? - exposed_params = exposed.each_with_object({}) { |x, memo| memo[x.first] = x.last } - properties = parse_response_params(exposed_params) - else - properties = parse_response_params(required) - end - key = model_name(@entity || @item) - - unless properties.empty? || (route.route_method == 'DELETE' && !@entity) - @definitions[key] = { type: 'object', properties: properties } unless @definitions.key?(key) - @definitions[key][:properties].merge!(properties) if @definitions.key?(key) - end - return route.route_params if route.route_params.present? && !route.route_settings[:declared_params].present? + return route.params if route.params.present? && !route.settings[:declared_params].present? request_params || {} end def default_type(params) params.each do |param| @@ -304,13 +293,11 @@ !value[:type] == Array ) end def hidden?(route) - if route.route_hidden - return route.route_hidden.is_a?(Proc) ? route.route_hidden.call : route.route_hidden - end - - false + route_hidden = route.options[:hidden] + route_hidden = route_hidden.call if route_hidden.is_a?(Proc) + route_hidden end end end