Skip to content

OpenProject is the leading open source project management software for product, project and portfolio management. A powerful Jira alternative with agile planning, issue tracking, roadmaps, Gantt charts, time tracking, collaboration features, and more. Available on premises or in the cloud. ⭐ Star us on GitHub

Replace JSON converter in API with raw json rendering

Oliver Günther3df3a9676379
diff --git a/modules/bcf/app/controllers/bcf/api/v2_1/viewpoints/api.rb b/modules/bcf/app/controllers/bcf/api/v2_1/viewpoints/api.rb
index f4e4fd118cd..d08a67ec391 100644
--- a/modules/bcf/app/controllers/bcf/api/v2_1/viewpoints/api.rb
+++ b/modules/bcf/app/controllers/bcf/api/v2_1/viewpoints/api.rb
@@ -32,32 +32,52 @@ module Bcf::API::V2_1
module Viewpoints
class API < ::API::OpenProjectAPI
resources :viewpoints do
- get &::Bcf::API::V2_1::Endpoints::Index
- .new(model: Bcf::Viewpoint,
- api_name: 'Viewpoints',
- scope: -> { @issue.viewpoints })
- .mount
+ get do
+ @issue.viewpoints
+ .select(:json_viewpoint)
+ .map(&:json_viewpoint)
+ end
route_param :viewpoint_uuid, regexp: /\A[a-f0-9\-]+\z/ do
- after_validation do
- @viewpoint = @issue.viewpoints.find_by!(uuid: params[:viewpoint_uuid])
+
+ helpers do
+
+ ##
+ # Extract raw json from database
+ # in an optional subpath
+ def find_json!(uuid, slice: nil)
+ query = @issue.viewpoints
+ .where(uuid: uuid)
+
+ query =
+ if slice
+ query.select("json_viewpoint #> '{#{slice.join(',')}}' as json_viewpoint")
+ else
+ query.select(:json_viewpoint)
+ end
+
+ row = query.first
+ raise ActiveRecord::RecordNotFound unless row
+
+ # If we access row.json, Rails is going to cast the json to ruby hash for us
+ row.raw_json_viewpoint
+ end
end
- get &::Bcf::API::V2_1::Endpoints::Show
- .new(model: Bcf::Viewpoint,
- api_name: 'Viewpoints')
- .mount
+ get do
+ find_json!(params[:viewpoint_uuid])
+ end
get :selection do
- SlicedRepresenter.new(@viewpoint, slice: %w[components selection])
+ find_json!(params[:viewpoint_uuid], slice: %w[components selection])
end
get :coloring do
- SlicedRepresenter.new(@viewpoint, slice: %w[components coloring])
+ find_json!(params[:viewpoint_uuid], slice: %w[components coloring])
end
get :visibility do
- SlicedRepresenter.new(@viewpoint, slice: %w[components visibility])
+ find_json!(params[:viewpoint_uuid], slice: %w[components visibility])
end
get :bitmaps do
@@ -68,7 +88,8 @@ module Bcf::API::V2_1
helpers ::API::Helpers::AttachmentRenderer
get do
- if snapshot = @viewpoint.snapshot
+ viewpoint = @issue.viewpoints.find_by!(uuid: params[:viewpoint_uuid])
+ if snapshot = viewpoint.snapshot
respond_with_attachment snapshot
else
raise ActiveRecord::RecordNotFound
diff --git a/modules/bcf/app/models/bcf/viewpoint.rb b/modules/bcf/app/models/bcf/viewpoint.rb
index 0267232b734..e5d6426deb9 100644
--- a/modules/bcf/app/models/bcf/viewpoint.rb
+++ b/modules/bcf/app/models/bcf/viewpoint.rb
@@ -15,6 +15,10 @@ module Bcf
has_many :comments, foreign_key: :viewpoint_id, class_name: "Bcf::Comment"
delegate :project, :project_id, to: :issue, allow_nil: true
+ def raw_json_viewpoint
+ attributes_before_type_cast['json_viewpoint']
+ end
+
def snapshot
attachments.find_by_description('snapshot')
end
diff --git a/modules/bcf/app/representers/bcf/api/v2_1/viewpoints/single_representer.rb b/modules/bcf/app/representers/bcf/api/v2_1/viewpoints/single_representer.rb
deleted file mode 100644
index 50263e1043d..00000000000
--- a/modules/bcf/app/representers/bcf/api/v2_1/viewpoints/single_representer.rb
+++ /dev/null
@@ -1,47 +0,0 @@
-#-- encoding: UTF-8
-
-#-- copyright
-# OpenProject is a project management system.
-# Copyright (C) 2012-2018 the OpenProject Foundation (OPF)
-#
-# This program is free software; you can redistribute it and/or
-# modify it under the terms of the GNU General Public License version 3.
-#
-# OpenProject is a fork of ChiliProject, which is a fork of Redmine. The copyright follows:
-# Copyright (C) 2006-2017 Jean-Philippe Lang
-# Copyright (C) 2010-2013 the ChiliProject Team
-#
-# This program is free software; you can redistribute it and/or
-# modify it under the terms of the GNU General Public License
-# as published by the Free Software Foundation; either version 2
-# of the License, or (at your option) any later version.
-#
-# This program is distributed in the hope that it will be useful,
-# but WITHOUT ANY WARRANTY; without even the implied warranty of
-# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
-# GNU General Public License for more details.
-#
-# You should have received a copy of the GNU General Public License
-# along with this program; if not, write to the Free Software
-# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
-#
-# See docs/COPYRIGHT.rdoc for more details.
-#++
-
-module Bcf::API::V2_1
- module Viewpoints
- class SingleRepresenter
- attr_reader :represented
-
- def initialize(represented)
- @represented = represented
- end
-
- def to_hash
- # Uses mapper to avoid saving JSON for now.
- # We will likely want a migration to move away from XML and render viewpoint JSON directly
- ::OpenProject::Bcf::BcfJson::ViewpointMapper.new(represented).result
- end
- end
- end
-end
diff --git a/modules/bcf/app/representers/bcf/api/v2_1/viewpoints/sliced_representer.rb b/modules/bcf/app/representers/bcf/api/v2_1/viewpoints/sliced_representer.rb
deleted file mode 100644
index 17c356827a5..00000000000
--- a/modules/bcf/app/representers/bcf/api/v2_1/viewpoints/sliced_representer.rb
+++ /dev/null
@@ -1,49 +0,0 @@
-#-- encoding: UTF-8
-
-#-- copyright
-# OpenProject is a project management system.
-# Copyright (C) 2012-2018 the OpenProject Foundation (OPF)
-#
-# This program is free software; you can redistribute it and/or
-# modify it under the terms of the GNU General Public License version 3.
-#
-# OpenProject is a fork of ChiliProject, which is a fork of Redmine. The copyright follows:
-# Copyright (C) 2006-2017 Jean-Philippe Lang
-# Copyright (C) 2010-2013 the ChiliProject Team
-#
-# This program is free software; you can redistribute it and/or
-# modify it under the terms of the GNU General Public License
-# as published by the Free Software Foundation; either version 2
-# of the License, or (at your option) any later version.
-#
-# This program is distributed in the hope that it will be useful,
-# but WITHOUT ANY WARRANTY; without even the implied warranty of
-# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
-# GNU General Public License for more details.
-#
-# You should have received a copy of the GNU General Public License
-# along with this program; if not, write to the Free Software
-# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
-#
-# See docs/COPYRIGHT.rdoc for more details.
-#++
-
-module Bcf::API::V2_1
- module Viewpoints
- class SlicedRepresenter < SingleRepresenter
- attr_reader :slice, :key
-
- def initialize(represented, slice:, key: nil)
- super(represented)
- @key = key || slice.last
- @slice = slice
- end
-
- def to_hash
- {
- key => super.dig(*slice)
- }
- end
- end
- end
-end
diff --git a/modules/bcf/db/migrate/20191121140202_migrate_xml_viewpoint_to_json.rb b/modules/bcf/db/migrate/20191121140202_migrate_xml_viewpoint_to_json.rb
new file mode 100644
index 00000000000..296547b5713
--- /dev/null
+++ b/modules/bcf/db/migrate/20191121140202_migrate_xml_viewpoint_to_json.rb
@@ -0,0 +1,30 @@
+class MigrateXmlViewpointToJson < ActiveRecord::Migration[6.0]
+ def up
+ # Add JSON viewpoint
+ change_table :bcf_viewpoints do |t|
+ t.jsonb :json_viewpoint
+ end
+
+ # Convert viewpoints
+ ::Bcf::Viewpoint.reset_column_information
+ ::Bcf::Viewpoint.find_each do |resource|
+ mapper = ::OpenProject::Bcf::BcfJson::ViewpointMapper
+ .new(resource.uuid, resource.viewpoint)
+
+ resource.update_column(:json_viewpoint, mapper.result)
+
+ Rails.logger.debug { "Converted viewpoint (##{resource.id}) #{resource.uuid} to JSON." }
+ rescue => e
+ warn "Failed to convert viewpoint #{viewpoint.uuid}: #{e} #{e.message}"
+ end
+
+ # Remove the old XML viewpoint
+ change_table :bcf_viewpoints do |t|
+ t.remove :viewpoint
+ end
+ end
+
+ def down
+ raise "Cannot be reverted yet!"
+ end
+end
diff --git a/modules/bcf/lib/open_project/bcf/bcf_json/viewpoint_mapper.rb b/modules/bcf/lib/open_project/bcf/bcf_json/viewpoint_mapper.rb
index c44fbb339b3..d7198f62269 100644
--- a/modules/bcf/lib/open_project/bcf/bcf_json/viewpoint_mapper.rb
+++ b/modules/bcf/lib/open_project/bcf/bcf_json/viewpoint_mapper.rb
@@ -3,10 +3,11 @@ module OpenProject::Bcf
class ViewpointMapper
ROOT_NODE ||= 'VisualizationInfo'.freeze
- attr_reader :resource
+ attr_reader :uuid, :xml
- def initialize(viewpoint)
- @resource = viewpoint
+ def initialize(uuid, xml)
+ @uuid = uuid
+ @xml = xml
end
def result
@@ -24,7 +25,7 @@ module OpenProject::Bcf
def viewpoint_hash
@viewpoint_hash ||= begin
# Load from XML using activesupport
- hash = Hash.from_xml(resource.viewpoint)
+ hash = Hash.from_xml(xml)
hash = hash[ROOT_NODE] if hash.key?(ROOT_NODE)
# Perform destructive transformations
@@ -57,7 +58,7 @@ module OpenProject::Bcf
end
def set_uuid(hash)
- hash['guid'] = resource.uuid
+ hash['guid'] = uuid
end
def transform_keys(hash)
@@ -111,6 +112,9 @@ module OpenProject::Bcf
selections = hash.dig('components', 'selection', 'component')
return unless selections
+ # Ensure selections are an array
+ selections = Array.wrap(selections)
+
# Skip any components that have no guid
selections.select! { |item| item['ifc_guid'] }
@@ -124,7 +128,7 @@ module OpenProject::Bcf
return unless colors
# avoid Array(colors) since that deconstructs the array
- colors = [colors] unless colors.is_a?(Array)
+ colors = Array.wrap(colors)
hash['components']['coloring'] = colors.map do |entry|
# Prepend hash for hex color
diff --git a/modules/bcf/lib/open_project/bcf/bcf_xml/issue_reader.rb b/modules/bcf/lib/open_project/bcf/bcf_xml/issue_reader.rb
index d7cf56f71b6..3090d3112b8 100644
--- a/modules/bcf/lib/open_project/bcf/bcf_xml/issue_reader.rb
+++ b/modules/bcf/lib/open_project/bcf/bcf_xml/issue_reader.rb
@@ -192,8 +192,8 @@ module OpenProject::Bcf::BcfXml
issue: issue,
uuid: vp[:uuid],
- # Save the viewpoint as XML
- viewpoint: read_entry(vp[:viewpoint]),
+ # Save the viewpoint as json
+ viewpoint: viewpoint_as_json(vp[:uuid], vp[:viewpoint]),
viewpoint_name: vp[:viewpoint],
# Save the snapshot as file attachment
@@ -235,6 +235,14 @@ module OpenProject::Bcf::BcfXml
file_entry.get_input_stream.read
end
+ ##
+ # Map the xml viewpoint as json
+ def viewpoint_as_json(uuid, xml)
+ ::OpenProject::Bcf::BcfJson::ViewpointMapper
+ .new(uuid, xml)
+ .to_json
+ end
+
def new_comment(comment_data)
bcf_comment = issue.comments.build(uuid: comment_data[:uuid], viewpoint: viewpoint_by_uuid(comment_data[:viewpoint_uuid]))
diff --git a/modules/bcf/spec/factories/xml_viewpoint_factory.rb b/modules/bcf/spec/factories/xml_viewpoint_factory.rb
new file mode 100644
index 00000000000..4168d45d98b
--- /dev/null
+++ b/modules/bcf/spec/factories/xml_viewpoint_factory.rb
@@ -0,0 +1,49 @@
+#-- copyright
+# OpenProject Backlogs Plugin
+#
+# Copyright (C)2013-2014 the OpenProject Foundation (OPF)
+# Copyright (C)2011 Stephan Eckardt, Tim Felgentreff, Marnen Laibow-Koser, Sandro Munda
+# Copyright (C)2010-2011 friflaj
+# Copyright (C)2010 Maxime Guilbot, Andrew Vit, Joakim Kolsjö, ibussieres, Daniel Passos, Jason Vasquez, jpic, Emiliano Heyns
+# Copyright (C)2009-2010 Mark Maglana
+# Copyright (C)2009 Joe Heck, Nate Lowrie
+#
+# This program is free software; you can redistribute it and/or modify it under
+# the terms of the GNU General Public License version 3.
+#
+# OpenProject Backlogs is a derivative work based on ChiliProject Backlogs.
+# The copyright follows:
+# Copyright (C) 2010-2011 - Emiliano Heyns, Mark Maglana, friflaj
+# Copyright (C) 2011 - Jens Ulferts, Gregor Schmidt - Finn GmbH - Berlin, Germany
+#
+# This program is free software; you can redistribute it and/or
+# modify it under the terms of the GNU General Public License
+# as published by the Free Software Foundation; either version 2
+# of the License, or (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+#
+# See doc/COPYRIGHT.rdoc for more details.
+#++
+
+FactoryBot.define do
+ factory :xml_viewpoint, class: OpenStruct do
+ uuid { SecureRandom.uuid }
+ viewpoint_name { "full_viewpoint.bcfv" }
+ viewpoint do
+ file = OpenProject::Bcf::Engine.root.join("spec/fixtures/viewpoints/#{viewpoint_name}.xml")
+ if file.readable?
+ file.read
+ else
+ warn "Viewpoint name #{viewpoint_name} doesnt map to a viewpoint fixture"
+ end
+ end
+ end
+end
diff --git a/modules/bcf/spec/lib/open_project/bcf/bcf_json/viewpoint_mapper_shared_examples.rb b/modules/bcf/spec/lib/open_project/bcf/bcf_json/viewpoint_mapper_shared_examples.rb
index f0ddc575e2e..1620b69c3fc 100644
--- a/modules/bcf/spec/lib/open_project/bcf/bcf_json/viewpoint_mapper_shared_examples.rb
+++ b/modules/bcf/spec/lib/open_project/bcf/bcf_json/viewpoint_mapper_shared_examples.rb
@@ -37,7 +37,7 @@ shared_examples 'viewpoint keys' do
it 'always has the guid set to the viewpoint' do
expect(subject.keys).to include 'guid'
- expect(subject['guid']).to eq(viewpoint.uuid)
+ expect(subject['guid']).to eq(xml_viewpoint.uuid)
end
it 'always has the perspective_camera OR orthogonal camera set' do
@@ -149,13 +149,13 @@ end
shared_examples 'matches the JSON counterpart' do
it 'matches the JSON viewpoint counterpart' do
- path = OpenProject::Bcf::Engine.root.join("spec/fixtures/viewpoints/#{viewpoint.viewpoint_name}.json")
+ path = OpenProject::Bcf::Engine.root.join("spec/fixtures/viewpoints/#{xml_viewpoint.viewpoint_name}.json")
raise "Expected #{path} to be readable for JSON comparsion" unless path.readable?
json = path.read
# Replace the static GUID with the one from this viewpoint
- json.gsub! '{{UUID}}', viewpoint.uuid
+ json.gsub! '{{UUID}}', xml_viewpoint.uuid
expect(subject.to_json).to be_json_eql(json)
end
diff --git a/modules/bcf/spec/lib/open_project/bcf/bcf_json/viewpoint_mapper_spec.rb b/modules/bcf/spec/lib/open_project/bcf/bcf_json/viewpoint_mapper_spec.rb
index e792ed0332d..efc9290f510 100644
--- a/modules/bcf/spec/lib/open_project/bcf/bcf_json/viewpoint_mapper_spec.rb
+++ b/modules/bcf/spec/lib/open_project/bcf/bcf_json/viewpoint_mapper_spec.rb
@@ -30,12 +30,12 @@ require 'spec_helper'
require_relative './viewpoint_mapper_shared_examples.rb'
describe OpenProject::Bcf::BcfJson::ViewpointMapper do
- let(:instance) { described_class.new viewpoint }
+ let(:instance) { described_class.new xml_viewpoint.uuid, xml_viewpoint.viewpoint }
subject { instance.result }
describe 'with minimal example' do
- let_it_be(:viewpoint) do
- FactoryBot.build_stubbed :bcf_viewpoint, viewpoint_name: 'minimal.bcfv'
+ let_it_be(:xml_viewpoint) do
+ FactoryBot.build_stubbed :xml_viewpoint, viewpoint_name: 'minimal.bcfv'
end
it_behaves_like 'viewpoint keys'
@@ -45,8 +45,8 @@ describe OpenProject::Bcf::BcfJson::ViewpointMapper do
end
describe 'with full viewpoint' do
- let_it_be(:viewpoint) do
- FactoryBot.build_stubbed :bcf_viewpoint, viewpoint_name: 'full_viewpoint.bcfv'
+ let_it_be(:xml_viewpoint) do
+ FactoryBot.build_stubbed :xml_viewpoint, viewpoint_name: 'full_viewpoint.bcfv'
end
it_behaves_like 'viewpoint keys'
@@ -61,8 +61,8 @@ describe OpenProject::Bcf::BcfJson::ViewpointMapper do
end
describe 'with real-world neuhaus_sc_1 example' do
- let_it_be(:viewpoint) do
- FactoryBot.build_stubbed :bcf_viewpoint, viewpoint_name: 'neubau_sc_1.bcfv'
+ let_it_be(:xml_viewpoint) do
+ FactoryBot.build_stubbed :xml_viewpoint, viewpoint_name: 'neubau_sc_1.bcfv'
end
it_behaves_like 'viewpoint keys'