Skip to content

Commit 886fbf6

Browse files
committed
Merged from master.
2 parents 633985c + 4de9620 commit 886fbf6

File tree

4 files changed

+28
-16
lines changed

4 files changed

+28
-16
lines changed

lib/grape/endpoint.rb

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ def prepare_routes
4848
options[:method].each do |method|
4949
options[:path].each do |path|
5050
prepared_path = prepare_path(path)
51-
path = compile_path(path, !options[:app])
51+
path = compile_path(prepared_path, !options[:app])
5252
regex = Rack::Mount::RegexpWithNamedGroups.new(path)
5353
path_params = regex.named_captures.map { |nc| nc[0] } - [ 'version', 'format' ]
5454
path_params |= (options[:route_options][:params] || [])
@@ -82,11 +82,10 @@ def namespace
8282
Rack::Mount::Utils.normalize_path(settings.stack.map{|s| s[:namespace]}.join('/'))
8383
end
8484

85-
def compile_path(path, anchor = true)
85+
def compile_path(prepared_path, anchor = true)
8686
endpoint_options = {}
8787
endpoint_options[:version] = /#{settings[:version].join('|')}/ if settings[:version]
88-
89-
Rack::Mount::Strexp.compile(prepare_path(path), endpoint_options, %w( / . ? ), anchor)
88+
Rack::Mount::Strexp.compile(prepared_path, endpoint_options, %w( / . ? ), anchor)
9089
end
9190

9291
def call(env)

lib/grape/middleware/base.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,8 @@ def decode_json(object)
111111
end
112112

113113
def encode_json(object)
114+
return object if object.is_a?(String)
115+
114116
if object.respond_to? :serializable_hash
115117
MultiJson.encode(object.serializable_hash)
116118
elsif object.respond_to? :to_json

spec/grape/api_spec.rb

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -175,33 +175,44 @@ def app; subject end
175175
last_response.body.should eql 'foo'
176176
end
177177

178-
it 'should allow for format' do
179-
subject.get("/abc") do
180-
"json"
178+
context "format" do
179+
before(:each) do
180+
subject.get("/abc") do
181+
RSpec::Mocks::Mock.new(:to_json => 'abc', :to_txt => 'def')
182+
end
183+
end
184+
185+
it "should allow .json" do
186+
get '/abc.json'
187+
last_response.status.should == 200
188+
last_response.body.should eql 'abc' # json-encoded symbol
181189
end
182190

183-
get '/abc.json'
184-
last_response.body.should eql '"json"'
191+
it "should allow .txt" do
192+
get '/abc.txt'
193+
last_response.status.should == 200
194+
last_response.body.should eql 'def' # raw text
195+
end
185196
end
186197

187198
it 'should allow for format without corrupting a param' do
188199
subject.get('/:id') do
189-
params[:id]
200+
{"id" => params[:id]}
190201
end
191202

192203
get '/awesome.json'
193-
last_response.body.should eql "\"awesome\""
204+
last_response.body.should eql '{"id":"awesome"}'
194205
end
195206

196207
it 'should allow for format in namespace with no path' do
197208
subject.namespace :abc do
198209
get do
199-
"json"
210+
["json"]
200211
end
201212
end
202213

203214
get '/abc.json'
204-
last_response.body.should eql '"json"'
215+
last_response.body.should eql '["json"]'
205216
end
206217

207218
it 'should allow for multiple verbs' do

spec/grape/middleware/formatter_spec.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
end
1515

1616
it 'should call #to_json first if it is available' do
17-
@body = "string"
17+
@body = ['foo']
1818
@body.instance_eval do
1919
def to_json
2020
"\"bar\""
@@ -124,9 +124,9 @@ def to_xml
124124
body.body.should == ['CUSTOM FORMAT']
125125
end
126126
it 'should use default json formatter' do
127-
@body = 'blah'
127+
@body = ['blah']
128128
_, _, body = subject.call({'PATH_INFO' => '/info.json'})
129-
body.body.should == ['"blah"']
129+
body.body.should == ['["blah"]']
130130
end
131131
it 'should use custom json formatter' do
132132
subject.options[:formatters][:json] = lambda { |obj| 'CUSTOM JSON FORMAT' }

0 commit comments

Comments
 (0)