Skip to content

Commit 81b6f9d

Browse files
authored
Merge pull request #204 from gtt-project/fix/geojson-output-format
Fix projects/issues/users index/show api to return json object correctly
2 parents 677c51d + 85a4e7d commit 81b6f9d

File tree

9 files changed

+247
-17
lines changed

9 files changed

+247
-17
lines changed

app/views/issues/index.api.rsb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,9 @@ api.array :issues, api_meta(:total_count => @issue_count, :offset => @offset, :l
2121
api.estimated_hours issue.estimated_hours
2222

2323
if issue.geom
24-
api.geojson issue.geojson.to_json
24+
api.geojson (params[:format] == "json") ? issue.geojson : issue.geojson.to_json
2525
else
26-
api.geojson ""
26+
api.geojson nil
2727
end
2828

2929
if issue.distance

app/views/issues/show.api.rsb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,9 @@ api.issue do
2424
end
2525

2626
if @issue.geom
27-
api.geojson @issue.geojson
27+
api.geojson (params[:format] == "json") ? @issue.geojson : @issue.geojson.to_json
2828
else
29-
api.geojson ""
29+
api.geojson nil
3030
end
3131

3232
render_api_custom_values @issue.visible_custom_field_values, api

app/views/projects/index.api.rsb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@ api.array :projects, api_meta(:total_count => @project_count, :offset => @offset
1111

1212
if @include_geometry
1313
if project.geom
14-
api.geojson project.geojson.to_json
14+
api.geojson (params[:format] == "json") ? project.geojson : project.geojson.to_json
1515
else
16-
api.geojson ""
16+
api.geojson nil
1717
end
1818
end
1919

app/views/projects/show.api.rsb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@ api.project do
99
api.is_public @project.is_public?
1010

1111
if @project.geom
12-
api.geojson @project.geojson.to_json
12+
api.geojson (params[:format] == "json") ? @project.geojson : @project.geojson.to_json
1313
else
14-
api.geojson ""
14+
api.geojson nil
1515
end
1616

1717
render_api_custom_values @project.visible_custom_field_values, api

app/views/users/index.api.rsb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,9 @@ api.array :users, api_meta(:total_count => @user_count, :offset => @offset, :lim
1010
api.last_login_on user.last_login_on
1111

1212
if user.geom
13-
api.geojson user.geojson
13+
api.geojson (params[:format] == "json") ? user.geojson : user.geojson.to_json
1414
else
15-
api.geojson ""
15+
api.geojson nil
1616
end
1717

1818
render_api_custom_values user.visible_custom_field_values, api

app/views/users/show.api.rsb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,9 @@ api.user do
1010
api.status @user.status if User.current.admin?
1111

1212
if @user.geom
13-
api.geojson @user.geojson
13+
api.geojson (params[:format] == "json") ? @user.geojson : @user.geojson.to_json
1414
else
15-
api.geojson ""
15+
api.geojson nil
1616
end
1717

1818
render_api_custom_values @user.visible_custom_field_values, api
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
require_relative '../test_helper'
2+
3+
class IssuesApiTest < Redmine::ApiTest::Base
4+
fixtures :projects,
5+
:users,
6+
:roles,
7+
:members,
8+
:member_roles,
9+
:issues,
10+
:issue_statuses,
11+
:enabled_modules
12+
13+
setup do
14+
@project = Project.find 'ecookbook'
15+
@project.enabled_modules.create name: 'gtt'
16+
end
17+
18+
test 'should include geojson' do
19+
geo = {
20+
'type' => 'Feature',
21+
'geometry' => {
22+
'type' => 'Point',
23+
'coordinates' => [123.269691,9.305099]
24+
}
25+
}
26+
geojson = geo.to_json
27+
28+
issue = @project.issues.find 1
29+
issue.update_attribute :geojson, geojson
30+
31+
# xml format - index api
32+
get '/issues.xml'
33+
assert_response :success
34+
xml = xml_data
35+
assert json = xml.xpath('/issues/issue[id=1]/geojson').text
36+
assert json.present?
37+
assert_match(/123\.269691/, json)
38+
assert_equal geo['geometry'], JSON.parse(json)['geometry'], json
39+
# xml format - show api
40+
get '/issues/1.xml'
41+
assert_response :success
42+
xml = xml_data
43+
assert json = xml.xpath('/issue/geojson').text
44+
assert json.present?
45+
assert_match(/123\.269691/, json)
46+
assert_equal geo['geometry'], JSON.parse(json)['geometry'], json
47+
48+
# json format - index api
49+
get '/issues.json'
50+
assert_response :success
51+
assert json = JSON.parse(@response.body)
52+
hsh = json['issues'].detect{|i|i['id'] == issue.id}['geojson']
53+
assert_equal geo['geometry'], hsh['geometry']
54+
# json format - show api
55+
get '/issues/1.json'
56+
assert_response :success
57+
assert json = JSON.parse(@response.body)
58+
hsh = json['issue']['geojson']
59+
assert_equal geo['geometry'], hsh['geometry']
60+
end
61+
62+
test 'should include empty geojson' do
63+
issue = @project.issues.find 1
64+
issue.update_attribute :geojson, nil
65+
66+
# xml format - index api
67+
get '/issues.xml'
68+
assert_response :success
69+
xml = xml_data
70+
assert json = xml.xpath('/issues/issue[id=1]/geojson').text
71+
assert_equal "", json
72+
# xml format - show api
73+
get '/issues/1.xml'
74+
assert_response :success
75+
xml = xml_data
76+
assert json = xml.xpath('/issue/geojson').text
77+
assert_equal "", json
78+
79+
# json format - index api
80+
get '/issues.json'
81+
assert_response :success
82+
assert json = JSON.parse(@response.body)
83+
hsh = json['issues'].detect{|p|p['id'] == issue.id}['geojson']
84+
assert_nil hsh
85+
# json format - show api
86+
get '/issues/1.json'
87+
assert_response :success
88+
assert json = JSON.parse(@response.body)
89+
hsh = json['issue']['geojson']
90+
assert_nil hsh
91+
end
92+
93+
def xml_data
94+
Nokogiri::XML(@response.body)
95+
end
96+
end

test/integration/projects_api_test.rb

Lines changed: 48 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ class ProjectsApiTest < Redmine::IntegrationTest
5656

5757
end
5858

59-
test 'should include geojson on demand' do
59+
test 'should include geojson (on demand)' do
6060
geo = {
6161
'type' => 'Feature',
6262
'geometry' => {
@@ -69,23 +69,66 @@ class ProjectsApiTest < Redmine::IntegrationTest
6969
geojson = geo.to_json
7070

7171
@project.update_attribute :geojson, geojson
72-
@subproject1.update_attribute :geojson, geojson
72+
# xml format - index api
7373
get '/projects.xml?include=geometry'
7474
assert_response :success
7575
xml = xml_data
76-
assert projects = xml.xpath('/projects/project')
77-
assert json = projects.xpath('geojson').first.text
76+
assert json = xml.xpath('/projects/project[id=1]/geojson').text
77+
assert json.present?
78+
assert_match(/123\.269691/, json)
79+
assert_equal geo['geometry'], JSON.parse(json)['geometry'], json
80+
# xml format - show api
81+
get '/projects/1.xml'
82+
assert_response :success
83+
xml = xml_data
84+
assert json = xml.xpath('/project/geojson').text
7885
assert json.present?
7986
assert_match(/123\.269691/, json)
8087
assert_equal geo['geometry'], JSON.parse(json)['geometry'], json
8188

89+
# json format - index api
8290
get '/projects.json?include=geometry'
8391
assert_response :success
8492
assert json = JSON.parse(@response.body)
85-
hsh = JSON.parse json['projects'].detect{|p|p['id'] == @project.id}['geojson']
93+
hsh = json['projects'].detect{|p|p['id'] == @project.id}['geojson']
94+
assert_equal geo['geometry'], hsh['geometry']
95+
# json format - show api
96+
get '/projects/1.json'
97+
assert_response :success
98+
assert json = JSON.parse(@response.body)
99+
hsh = json['project']['geojson']
86100
assert_equal geo['geometry'], hsh['geometry']
87101
end
88102

103+
test 'should include empty geojson (on demand)' do
104+
@project.update_attribute :geojson, nil
105+
# xml format - index api
106+
get '/projects.xml?include=geometry'
107+
assert_response :success
108+
xml = xml_data
109+
assert json = xml.xpath('/projects/project[id=1]/geojson').text
110+
assert_equal "", json
111+
# xml format - show api
112+
get '/projects/1.xml'
113+
assert_response :success
114+
xml = xml_data
115+
assert json = xml.xpath('/project/geojson').text
116+
assert_equal "", json
117+
118+
# json format - index api
119+
get '/projects.json?include=geometry'
120+
assert_response :success
121+
assert json = JSON.parse(@response.body)
122+
hsh = json['projects'].detect{|p|p['id'] == @project.id}['geojson']
123+
assert_nil hsh
124+
# json format - show api
125+
get '/projects/1.json'
126+
assert_response :success
127+
assert json = JSON.parse(@response.body)
128+
hsh = json['project']['geojson']
129+
assert_nil hsh
130+
end
131+
89132
test 'should filter projects by query and geometry' do
90133
# default
91134
get '/projects.xml'

test/integration/users_api_test.rb

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
require_relative '../test_helper'
2+
3+
class UsersApiTest < Redmine::ApiTest::Base
4+
fixtures :users,
5+
:email_addresses,
6+
:members,
7+
:member_roles,
8+
:roles,
9+
:projects
10+
11+
setup do
12+
@user = User.find_by_login 'jsmith' # id=2
13+
end
14+
15+
test 'should include geojson' do
16+
geo = {
17+
'type' => 'Feature',
18+
'geometry' => {
19+
'type' => 'Point',
20+
'coordinates' => [123.269691,9.305099]
21+
}
22+
}
23+
geojson = geo.to_json
24+
25+
@user.update_attribute :geojson, geojson
26+
27+
# xml format - index api
28+
get '/users.xml', :headers => credentials('admin')
29+
assert_response :success
30+
xml = xml_data
31+
assert json = xml.xpath('/users/user[id=2]/geojson').text
32+
assert json.present?
33+
assert_match(/123\.269691/, json)
34+
assert_equal geo['geometry'], JSON.parse(json)['geometry'], json
35+
# xml format - show api
36+
get '/users/2.xml', :headers => credentials('admin')
37+
assert_response :success
38+
xml = xml_data
39+
assert json = xml.xpath('/user/geojson').text
40+
assert json.present?
41+
assert_match(/123\.269691/, json)
42+
assert_equal geo['geometry'], JSON.parse(json)['geometry'], json
43+
44+
# json format - index api
45+
get '/users.json', :headers => credentials('admin')
46+
assert_response :success
47+
assert json = JSON.parse(@response.body)
48+
hsh = json['users'].detect{|i|i['id'] == @user.id}['geojson']
49+
assert_equal geo['geometry'], hsh['geometry']
50+
# json format - show api
51+
get '/users/2.json', :headers => credentials('admin')
52+
assert_response :success
53+
assert json = JSON.parse(@response.body)
54+
hsh = json['user']['geojson']
55+
assert_equal geo['geometry'], hsh['geometry']
56+
end
57+
58+
test 'should include empty geojson' do
59+
@user.update_attribute :geojson, nil
60+
61+
# xml format - index api
62+
get '/users.xml', :headers => credentials('admin')
63+
assert_response :success
64+
xml = xml_data
65+
assert json = xml.xpath('/users/user[id=2]/geojson').text
66+
assert_equal "", json
67+
# xml format - show api
68+
get '/users/2.xml', :headers => credentials('admin')
69+
assert_response :success
70+
xml = xml_data
71+
assert json = xml.xpath('/user/geojson').text
72+
assert_equal "", json
73+
74+
# json format - index api
75+
get '/users.json', :headers => credentials('admin')
76+
assert_response :success
77+
assert json = JSON.parse(@response.body)
78+
hsh = json['users'].detect{|p|p['id'] == @user.id}['geojson']
79+
assert_nil hsh
80+
# json format - show api
81+
get '/users/2.json', :headers => credentials('admin')
82+
assert_response :success
83+
assert json = JSON.parse(@response.body)
84+
hsh = json['user']['geojson']
85+
assert_nil hsh
86+
end
87+
88+
def xml_data
89+
Nokogiri::XML(@response.body)
90+
end
91+
end

0 commit comments

Comments
 (0)