diff --git a/lib/api/v3/string_objects/string_objects_api.rb b/lib/api/v3/string_objects/string_objects_api.rb index 0fe8479228..8a28b19026 100644 --- a/lib/api/v3/string_objects/string_objects_api.rb +++ b/lib/api/v3/string_objects/string_objects_api.rb @@ -33,13 +33,19 @@ module API class StringObjectsAPI < Grape::API resources :string_objects do - namespace ':value' do + params do + requires :value, type: String + end + + # N.B. we are specifying our own requirements to allow arbitrary strings as value + # as of today values like '.foo' or 'foo.' would not be possible otherwise + route_param :value, requirements: { value: /.+/ } do get do StringObjectRepresenter.new(params[:value]) end end - # answer requests for empty strings too + # we need to explicitly cover the empty case because of the way route matching works get do StringObjectRepresenter.new('') end diff --git a/spec/requests/api/v3/string_objects_resource_spec.rb b/spec/requests/api/v3/string_objects_resource_spec.rb index 8b078a778b..ec2291a50f 100644 --- a/spec/requests/api/v3/string_objects_resource_spec.rb +++ b/spec/requests/api/v3/string_objects_resource_spec.rb @@ -48,5 +48,30 @@ describe 'API v3 String Objects resource' do it 'returns the value' do expect(subject.body).to be_json_eql('foo bar'.to_json).at_path('value') end + + context 'empty string' do + let(:path) { '/api/v3/string_objects/' } + + it 'is successful' do + expect(subject.status).to eql(200) + end + + it 'returns the value' do + expect(subject.body).to be_json_eql(''.to_json).at_path('value') + end + end + + # values starting with a dot is a known limitation of grapes default route matching + context 'beginning with .' do + let(:path) { '/api/v3/string_objects/.foo' } + + it 'is successful' do + expect(subject.status).to eql(200) + end + + it 'returns the value' do + expect(subject.body).to be_json_eql('.foo'.to_json).at_path('value') + end + end end end