Skip to content

Commit 729de7b

Browse files
author
Peter Scholz
committed
reduces rubocop warnings
1 parent b1be61c commit 729de7b

File tree

2 files changed

+74
-11
lines changed

2 files changed

+74
-11
lines changed

lib/grape-swagger/endpoint.rb

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -165,15 +165,13 @@ def response_object(route)
165165
response_model = @item
166166
response_model = expose_params_from_model(value[:model]) if value[:model]
167167

168+
next unless !response_model.start_with?('Swagger_doc') && ((@definitions[response_model] && value[:code].to_s.start_with?('2')) || value[:model])
169+
168170
# TODO: proof that the definition exist, if model isn't specified
169-
if !response_model.start_with?('Swagger_doc') &&
170-
((!!@definitions[response_model] && value[:code].to_s.start_with?('2')) ||
171-
value[:model])
172-
if route.route_is_array
173-
memo[value[:code]][:schema] = { 'type' => 'array', 'items' => { '$ref' => "#/definitions/#{response_model}" } }
174-
else
175-
memo[value[:code]][:schema] = { '$ref' => "#/definitions/#{response_model}" }
176-
end
171+
if route.route_is_array
172+
memo[value[:code]][:schema] = { 'type' => 'array', 'items' => { '$ref' => "#/definitions/#{response_model}" } }
173+
else
174+
memo[value[:code]][:schema] = { '$ref' => "#/definitions/#{response_model}" }
177175
end
178176
end
179177
end
@@ -190,7 +188,7 @@ def default_staus_codes
190188

191189
def params_object(route)
192190
partition_params(route).map do |param, value|
193-
parse_params(param, value, route.route_path, route.route_method)
191+
parse_params(param, { required: false }.merge(value), route.route_path, route.route_method)
194192
end
195193
end
196194

@@ -252,7 +250,7 @@ def expose_params_from_model(model)
252250
if GrapeEntity::VERSION =~ /0\.4\.\d/
253251
parameters = model.exposures ? model.exposures : model.documentation
254252
elsif GrapeEntity::VERSION =~ /0\.5\.\d/
255-
parameters = model.root_exposures.each_with_object({}) do |value,memo|
253+
parameters = model.root_exposures.each_with_object({}) do |value, memo|
256254
memo[value.attribute] = value.send(:options)
257255
end
258256
end
@@ -289,7 +287,7 @@ def parse_params(param, value, path, method)
289287
end
290288

291289
description = value.is_a?(Hash) ? value[:desc] || value[:description] : nil
292-
required = value.is_a?(Hash) ? !!value[:required] : false
290+
required = value.is_a?(Hash) ? value[:required] : false
293291
default_value = value.is_a?(Hash) ? value[:default] : nil
294292
example = value.is_a?(Hash) ? value[:example] : nil
295293
is_array = value.is_a?(Hash) ? (value[:is_array] || false) : false
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
require 'spec_helper'
2+
3+
describe 'format, content_type' do
4+
include_context "the api entities"
5+
6+
before :all do
7+
module TheApi
8+
class ProducesApi < Grape::API
9+
format :json
10+
11+
desc 'This uses formats for produces',
12+
failure: [{code: 400, model: Entities::ApiError}],
13+
formats: [:xml, :binary, "application/vdns"],
14+
entity: Entities::UseResponse
15+
get '/use_format' do
16+
{ "declared_params" => declared(params) }
17+
end
18+
19+
desc 'This uses content_types for produces',
20+
failure: [{code: 400, model: Entities::ApiError}],
21+
content_types: [:xml, :binary, "application/vdns"],
22+
entity: Entities::UseResponse
23+
get '/use_content_type' do
24+
{ "declared_params" => declared(params) }
25+
end
26+
27+
add_swagger_documentation
28+
end
29+
end
30+
end
31+
32+
def app
33+
TheApi::ProducesApi
34+
end
35+
36+
subject do
37+
get '/swagger_doc/use_format'
38+
JSON.parse(last_response.body)
39+
end
40+
41+
specify do
42+
require 'pry'; binding.pry
43+
expect(subject['paths']['/use_produces']['get']).to include('produces')
44+
expect(subject['paths']['/use_produces']['get']['produces']).to eql([
45+
'application/xml',
46+
'application/octet-stream',
47+
'application/vdns'
48+
])
49+
end
50+
51+
subject do
52+
get '/swagger_doc/use_content_type'
53+
JSON.parse(last_response.body)
54+
end
55+
56+
specify do
57+
require 'pry'; binding.pry
58+
expect(subject['paths']['/use_produces']['get']).to include('produces')
59+
expect(subject['paths']['/use_produces']['get']['produces']).to eql([
60+
'application/xml',
61+
'application/octet-stream',
62+
'application/vdns'
63+
])
64+
end
65+
end

0 commit comments

Comments
 (0)