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

Fix: Allow non Float numeric values as JSON values in viewpoints

Wieland Lindenthal08369e30eaab
diff --git a/modules/bcf/app/contracts/bcf/viewpoints/create_contract.rb b/modules/bcf/app/contracts/bcf/viewpoints/create_contract.rb
index 3fa259494ea..21c21a7ceb1 100644
--- a/modules/bcf/app/contracts/bcf/viewpoints/create_contract.rb
+++ b/modules/bcf/app/contracts/bcf/viewpoints/create_contract.rb
@@ -131,7 +131,7 @@ module Bcf::Viewpoints
if ocjson.keys != ORTHOGONAL_CAMERA_PROPERTIES ||
ocjson.except('view_to_world_scale').any? { |_, direction| invalid_direction?(direction) } ||
- !ocjson['view_to_world_scale'].is_a?(Float)
+ !ocjson['view_to_world_scale'].is_a?(Numeric)
errors.add(:json_viewpoint, :invalid_orthogonal_camera)
end
end
@@ -141,7 +141,7 @@ module Bcf::Viewpoints
if pcjson.keys != PERSPECTIVE_CAMERA_PROPERTIES ||
pcjson.except('field_of_view').any? { |_, direction| invalid_direction?(direction) } ||
- !pcjson['field_of_view'].is_a?(Float)
+ !pcjson['field_of_view'].is_a?(Numeric)
errors.add(:json_viewpoint, :invalid_perspective_camera)
end
end
@@ -246,7 +246,7 @@ module Bcf::Viewpoints
def invalid_direction?(direction)
!direction.is_a?(Hash) ||
direction.keys != WHITELISTED_DIMENSIONS ||
- direction.values.any? { |v| !v.is_a? Float }
+ direction.values.any? { |v| !v.is_a? Numeric }
end
alias_method :invalid_point?, :invalid_direction?
diff --git a/modules/bcf/lib/open_project/bcf/bcf_json/viewpoint_reader.rb b/modules/bcf/lib/open_project/bcf/bcf_json/viewpoint_reader.rb
index d27f7282740..bc5310a2a5e 100644
--- a/modules/bcf/lib/open_project/bcf/bcf_json/viewpoint_reader.rb
+++ b/modules/bcf/lib/open_project/bcf/bcf_json/viewpoint_reader.rb
@@ -1,3 +1,5 @@
+require 'bigdecimal'
+
module OpenProject::Bcf
module BcfJson
class ViewpointReader
@@ -84,9 +86,9 @@ module OpenProject::Bcf
hash[key].transform_values! do |v|
if v.is_a?(Hash)
- v.transform_values!(&:to_f)
+ v.transform_values! { |val| to_numeric(val) }
else
- v.to_f
+ to_numeric(v)
end
end
end
@@ -95,7 +97,7 @@ module OpenProject::Bcf
return unless hash.key?('lines')
hash['lines'] = hash['lines']['line'].map! do |line|
- line.deep_transform_values!(&:to_f)
+ line.deep_transform_values! { |val| to_numeric(val) }
end
end
@@ -103,7 +105,7 @@ module OpenProject::Bcf
return unless hash.key?('clipping_planes')
hash['clipping_planes'] = hash['clipping_planes']['clipping_plane'].map! do |plane|
- plane.deep_transform_values!(&:to_f)
+ plane.deep_transform_values! { |val| to_numeric(val) }
end
end
@@ -118,13 +120,13 @@ module OpenProject::Bcf
hash['bitmaps'] = bitmaps.map! do |bitmap|
bitmap['bitmap_type'] = bitmap.delete('bitmap').downcase
bitmap['bitmap_data'] = bitmap.delete('reference')
- bitmap['height'] = bitmap['height'].to_f
+ bitmap['height'] = to_numeric(bitmap['height'])
%w[location normal up].each do |key|
next unless bitmap.key?(key)
# Transform all coordinates to floats
- bitmap[key].transform_values!(&:to_f)
+ bitmap[key].transform_values! { |val| to_numeric(val) }
end
bitmap
@@ -180,6 +182,15 @@ module OpenProject::Bcf
# Remove the old node
hash['components'].delete('view_setup_hints')
end
+
+ def to_numeric(anything)
+ num = BigDecimal.new(anything.to_s)
+ if num.frac == 0
+ num.to_i
+ else
+ num.to_f
+ end
+ end
end
end
end
diff --git a/modules/bcf/spec/fixtures/viewpoints/full_viewpoint.bcfv.json b/modules/bcf/spec/fixtures/viewpoints/full_viewpoint.bcfv.json
index 226d1104215..5d9d3476fb0 100644
--- a/modules/bcf/spec/fixtures/viewpoints/full_viewpoint.bcfv.json
+++ b/modules/bcf/spec/fixtures/viewpoints/full_viewpoint.bcfv.json
@@ -79,69 +79,69 @@
"y": 0.126656300502579,
"z": 0.990215996212637
},
- "field_of_view": 60.0
+ "field_of_view": 60
},
"lines": [
{
"start_point": {
- "x": 0.0,
- "y": 0.0,
- "z": 0.0
+ "x": 0,
+ "y": 0,
+ "z": 0
},
"end_point": {
- "x": 0.0,
- "y": 0.0,
- "z": 1.0
+ "x": 0,
+ "y": 0,
+ "z": 1
}
},
{
"start_point": {
- "x": 0.0,
- "y": 0.0,
- "z": 1.0
+ "x": 0,
+ "y": 0,
+ "z": 1
},
"end_point": {
- "x": 0.0,
- "y": 1.0,
- "z": 1.0
+ "x": 0,
+ "y": 1,
+ "z": 1
}
},
{
"start_point": {
- "x": 0.0,
- "y": 1.0,
- "z": 1.0
+ "x": 0,
+ "y": 1,
+ "z": 1
},
"end_point": {
- "x": 1.0,
- "y": 1.0,
- "z": 1.0
+ "x": 1,
+ "y": 1,
+ "z": 1
}
}
],
"clipping_planes": [
{
"location": {
- "x": 0.0,
- "y": 0.0,
- "z": 0.0
+ "x": 0,
+ "y": 0,
+ "z": 0
},
"direction": {
- "x": 0.0,
- "y": 0.0,
- "z": 1.0
+ "x": 0,
+ "y": 0,
+ "z": 1
}
},
{
"location": {
- "x": 0.0,
- "y": 0.0,
- "z": 0.0
+ "x": 0,
+ "y": 0,
+ "z": 0
},
"direction": {
- "x": 0.0,
- "y": 1.0,
- "z": 0.0
+ "x": 0,
+ "y": 1,
+ "z": 0
}
}
],
@@ -157,12 +157,12 @@
"normal": {
"x": -0.9999999999999999,
"y": 1.253656364893038E-16,
- "z": 0.0
+ "z": 0
},
"up": {
"x": -5.43903050550883E-34,
"y": -4.338533794284917E-18,
- "z": 1.0
+ "z": 1
},
"height": 1666.1814563907683
},
@@ -170,21 +170,21 @@
"bitmap_type": "jpg",
"bitmap_data": "3f6ac03e-de8e-4c5e-b3f0-c7bf5f87fe51/bitmaps-e6f5a36a-c60b-4fcc-815f-a1dd939ebc87.jpg",
"location": {
- "x": 1.0,
- "y": 2.0,
- "z": 3.0
+ "x": 1,
+ "y": 2,
+ "z": 3
},
"normal": {
- "x": -3.0,
+ "x": -3,
"y": 1.253656364893038E-16,
- "z": 0.0
+ "z": 0
},
"up": {
"x": 30.1234,
- "y": -9000.0,
- "z": 1.0
+ "y": -9000,
+ "z": 1
},
- "height": 10.0
+ "height": 10
}
]
}
diff --git a/modules/bcf/spec/fixtures/viewpoints/minimal.bcfv.json b/modules/bcf/spec/fixtures/viewpoints/minimal.bcfv.json
index de121a4c73c..0711338b58f 100644
--- a/modules/bcf/spec/fixtures/viewpoints/minimal.bcfv.json
+++ b/modules/bcf/spec/fixtures/viewpoints/minimal.bcfv.json
@@ -7,15 +7,15 @@
"z": -1234.1234
},
"camera_direction": {
- "x": -1.0,
- "y": -2.0,
- "z": -3.0
+ "x": -1,
+ "y": -2,
+ "z": -3
},
"camera_up_vector": {
"x": 0.223629,
"y": 0.209889,
"z": 0.951807
},
- "field_of_view": 180.0
+ "field_of_view": 180
}
}
\ No newline at end of file
diff --git a/modules/bcf/spec/fixtures/viewpoints/neubau_sc_1.bcfv.json b/modules/bcf/spec/fixtures/viewpoints/neubau_sc_1.bcfv.json
index 4aee7f776f3..f2053f87fb7 100644
--- a/modules/bcf/spec/fixtures/viewpoints/neubau_sc_1.bcfv.json
+++ b/modules/bcf/spec/fixtures/viewpoints/neubau_sc_1.bcfv.json
@@ -548,6 +548,6 @@
"y": 0.209889,
"z": 0.951807
},
- "field_of_view": 60.0
+ "field_of_view": 60
}
}
\ No newline at end of file