]> git.openstreetmap.org Git - rails.git/commitdiff
Merge pull request #2485 from mmd-osm/patch/json2
authorAndy Allan <git@gravitystorm.co.uk>
Wed, 26 Feb 2020 14:55:48 +0000 (15:55 +0100)
committerGitHub <noreply@github.com>
Wed, 26 Feb 2020 14:55:48 +0000 (15:55 +0100)
JSON output nodes, ways, relations, map

43 files changed:
app/controllers/api/map_controller.rb
app/controllers/api/nodes_controller.rb
app/controllers/api/old_controller.rb
app/controllers/api/relations_controller.rb
app/controllers/api/ways_controller.rb
app/controllers/api_controller.rb
app/controllers/application_controller.rb
app/models/concerns/geo_record.rb
app/views/api/_root_attributes.json.jbuilder [new file with mode: 0644]
app/views/api/map/_bounds.json.jbuilder [new file with mode: 0644]
app/views/api/map/index.json.jbuilder [new file with mode: 0644]
app/views/api/nodes/_node.json.jbuilder [new file with mode: 0644]
app/views/api/nodes/index.json.jbuilder [new file with mode: 0644]
app/views/api/nodes/show.json.jbuilder [new file with mode: 0644]
app/views/api/old_nodes/_old_node.json.jbuilder [new file with mode: 0644]
app/views/api/old_nodes/history.json.jbuilder [new file with mode: 0644]
app/views/api/old_nodes/version.json.jbuilder [new file with mode: 0644]
app/views/api/old_relations/_old_relation.json.jbuilder [new file with mode: 0644]
app/views/api/old_relations/history.json.jbuilder [new file with mode: 0644]
app/views/api/old_relations/version.json.jbuilder [new file with mode: 0644]
app/views/api/old_ways/_old_way.json.jbuilder [new file with mode: 0644]
app/views/api/old_ways/history.json.jbuilder [new file with mode: 0644]
app/views/api/old_ways/version.json.jbuilder [new file with mode: 0644]
app/views/api/relations/_relation.json.jbuilder [new file with mode: 0644]
app/views/api/relations/full.json.jbuilder [new file with mode: 0644]
app/views/api/relations/index.json.jbuilder [new file with mode: 0644]
app/views/api/relations/relations_for_node.json.jbuilder [new file with mode: 0644]
app/views/api/relations/relations_for_relation.json.jbuilder [new file with mode: 0644]
app/views/api/relations/relations_for_way.json.jbuilder [new file with mode: 0644]
app/views/api/relations/show.json.jbuilder [new file with mode: 0644]
app/views/api/ways/_way.json.jbuilder [new file with mode: 0644]
app/views/api/ways/full.json.jbuilder [new file with mode: 0644]
app/views/api/ways/index.json.jbuilder [new file with mode: 0644]
app/views/api/ways/show.json.jbuilder [new file with mode: 0644]
app/views/api/ways/ways_for_node.json.jbuilder [new file with mode: 0644]
test/controllers/api/map_controller_test.rb
test/controllers/api/nodes_controller_test.rb
test/controllers/api/old_nodes_controller_test.rb
test/controllers/api/old_relations_controller_test.rb
test/controllers/api/old_ways_controller_test.rb
test/controllers/api/relations_controller_test.rb
test/controllers/api/ways_controller_test.rb
test/test_helper.rb

index b0998f7ebaa9aa6ae49f8aa125f0583f5353049e..113554c72fb38ad590fcbfcf869ce8d7a20f4747 100644 (file)
@@ -5,6 +5,8 @@ module Api
     before_action :check_api_readable
     around_action :api_call_handle_error, :api_call_timeout
 
+    before_action :set_default_request_format
+
     # This is probably the most common call of all. It is used for getting the
     # OSM data for a specified bounding box, usually for editing. First the
     # bounding box (bbox) is checked to make sure that it is sane. All nodes
@@ -90,7 +92,10 @@ module Api
 
       response.headers["Content-Disposition"] = "attachment; filename=\"map.osm\""
       # Render the result
-      render :formats => [:xml]
+      respond_to do |format|
+        format.xml
+        format.json
+      end
     end
   end
 end
index 2962ce070ffe15ebf1cd98f43cb66adaa568c767..336aebc915793f05f4f7429214cb6ba74b169eb5 100644 (file)
@@ -13,6 +13,8 @@ module Api
     before_action :check_api_readable, :except => [:create, :update, :delete]
     around_action :api_call_handle_error, :api_call_timeout
 
+    before_action :set_default_request_format, :except => [:create, :update, :delete]
+
     # Create a node from XML.
     def create
       assert_method :put
@@ -32,7 +34,10 @@ module Api
 
       if @node.visible
         # Render the result
-        render :formats => [:xml]
+        respond_to do |format|
+          format.xml
+          format.json
+        end
       else
         head :gone
       end
@@ -73,7 +78,10 @@ module Api
       @nodes = Node.find(ids)
 
       # Render the result
-      render :formats => [:xml]
+      respond_to do |format|
+        format.xml
+        format.json
+      end
     end
   end
 end
index 862d14716f9b12e34c25e6629ada8d1b7e16b8e2..f672e7c4960d9c66c1d0e94c9dd2eac272c53a0e 100644 (file)
@@ -16,6 +16,8 @@ module Api
     before_action :lookup_old_element, :except => [:history]
     before_action :lookup_old_element_versions, :only => [:history]
 
+    before_action :set_default_request_format, :except => [:redact]
+
     def history
       # the .where() method used in the lookup_old_element_versions
       # call won't throw an error if no records are found, so we have
@@ -30,7 +32,10 @@ module Api
                end
 
       # Render the result
-      render :formats => [:xml]
+      respond_to do |format|
+        format.xml
+        format.json
+      end
     end
 
     def version
@@ -41,7 +46,10 @@ module Api
         response.last_modified = @old_element.timestamp
 
         # Render the result
-        render :formats => [:xml]
+        respond_to do |format|
+          format.xml
+          format.json
+        end
       end
     end
 
index ba0dd0c6bb99ae870211f19568b3ad8445dc434c..329c5e29c7fa7adfc504da3ab7efcd719701adc7 100644 (file)
@@ -11,6 +11,8 @@ module Api
     before_action :check_api_readable, :except => [:create, :update, :delete]
     around_action :api_call_handle_error, :api_call_timeout
 
+    before_action :set_default_request_format, :except => [:create, :update, :delete]
+
     def create
       assert_method :put
 
@@ -26,7 +28,10 @@ module Api
       response.last_modified = @relation.timestamp
       if @relation.visible
         # Render the result
-        render :formats => [:xml]
+        respond_to do |format|
+          format.xml
+          format.json
+        end
       else
         head :gone
       end
@@ -117,7 +122,10 @@ module Api
         @relations << relation
 
         # Render the result
-        render :formats => [:xml]
+        respond_to do |format|
+          format.xml
+          format.json
+        end
       else
         head :gone
       end
@@ -133,7 +141,10 @@ module Api
       @relations = Relation.find(ids)
 
       # Render the result
-      render :formats => [:xml]
+      respond_to do |format|
+        format.xml
+        format.json
+      end
     end
 
     def relations_for_way
@@ -160,7 +171,10 @@ module Api
       end
 
       # Render the result
-      render :formats => [:xml]
+      respond_to do |format|
+        format.xml
+        format.json
+      end
     end
   end
 end
index 9af087d83a31fb06de92ce0a5718f9aa10225cd7..a7c876710ce4c8eafbbab30566d63f42a71a48ca 100644 (file)
@@ -11,6 +11,8 @@ module Api
     before_action :check_api_readable, :except => [:create, :update, :delete]
     around_action :api_call_handle_error, :api_call_timeout
 
+    before_action :set_default_request_format, :except => [:create, :update, :delete]
+
     def create
       assert_method :put
 
@@ -28,7 +30,10 @@ module Api
 
       if @way.visible
         # Render the result
-        render :formats => [:xml]
+        respond_to do |format|
+          format.xml
+          format.json
+        end
       else
         head :gone
       end
@@ -75,7 +80,10 @@ module Api
         end
 
         # Render the result
-        render :formats => [:xml]
+        respond_to do |format|
+          format.xml
+          format.json
+        end
       else
         head :gone
       end
@@ -93,7 +101,10 @@ module Api
       @ways = Way.find(ids)
 
       # Render the result
-      render :formats => [:xml]
+      respond_to do |format|
+        format.xml
+        format.json
+      end
     end
 
     ##
@@ -106,7 +117,10 @@ module Api
       @ways = Way.where(:id => wayids, :visible => true)
 
       # Render the result
-      render :formats => [:xml]
+      respond_to do |format|
+        format.xml
+        format.json
+      end
     end
   end
 end
index 44efdc0711f2cb39d53e8b59d33d58e8583dca5b..64514a4321f8ff3523621fae28b2830f6f3fa6e5 100644 (file)
@@ -3,6 +3,56 @@ class ApiController < ApplicationController
 
   private
 
+  ##
+  # Set default request format to xml unless a client requests a specific format,
+  # which can be done via (a) URL suffix and/or (b) HTTP Accept header, where
+  # the URL suffix always takes precedence over the Accept header.
+  def set_default_request_format
+    unless params[:format]
+      accept_header = request.headers["HTTP_ACCEPT"]
+      if accept_header.nil?
+        # e.g. unit tests don't set an Accept: header by default, force XML in this case
+        request.format = "xml"
+        return
+      end
+
+      req_mimetypes = []
+
+      # Some clients (JOSM) send Accept headers which cannot be parsed by Rails, example: *; q=.2
+      # To be fair, JOSM's Accept header doesn't adhere to RFC 7231, section 5.3.1, et al. either
+      # As a workaround for backwards compatibility, we're assuming XML format
+      begin
+        req_mimetypes = Mime::Type.parse(accept_header)
+      rescue Mime::Type::InvalidMimeType
+        request.format = "xml"
+        return
+      end
+
+      # req_mimetypes contains all Accept header MIME types with descending priority
+      req_mimetypes.each do |mime|
+        if mime.symbol == :xml
+          request.format = "xml"
+          break
+        end
+
+        if mime.symbol == :json
+          request.format = "json"
+          break
+        end
+
+        # Any format, not explicitly requesting XML or JSON -> assume XML as default
+        if mime == "*/*"
+          request.format = "xml"
+          break
+        end
+
+        # In case the client requests some other format besides XML, JSON and */*,
+        # we deliberately don't set request.format. The framework will return an
+        # ActionController::UnknownFormat error to the client later on in this case.
+      end
+    end
+  end
+
   def authorize(realm = "Web Password", errormessage = "Couldn't authenticate you")
     # make the current_user object from any auth sources we have
     setup_user_auth
index bee5e8169a4709d05f5d36ead2c6924a03f2db6a..f419460ee77bc97762ad6c9bc230d0718cb88c9b 100644 (file)
@@ -185,6 +185,8 @@ class ApplicationController < ActionController::Base
 
   def api_call_handle_error
     yield
+  rescue ActionController::UnknownFormat
+    head :not_acceptable
   rescue ActiveRecord::RecordNotFound => e
     head :not_found
   rescue LibXML::XML::Error, ArgumentError => e
index 91533ece438f89fd9cf8440bfd5400d84ad9addf..447ee19df61050e83f229619db8968351498a62a 100644 (file)
@@ -12,6 +12,10 @@ module GeoRecord
     def to_s
       format("%.7f", self)
     end
+
+    def as_json(_)
+      format("%.7f", self).to_f
+    end
   end
 
   # This scaling factor is used to convert between the float lat/lon that is
diff --git a/app/views/api/_root_attributes.json.jbuilder b/app/views/api/_root_attributes.json.jbuilder
new file mode 100644 (file)
index 0000000..d8fbef9
--- /dev/null
@@ -0,0 +1,5 @@
+json.version Settings.api_version
+json.generator Settings.generator
+json.copyright Settings.copyright_owner
+json.attribution Settings.attribution_url
+json.license Settings.license_url
diff --git a/app/views/api/map/_bounds.json.jbuilder b/app/views/api/map/_bounds.json.jbuilder
new file mode 100644 (file)
index 0000000..16fdbeb
--- /dev/null
@@ -0,0 +1,6 @@
+json.bounds do
+  json.minlat GeoRecord::Coord.new(@bounds.min_lat)
+  json.minlon GeoRecord::Coord.new(@bounds.min_lon)
+  json.maxlat GeoRecord::Coord.new(@bounds.max_lat)
+  json.maxlon GeoRecord::Coord.new(@bounds.max_lon)
+end
diff --git a/app/views/api/map/index.json.jbuilder b/app/views/api/map/index.json.jbuilder
new file mode 100644 (file)
index 0000000..7cc983a
--- /dev/null
@@ -0,0 +1,9 @@
+json.partial! "root_attributes"
+
+json.partial! "bounds"
+
+all = @nodes + @ways + @relations
+
+json.elements(all) do |obj|
+  json.partial! obj
+end
diff --git a/app/views/api/nodes/_node.json.jbuilder b/app/views/api/nodes/_node.json.jbuilder
new file mode 100644 (file)
index 0000000..e48d5f1
--- /dev/null
@@ -0,0 +1,15 @@
+json.type "node"
+json.id node.id
+if node.visible
+  json.lat GeoRecord::Coord.new(node.lat)
+  json.lon GeoRecord::Coord.new(node.lon)
+end
+json.timestamp node.timestamp.xmlschema
+json.version node.version
+json.changeset node.changeset_id
+json.user node.changeset.user.display_name
+json.uid node.changeset.user_id
+
+json.visible node.visible unless node.visible
+
+json.tags node.tags unless node.tags.empty?
diff --git a/app/views/api/nodes/index.json.jbuilder b/app/views/api/nodes/index.json.jbuilder
new file mode 100644 (file)
index 0000000..3e3ceb4
--- /dev/null
@@ -0,0 +1,5 @@
+json.partial! "api/root_attributes"
+
+json.elements(@nodes) do |node|
+  json.partial! node
+end
diff --git a/app/views/api/nodes/show.json.jbuilder b/app/views/api/nodes/show.json.jbuilder
new file mode 100644 (file)
index 0000000..9974da8
--- /dev/null
@@ -0,0 +1,5 @@
+json.partial! "api/root_attributes"
+
+json.elements([@node]) do |node|
+  json.partial! node
+end
diff --git a/app/views/api/old_nodes/_old_node.json.jbuilder b/app/views/api/old_nodes/_old_node.json.jbuilder
new file mode 100644 (file)
index 0000000..211d503
--- /dev/null
@@ -0,0 +1,15 @@
+json.type "node"
+json.id old_node.node_id
+if old_node.visible
+  json.lat GeoRecord::Coord.new(old_node.lat)
+  json.lon GeoRecord::Coord.new(old_node.lon)
+end
+json.timestamp old_node.timestamp.xmlschema
+json.version old_node.version
+json.changeset old_node.changeset_id
+json.user old_node.changeset.user.display_name
+json.uid old_node.changeset.user_id
+
+json.visible old_node.visible unless old_node.visible
+
+json.tags old_node.tags unless old_node.tags.empty?
diff --git a/app/views/api/old_nodes/history.json.jbuilder b/app/views/api/old_nodes/history.json.jbuilder
new file mode 100644 (file)
index 0000000..96e8cca
--- /dev/null
@@ -0,0 +1,5 @@
+json.partial! "api/root_attributes"
+
+json.elements(@elems) do |old_node|
+  json.partial! old_node
+end
diff --git a/app/views/api/old_nodes/version.json.jbuilder b/app/views/api/old_nodes/version.json.jbuilder
new file mode 100644 (file)
index 0000000..f63e07d
--- /dev/null
@@ -0,0 +1,5 @@
+json.partial! "api/root_attributes"
+
+json.elements([@old_element]) do |old_node|
+  json.partial! old_node
+end
diff --git a/app/views/api/old_relations/_old_relation.json.jbuilder b/app/views/api/old_relations/_old_relation.json.jbuilder
new file mode 100644 (file)
index 0000000..c52ca20
--- /dev/null
@@ -0,0 +1,19 @@
+json.type "relation"
+json.id old_relation.relation_id
+json.timestamp old_relation.timestamp.xmlschema
+json.version old_relation.version
+json.changeset old_relation.changeset_id
+json.user old_relation.changeset.user.display_name
+json.uid old_relation.changeset.user_id
+
+json.visible old_relation.visible unless old_relation.visible
+
+unless old_relation.relation_members.empty?
+  json.members(old_relation.relation_members) do |m|
+    json.type m.member_type.downcase
+    json.ref m.member_id
+    json.role m.member_role
+  end
+end
+
+json.tags old_relation.tags unless old_relation.tags.empty?
diff --git a/app/views/api/old_relations/history.json.jbuilder b/app/views/api/old_relations/history.json.jbuilder
new file mode 100644 (file)
index 0000000..311a80a
--- /dev/null
@@ -0,0 +1,5 @@
+json.partial! "api/root_attributes"
+
+json.elements(@elems) do |old_relation|
+  json.partial! old_relation
+end
diff --git a/app/views/api/old_relations/version.json.jbuilder b/app/views/api/old_relations/version.json.jbuilder
new file mode 100644 (file)
index 0000000..5b33e4b
--- /dev/null
@@ -0,0 +1,5 @@
+json.partial! "api/root_attributes"
+
+json.elements([@old_element]) do |old_relation|
+  json.partial! old_relation
+end
diff --git a/app/views/api/old_ways/_old_way.json.jbuilder b/app/views/api/old_ways/_old_way.json.jbuilder
new file mode 100644 (file)
index 0000000..b2e79f8
--- /dev/null
@@ -0,0 +1,13 @@
+json.type "way"
+json.id old_way.way_id
+json.timestamp old_way.timestamp.xmlschema
+json.version old_way.version
+json.changeset old_way.changeset_id
+json.user old_way.changeset.user.display_name
+json.uid old_way.changeset.user_id
+
+json.visible old_way.visible unless old_way.visible
+
+json.nodes old_way.nds unless old_way.nds.empty?
+
+json.tags old_way.tags unless old_way.tags.empty?
diff --git a/app/views/api/old_ways/history.json.jbuilder b/app/views/api/old_ways/history.json.jbuilder
new file mode 100644 (file)
index 0000000..b5cf80d
--- /dev/null
@@ -0,0 +1,5 @@
+json.partial! "api/root_attributes"
+
+json.elements(@elems) do |old_way|
+  json.partial! old_way
+end
diff --git a/app/views/api/old_ways/version.json.jbuilder b/app/views/api/old_ways/version.json.jbuilder
new file mode 100644 (file)
index 0000000..c664885
--- /dev/null
@@ -0,0 +1,5 @@
+json.partial! "api/root_attributes"
+
+json.elements([@old_element]) do |old_way|
+  json.partial! old_way
+end
diff --git a/app/views/api/relations/_relation.json.jbuilder b/app/views/api/relations/_relation.json.jbuilder
new file mode 100644 (file)
index 0000000..52263ce
--- /dev/null
@@ -0,0 +1,19 @@
+json.type "relation"
+json.id relation.id
+json.timestamp relation.timestamp.xmlschema
+json.version relation.version
+json.changeset relation.changeset_id
+json.user relation.changeset.user.display_name
+json.uid relation.changeset.user_id
+
+json.visible relation.visible unless relation.visible
+
+unless relation.relation_members.empty?
+  json.members(relation.relation_members) do |m|
+    json.type m.member_type.downcase
+    json.ref m.member_id
+    json.role m.member_role
+  end
+end
+
+json.tags relation.tags unless relation.tags.empty?
diff --git a/app/views/api/relations/full.json.jbuilder b/app/views/api/relations/full.json.jbuilder
new file mode 100644 (file)
index 0000000..98cbbfc
--- /dev/null
@@ -0,0 +1,7 @@
+json.partial! "api/root_attributes"
+
+all = @nodes + @ways + @relations
+
+json.elements(all) do |obj|
+  json.partial! obj
+end
diff --git a/app/views/api/relations/index.json.jbuilder b/app/views/api/relations/index.json.jbuilder
new file mode 100644 (file)
index 0000000..f170cb1
--- /dev/null
@@ -0,0 +1,5 @@
+json.partial! "api/root_attributes"
+
+json.elements(@relations) do |relation|
+  json.partial! relation
+end
diff --git a/app/views/api/relations/relations_for_node.json.jbuilder b/app/views/api/relations/relations_for_node.json.jbuilder
new file mode 100644 (file)
index 0000000..f170cb1
--- /dev/null
@@ -0,0 +1,5 @@
+json.partial! "api/root_attributes"
+
+json.elements(@relations) do |relation|
+  json.partial! relation
+end
diff --git a/app/views/api/relations/relations_for_relation.json.jbuilder b/app/views/api/relations/relations_for_relation.json.jbuilder
new file mode 100644 (file)
index 0000000..f170cb1
--- /dev/null
@@ -0,0 +1,5 @@
+json.partial! "api/root_attributes"
+
+json.elements(@relations) do |relation|
+  json.partial! relation
+end
diff --git a/app/views/api/relations/relations_for_way.json.jbuilder b/app/views/api/relations/relations_for_way.json.jbuilder
new file mode 100644 (file)
index 0000000..f170cb1
--- /dev/null
@@ -0,0 +1,5 @@
+json.partial! "api/root_attributes"
+
+json.elements(@relations) do |relation|
+  json.partial! relation
+end
diff --git a/app/views/api/relations/show.json.jbuilder b/app/views/api/relations/show.json.jbuilder
new file mode 100644 (file)
index 0000000..7f85d0f
--- /dev/null
@@ -0,0 +1,5 @@
+json.partial! "api/root_attributes"
+
+json.elements([@relation]) do |relation|
+  json.partial! relation
+end
diff --git a/app/views/api/ways/_way.json.jbuilder b/app/views/api/ways/_way.json.jbuilder
new file mode 100644 (file)
index 0000000..11e796b
--- /dev/null
@@ -0,0 +1,13 @@
+json.type "way"
+json.id way.id
+json.timestamp way.timestamp.xmlschema
+json.version way.version
+json.changeset way.changeset_id
+json.user way.changeset.user.display_name
+json.uid way.changeset.user_id
+
+json.visible way.visible unless way.visible
+
+json.nodes way.nodes.ids unless way.nodes.ids.empty?
+
+json.tags way.tags unless way.tags.empty?
diff --git a/app/views/api/ways/full.json.jbuilder b/app/views/api/ways/full.json.jbuilder
new file mode 100644 (file)
index 0000000..bebad5e
--- /dev/null
@@ -0,0 +1,7 @@
+json.partial! "api/root_attributes"
+
+all = @nodes + [@way]
+
+json.elements(all) do |obj|
+  json.partial! obj
+end
diff --git a/app/views/api/ways/index.json.jbuilder b/app/views/api/ways/index.json.jbuilder
new file mode 100644 (file)
index 0000000..19e59cf
--- /dev/null
@@ -0,0 +1,5 @@
+json.partial! "api/root_attributes"
+
+json.elements(@ways) do |way|
+  json.partial! way
+end
diff --git a/app/views/api/ways/show.json.jbuilder b/app/views/api/ways/show.json.jbuilder
new file mode 100644 (file)
index 0000000..acb93c1
--- /dev/null
@@ -0,0 +1,5 @@
+json.partial! "api/root_attributes"
+
+json.elements([@way]) do |way|
+  json.partial! way
+end
diff --git a/app/views/api/ways/ways_for_node.json.jbuilder b/app/views/api/ways/ways_for_node.json.jbuilder
new file mode 100644 (file)
index 0000000..19e59cf
--- /dev/null
@@ -0,0 +1,5 @@
+json.partial! "api/root_attributes"
+
+json.elements(@ways) do |way|
+  json.partial! way
+end
index db83aaa9f32fe4a29fdf8f0de5dbf13757249923..9f14cc7fd8dbf8cd5512766a214105c2ce1a3ebb 100644 (file)
@@ -23,6 +23,87 @@ module Api
         { :path => "/api/0.6/map", :method => :get },
         { :controller => "api/map", :action => "index" }
       )
+      assert_routing(
+        { :path => "/api/0.6/map.json", :method => :get },
+        { :controller => "api/map", :action => "index", :format => "json" }
+      )
+    end
+
+    ##
+    # test http accept headers
+    def test_http_accept_header
+      node = create(:node)
+
+      minlon = node.lon - 0.1
+      minlat = node.lat - 0.1
+      maxlon = node.lon + 0.1
+      maxlat = node.lat + 0.1
+      bbox = "#{minlon},#{minlat},#{maxlon},#{maxlat}"
+
+      # Accept: XML format -> use XML
+      http_accept_format("text/xml")
+      get :index, :params => { :bbox => bbox }
+      assert_response :success, "Expected success with the map call"
+      assert_equal "application/xml; charset=utf-8", @response.header["Content-Type"]
+
+      # Accept: Any format -> use XML
+      http_accept_format("*/*")
+      get :index, :params => { :bbox => bbox }
+      assert_response :success, "Expected success with the map call"
+      assert_equal "application/xml; charset=utf-8", @response.header["Content-Type"]
+
+      # Accept: Any format, .json URL suffix -> use json
+      http_accept_format("*/*")
+      get :index, :params => { :bbox => bbox, :format => "json" }
+      assert_response :success, "Expected success with the map call"
+      assert_equal "application/json; charset=utf-8", @response.header["Content-Type"]
+
+      # Accept: Firefox header -> use XML
+      http_accept_format("text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8")
+      get :index, :params => { :bbox => bbox }
+      assert_response :success, "Expected success with the map call"
+      assert_equal "application/xml; charset=utf-8", @response.header["Content-Type"]
+
+      # Accept: JOSM header text/html, image/gif, image/jpeg, *; q=.2, */*; q=.2 -> use XML
+      # Note: JOSM's header does not comply with RFC 7231, section 5.3.1
+      http_accept_format("text/html, image/gif, image/jpeg, *; q=.2, */*; q=.2")
+      get :index, :params => { :bbox => bbox }
+      assert_response :success, "Expected success with the map call"
+      assert_equal "application/xml; charset=utf-8", @response.header["Content-Type"]
+
+      # Accept: text/plain, */* -> use XML
+      http_accept_format("text/plain, */*")
+      get :index, :params => { :bbox => bbox }
+      assert_response :success, "Expected success with the map call"
+      assert_equal "application/xml; charset=utf-8", @response.header["Content-Type"]
+
+      # Accept: text/* -> use XML
+      http_accept_format("text/*")
+      get :index, :params => { :bbox => bbox }
+      assert_response :success, "Expected success with the map call"
+      assert_equal "application/xml; charset=utf-8", @response.header["Content-Type"]
+
+      # Accept: json, */* format -> use json
+      http_accept_format("application/json, */*")
+      get :index, :params => { :bbox => bbox }
+      assert_response :success, "Expected success with the map call"
+      assert_equal "application/json; charset=utf-8", @response.header["Content-Type"]
+
+      # Accept: json format -> use json
+      http_accept_format("application/json")
+      get :index, :params => { :bbox => bbox }
+      assert_response :success, "Expected success with the map call"
+      assert_equal "application/json; charset=utf-8", @response.header["Content-Type"]
+
+      # text/json is in invalid format, return HTTP 406 Not acceptable
+      http_accept_format("text/json")
+      get :index, :params => { :bbox => bbox }
+      assert_response :not_acceptable, "text/json should fail"
+
+      # image/jpeg is a format which we don't support, return HTTP 406 Not acceptable
+      http_accept_format("image/jpeg")
+      get :index, :params => { :bbox => bbox }
+      assert_response :not_acceptable, "text/json should fail"
     end
 
     # -------------------------------------
@@ -63,6 +144,54 @@ module Api
       end
     end
 
+    def test_map_json
+      node = create(:node, :lat => 7, :lon => 7)
+      tag = create(:node_tag, :node => node)
+      way1 = create(:way_node, :node => node).way
+      way2 = create(:way_node, :node => node).way
+      relation = create(:relation_member, :member => node).relation
+
+      # Need to split the min/max lat/lon out into their own variables here
+      # so that we can test they are returned later.
+      minlon = node.lon - 0.1
+      minlat = node.lat - 0.1
+      maxlon = node.lon + 0.1
+      maxlat = node.lat + 0.1
+      bbox = "#{minlon},#{minlat},#{maxlon},#{maxlat}"
+      get :index, :params => { :bbox => bbox, :format => "json" }
+      if $VERBOSE
+        print @request.to_yaml
+        print @response.body
+      end
+      assert_response :success, "Expected success with the map call"
+      js = ActiveSupport::JSON.decode(@response.body)
+      assert_not_nil js
+
+      assert_equal Settings.api_version, js["version"]
+      assert_equal Settings.generator, js["generator"]
+      assert_equal GeoRecord::Coord.new(minlon), js["bounds"]["minlon"]
+      assert_equal GeoRecord::Coord.new(minlat), js["bounds"]["minlat"]
+      assert_equal GeoRecord::Coord.new(maxlon), js["bounds"]["maxlon"]
+      assert_equal GeoRecord::Coord.new(maxlat), js["bounds"]["maxlat"]
+
+      result_nodes = js["elements"].select { |a| a["type"] == "node" }
+                                   .select { |a| a["id"] == node.id }
+                                   .select { |a| a["lat"] == GeoRecord::Coord.new(node.lat) }
+                                   .select { |a| a["lon"] == GeoRecord::Coord.new(node.lon) }
+                                   .select { |a| a["version"] == node.version }
+                                   .select { |a| a["changeset"] == node.changeset_id }
+                                   .select { |a| a["timestamp"] == node.timestamp.xmlschema }
+      assert_equal result_nodes.count, 1
+      result_node = result_nodes.first
+
+      assert_equal result_node["tags"], tag.k => tag.v
+      assert_equal 2, (js["elements"].count { |a| a["type"] == "way" })
+      assert_equal 1, (js["elements"].count { |a| a["type"] == "way" && a["id"] == way1.id })
+      assert_equal 1, (js["elements"].count { |a| a["type"] == "way" && a["id"] == way2.id })
+      assert_equal 1, (js["elements"].count { |a| a["type"] == "relation" })
+      assert_equal 1, (js["elements"].count { |a| a["type"] == "relation" && a["id"] == relation.id })
+    end
+
     # This differs from the above test in that we are making the bbox exactly
     # the same as the node we are looking at
     def test_map_inclusive
index aee41d51548e788bc6d6f389750f891025f8510f..e2174a12603c2400c2a248c3d407bf34ef938430 100644 (file)
@@ -13,6 +13,10 @@ module Api
         { :path => "/api/0.6/node/1", :method => :get },
         { :controller => "api/nodes", :action => "show", :id => "1" }
       )
+      assert_routing(
+        { :path => "/api/0.6/node/1.json", :method => :get },
+        { :controller => "api/nodes", :action => "show", :id => "1", :format => "json" }
+      )
       assert_routing(
         { :path => "/api/0.6/node/1", :method => :put },
         { :controller => "api/nodes", :action => "update", :id => "1" }
@@ -25,6 +29,10 @@ module Api
         { :path => "/api/0.6/nodes", :method => :get },
         { :controller => "api/nodes", :action => "index" }
       )
+      assert_routing(
+        { :path => "/api/0.6/nodes.json", :method => :get },
+        { :controller => "api/nodes", :action => "index", :format => "json" }
+      )
     end
 
     def test_create
@@ -464,6 +472,19 @@ module Api
         assert_select "node[id='#{node5.id}'][visible='false']", :count => 1
       end
 
+      # test a working call with json format
+      get :index, :params => { :nodes => "#{node1.id},#{node2.id},#{node3.id},#{node4.id},#{node5.id}", :format => "json" }
+
+      js = ActiveSupport::JSON.decode(@response.body)
+      assert_not_nil js
+      assert_equal 5, js["elements"].count
+      assert_equal 5, (js["elements"].count { |a| a["type"] == "node" })
+      assert_equal 1, (js["elements"].count { |a| a["id"] == node1.id && a["visible"].nil? })
+      assert_equal 1, (js["elements"].count { |a| a["id"] == node2.id && a["visible"] == false })
+      assert_equal 1, (js["elements"].count { |a| a["id"] == node3.id && a["visible"].nil? })
+      assert_equal 1, (js["elements"].count { |a| a["id"] == node4.id && a["visible"].nil? })
+      assert_equal 1, (js["elements"].count { |a| a["id"] == node5.id && a["visible"] == false })
+
       # check error when a non-existent node is included
       get :index, :params => { :nodes => "#{node1.id},#{node2.id},#{node3.id},#{node4.id},#{node5.id},0" }
       assert_response :not_found
index 941787d0a2872ddd39d31693cadfb2f40c67c580..76e78ffadaa47b8df30cfe495d40f34d2429e57c 100644 (file)
@@ -17,6 +17,14 @@ module Api
         { :path => "/api/0.6/node/1/2", :method => :get },
         { :controller => "api/old_nodes", :action => "version", :id => "1", :version => "2" }
       )
+      assert_routing(
+        { :path => "/api/0.6/node/1/history.json", :method => :get },
+        { :controller => "api/old_nodes", :action => "history", :id => "1", :format => "json" }
+      )
+      assert_routing(
+        { :path => "/api/0.6/node/1/2.json", :method => :get },
+        { :controller => "api/old_nodes", :action => "version", :id => "1", :version => "2", :format => "json" }
+      )
       assert_routing(
         { :path => "/api/0.6/node/1/2/redact", :method => :post },
         { :controller => "api/old_nodes", :action => "redact", :id => "1", :version => "2" }
index 40a80248c3f995aa5a1e36a2f963f72590cea51f..19e8ae357ef10d7ac2691be0ec0b4ff21f4c93e9 100644 (file)
@@ -13,6 +13,14 @@ module Api
         { :path => "/api/0.6/relation/1/2", :method => :get },
         { :controller => "api/old_relations", :action => "version", :id => "1", :version => "2" }
       )
+      assert_routing(
+        { :path => "/api/0.6/relation/1/history.json", :method => :get },
+        { :controller => "api/old_relations", :action => "history", :id => "1", :format => "json" }
+      )
+      assert_routing(
+        { :path => "/api/0.6/relation/1/2.json", :method => :get },
+        { :controller => "api/old_relations", :action => "version", :id => "1", :version => "2", :format => "json" }
+      )
       assert_routing(
         { :path => "/api/0.6/relation/1/2/redact", :method => :post },
         { :controller => "api/old_relations", :action => "redact", :id => "1", :version => "2" }
index 73e968f3095c11f569fc8016b453407cc683d336..cd676a960f945fa99801b9070b6b505c38f13c46 100644 (file)
@@ -13,6 +13,14 @@ module Api
         { :path => "/api/0.6/way/1/2", :method => :get },
         { :controller => "api/old_ways", :action => "version", :id => "1", :version => "2" }
       )
+      assert_routing(
+        { :path => "/api/0.6/way/1/history.json", :method => :get },
+        { :controller => "api/old_ways", :action => "history", :id => "1", :format => "json" }
+      )
+      assert_routing(
+        { :path => "/api/0.6/way/1/2.json", :method => :get },
+        { :controller => "api/old_ways", :action => "version", :id => "1", :version => "2", :format => "json" }
+      )
       assert_routing(
         { :path => "/api/0.6/way/1/2/redact", :method => :post },
         { :controller => "api/old_ways", :action => "redact", :id => "1", :version => "2" }
index b7c17a176485f3a448539e16cc23a3898b7c13a4..da18099bda33d5aa10b05a6b569b9da255c5de09 100644 (file)
@@ -13,10 +13,18 @@ module Api
         { :path => "/api/0.6/relation/1/full", :method => :get },
         { :controller => "api/relations", :action => "full", :id => "1" }
       )
+      assert_routing(
+        { :path => "/api/0.6/relation/1/full.json", :method => :get },
+        { :controller => "api/relations", :action => "full", :id => "1", :format => "json" }
+      )
       assert_routing(
         { :path => "/api/0.6/relation/1", :method => :get },
         { :controller => "api/relations", :action => "show", :id => "1" }
       )
+      assert_routing(
+        { :path => "/api/0.6/relation/1.json", :method => :get },
+        { :controller => "api/relations", :action => "show", :id => "1", :format => "json" }
+      )
       assert_routing(
         { :path => "/api/0.6/relation/1", :method => :put },
         { :controller => "api/relations", :action => "update", :id => "1" }
@@ -29,6 +37,10 @@ module Api
         { :path => "/api/0.6/relations", :method => :get },
         { :controller => "api/relations", :action => "index" }
       )
+      assert_routing(
+        { :path => "/api/0.6/relations.json", :method => :get },
+        { :controller => "api/relations", :action => "index", :format => "json" }
+      )
 
       assert_routing(
         { :path => "/api/0.6/node/1/relations", :method => :get },
@@ -42,6 +54,18 @@ module Api
         { :path => "/api/0.6/relation/1/relations", :method => :get },
         { :controller => "api/relations", :action => "relations_for_relation", :id => "1" }
       )
+      assert_routing(
+        { :path => "/api/0.6/node/1/relations.json", :method => :get },
+        { :controller => "api/relations", :action => "relations_for_node", :id => "1", :format => "json" }
+      )
+      assert_routing(
+        { :path => "/api/0.6/way/1/relations.json", :method => :get },
+        { :controller => "api/relations", :action => "relations_for_way", :id => "1", :format => "json" }
+      )
+      assert_routing(
+        { :path => "/api/0.6/relation/1/relations.json", :method => :get },
+        { :controller => "api/relations", :action => "relations_for_relation", :id => "1", :format => "json" }
+      )
     end
 
     # -------------------------------------
@@ -187,6 +211,18 @@ module Api
         assert_select "relation[id='#{relation4.id}'][visible='true']", :count => 1
       end
 
+      # test a working call with json format
+      get :index, :params => { :relations => "#{relation1.id},#{relation2.id},#{relation3.id},#{relation4.id}", :format => "json" }
+
+      js = ActiveSupport::JSON.decode(@response.body)
+      assert_not_nil js
+      assert_equal 4, js["elements"].count
+      assert_equal 4, (js["elements"].count { |a| a["type"] == "relation" })
+      assert_equal 1, (js["elements"].count { |a| a["id"] == relation1.id && a["visible"].nil? })
+      assert_equal 1, (js["elements"].count { |a| a["id"] == relation2.id && a["visible"] == false })
+      assert_equal 1, (js["elements"].count { |a| a["id"] == relation3.id && a["visible"].nil? })
+      assert_equal 1, (js["elements"].count { |a| a["id"] == relation4.id && a["visible"].nil? })
+
       # check error when a non-existent relation is included
       get :index, :params => { :relations => "#{relation1.id},#{relation2.id},#{relation3.id},#{relation4.id},0" }
       assert_response :not_found
index 433b43f27cbea7d63e10306f03573ab54dfe31cf..e4b9f36632952ac32839296dd5ff90fe5135a157 100644 (file)
@@ -13,10 +13,18 @@ module Api
         { :path => "/api/0.6/way/1/full", :method => :get },
         { :controller => "api/ways", :action => "full", :id => "1" }
       )
+      assert_routing(
+        { :path => "/api/0.6/way/1/full.json", :method => :get },
+        { :controller => "api/ways", :action => "full", :id => "1", :format => "json" }
+      )
       assert_routing(
         { :path => "/api/0.6/way/1", :method => :get },
         { :controller => "api/ways", :action => "show", :id => "1" }
       )
+      assert_routing(
+        { :path => "/api/0.6/way/1.json", :method => :get },
+        { :controller => "api/ways", :action => "show", :id => "1", :format => "json" }
+      )
       assert_routing(
         { :path => "/api/0.6/way/1", :method => :put },
         { :controller => "api/ways", :action => "update", :id => "1" }
@@ -29,6 +37,10 @@ module Api
         { :path => "/api/0.6/ways", :method => :get },
         { :controller => "api/ways", :action => "index" }
       )
+      assert_routing(
+        { :path => "/api/0.6/ways.json", :method => :get },
+        { :controller => "api/ways", :action => "index", :format => "json" }
+      )
     end
 
     # -------------------------------------
@@ -104,6 +116,18 @@ module Api
         assert_select "way[id='#{way4.id}'][visible='true']", :count => 1
       end
 
+      # test a working call with json format
+      get :index, :params => { :ways => "#{way1.id},#{way2.id},#{way3.id},#{way4.id}", :format => "json" }
+
+      js = ActiveSupport::JSON.decode(@response.body)
+      assert_not_nil js
+      assert_equal 4, js["elements"].count
+      assert_equal 4, (js["elements"].count { |a| a["type"] == "way" })
+      assert_equal 1, (js["elements"].count { |a| a["id"] == way1.id && a["visible"].nil? })
+      assert_equal 1, (js["elements"].count { |a| a["id"] == way2.id && a["visible"] == false })
+      assert_equal 1, (js["elements"].count { |a| a["id"] == way3.id && a["visible"].nil? })
+      assert_equal 1, (js["elements"].count { |a| a["id"] == way4.id && a["visible"].nil? })
+
       # check error when a non-existent way is included
       get :index, :params => { :ways => "#{way1.id},#{way2.id},#{way3.id},#{way4.id},0" }
       assert_response :not_found
index 378d49bb9a02f43097cfe13c9b73c32c702e1aac..08f01e8e7d488c21c2b9493ca74434b5a82516ee 100644 (file)
@@ -115,6 +115,12 @@ module ActiveSupport
       @request.env["HTTP_AUTHORIZATION"] = format("Basic %{auth}", :auth => Base64.encode64("#{user}:#{pass}"))
     end
 
+    ##
+    # set request header for HTTP Accept
+    def http_accept_format(format)
+      @request.env["HTTP_ACCEPT"] = format
+    end
+
     ##
     # set request readers to ask for a particular error format
     def error_format(format)