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
Ruby agile-developmentangularbcfboardsbug-trackerganttgantt-chartifcissue-trackerjira-alternativekanbanopenprojectproduct-managementproject-managementproject-planningroadmaprubyscrumtimelineworkflows
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.rbindex 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_1module Viewpointsclass API < ::API::OpenProjectAPIresources :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)+ endroute_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+ endend- get &::Bcf::API::V2_1::Endpoints::Show- .new(model: Bcf::Viewpoint,- api_name: 'Viewpoints')- .mount+ get do+ find_json!(params[:viewpoint_uuid])+ endget :selection do- SlicedRepresenter.new(@viewpoint, slice: %w[components selection])+ find_json!(params[:viewpoint_uuid], slice: %w[components selection])endget :coloring do- SlicedRepresenter.new(@viewpoint, slice: %w[components coloring])+ find_json!(params[:viewpoint_uuid], slice: %w[components coloring])endget :visibility do- SlicedRepresenter.new(@viewpoint, slice: %w[components visibility])+ find_json!(params[:viewpoint_uuid], slice: %w[components visibility])endget :bitmaps do@@ -68,7 +88,8 @@ module Bcf::API::V2_1helpers ::API::Helpers::AttachmentRendererget do- if snapshot = @viewpoint.snapshot+ viewpoint = @issue.viewpoints.find_by!(uuid: params[:viewpoint_uuid])+ if snapshot = viewpoint.snapshotrespond_with_attachment snapshotelseraise ActiveRecord::RecordNotFounddiff --git a/modules/bcf/app/models/bcf/viewpoint.rb b/modules/bcf/app/models/bcf/viewpoint.rbindex 0267232b734..e5d6426deb9 100644--- a/modules/bcf/app/models/bcf/viewpoint.rb+++ b/modules/bcf/app/models/bcf/viewpoint.rb@@ -15,6 +15,10 @@ module Bcfhas_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 snapshotattachments.find_by_description('snapshot')enddiff --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.rbdeleted file mode 100644index 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-enddiff --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.rbdeleted file mode 100644index 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-enddiff --git a/modules/bcf/db/migrate/20191121140202_migrate_xml_viewpoint_to_json.rb b/modules/bcf/db/migrate/20191121140202_migrate_xml_viewpoint_to_json.rbnew file mode 100644index 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+enddiff --git a/modules/bcf/lib/open_project/bcf/bcf_json/viewpoint_mapper.rb b/modules/bcf/lib/open_project/bcf/bcf_json/viewpoint_mapper.rbindex 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::Bcfclass ViewpointMapperROOT_NODE ||= 'VisualizationInfo'.freeze- attr_reader :resource+ attr_reader :uuid, :xml- def initialize(viewpoint)- @resource = viewpoint+ def initialize(uuid, xml)+ @uuid = uuid+ @xml = xmlenddef result@@ -24,7 +25,7 @@ module OpenProject::Bcfdef 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::Bcfenddef set_uuid(hash)- hash['guid'] = resource.uuid+ hash['guid'] = uuidenddef transform_keys(hash)@@ -111,6 +112,9 @@ module OpenProject::Bcfselections = hash.dig('components', 'selection', 'component')return unless selections+ # Ensure selections are an array+ selections = Array.wrap(selections)+# Skip any components that have no guidselections.select! { |item| item['ifc_guid'] }@@ -124,7 +128,7 @@ module OpenProject::Bcfreturn 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 colordiff --git a/modules/bcf/lib/open_project/bcf/bcf_xml/issue_reader.rb b/modules/bcf/lib/open_project/bcf/bcf_xml/issue_reader.rbindex 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::BcfXmlissue: 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::BcfXmlfile_entry.get_input_stream.readend+ ##+ # 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.rbnew file mode 100644index 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+enddiff --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.rbindex 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' doit 'always has the guid set to the viewpoint' doexpect(subject.keys).to include 'guid'- expect(subject['guid']).to eq(viewpoint.uuid)+ expect(subject['guid']).to eq(xml_viewpoint.uuid)endit 'always has the perspective_camera OR orthogonal camera set' do@@ -149,13 +149,13 @@ endshared_examples 'matches the JSON counterpart' doit '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.uuidexpect(subject.to_json).to be_json_eql(json)enddiff --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.rbindex 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'endit_behaves_like 'viewpoint keys'@@ -45,8 +45,8 @@ describe OpenProject::Bcf::BcfJson::ViewpointMapper doenddescribe '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'endit_behaves_like 'viewpoint keys'@@ -61,8 +61,8 @@ describe OpenProject::Bcf::BcfJson::ViewpointMapper doenddescribe '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'endit_behaves_like 'viewpoint keys'