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

fixup! [#46555] Extend Query resource in APIv3 by timestamps

Dombi Attila732cd020dcc5
diff --git a/app/contracts/queries/base_contract.rb b/app/contracts/queries/base_contract.rb
index 03fb42596be..18bdf8dc577 100644
--- a/app/contracts/queries/base_contract.rb
+++ b/app/contracts/queries/base_contract.rb
@@ -60,6 +60,7 @@ module Queries
validate :validate_project
validate :user_allowed_to_make_public
+ validate :timestamps_are_parsable
def validate_project
errors.add :project, :error_not_found if project_id.present? && !project_visible?
@@ -82,5 +83,13 @@ module Queries
errors.add :public, :error_unauthorized
end
end
+
+ def timestamps_are_parsable
+ invalid_timestamps = model.timestamps.reject(&:valid?)
+
+ if invalid_timestamps.any?
+ errors.add :timestamps, :invalid, values: invalid_timestamps.join(", ")
+ end
+ end
end
end
diff --git a/app/models/query/timestamps.rb b/app/models/query/timestamps.rb
index c111d3c6550..1f4360719cd 100644
--- a/app/models/query/timestamps.rb
+++ b/app/models/query/timestamps.rb
@@ -32,9 +32,6 @@ module Query::Timestamps
included do
serialize :timestamps, Array
- validates :timestamps, length: { maximum: 2 }
- validate :timestamps_are_parsable
-
# Returns the timestamps the query should be evaluated at.
#
# In the database, the timestamps are stored as ISO8601 strings.
@@ -51,8 +48,8 @@ module Query::Timestamps
timestamps.any? ? timestamps : [Timestamp.now]
end
- def timestamps=(array)
- super(array.collect { |element| element.respond_to?(:iso8601) ? element.iso8601 : element })
+ def timestamps=(params)
+ super(Array(params).collect { |element| element.respond_to?(:iso8601) ? element.iso8601 : element })
end
# Does this query perform a historic search?
@@ -60,15 +57,5 @@ module Query::Timestamps
def historic?
timestamps != [Timestamp.now]
end
-
- private
-
- def timestamps_are_parsable
- invalid_timestamps = timestamps.reject(&:valid?)
-
- if invalid_timestamps.any?
- errors.add :timestamps, :invalid, values: invalid_timestamps.join(", ")
- end
- end
end
end
diff --git a/config/locales/en.yml b/config/locales/en.yml
index 6a82614d2e3..dee0ecc8f39 100644
--- a/config/locales/en.yml
+++ b/config/locales/en.yml
@@ -810,7 +810,6 @@ en:
timestamps:
invalid: "Timestamps contain invalid values: %{values}"
format: "%{message}"
- too_long: "Timestamps are too long (maximum is %{count} values)."
group_by_hierarchies_exclusive: "is mutually exclusive with group by '%{group_by}'. You cannot activate both."
filters:
custom_fields:
diff --git a/docs/api/apiv3/tags/basic_objects.yml b/docs/api/apiv3/tags/basic_objects.yml
index d9815f95435..5d8f371b209 100644
--- a/docs/api/apiv3/tags/basic_objects.yml
+++ b/docs/api/apiv3/tags/basic_objects.yml
@@ -235,6 +235,8 @@ description: |-
* `Duration` - refers to an ISO 8601 duration, e.g. "P1DT18H"
+ * `Timestamp` - refers to an ISO 8601 combined date and time, e.g. "2014-05-21T13:37:00Z" or to an ISO 8601 duration, e.g. "P1DT18H"
+
## Colors
Colors are represented in RGB using hexadecimal notation as specified in [CSS Color Module Level 3](https://www.w3.org/TR/css3-color/).
diff --git a/spec/requests/api/v3/queries/create_form_api_spec.rb b/spec/requests/api/v3/queries/create_form_api_spec.rb
index 29712e574fa..714f6267dca 100644
--- a/spec/requests/api/v3/queries/create_form_api_spec.rb
+++ b/spec/requests/api/v3/queries/create_form_api_spec.rb
@@ -455,6 +455,14 @@ describe "POST /api/v3/queries/form" do
expect(form.dig("_embedded", "payload", "timestamps")).to eq timestamps
end
+ context 'with one timestamp is present only' do
+ let(:timestamps) { "PT0S" }
+
+ it 'has the timestamp set' do
+ expect(form.dig("_embedded", "payload", "timestamps")).to eq [timestamps]
+ end
+ end
+
context "with the project referred to by its identifier" do
let(:override_params) do
links = parameters[:_links]
@@ -474,7 +482,7 @@ describe "POST /api/v3/queries/form" do
end
context "with groupBy specified as a GET parameter" do
- let(:path) { api_v3_paths.create_query_form + "?groupBy=author" }
+ let(:path) { "#{api_v3_paths.create_query_form}?groupBy=author" }
let(:override_params) do
links = parameters[:_links]
@@ -577,17 +585,6 @@ describe "POST /api/v3/queries/form" do
.to eq "Timestamps contain invalid values: invalid, invalid2"
end
end
-
- context 'when there are too many values' do
- let(:override_params) do
- { timestamps: [2.weeks.ago.iso8601, 1.week.ago.iso8601, "P0D"] }
- end
-
- it "returns a validation error" do
- expect(form.dig("_embedded", "validationErrors", "timestamps", "message"))
- .to eq "Timestamps are too long (maximum is 2 values)."
- end
- end
end
context "with an unauthorized user trying to set the query public" do
diff --git a/spec/requests/api/v3/queries/update_form_api_spec.rb b/spec/requests/api/v3/queries/update_form_api_spec.rb
index f75605a0da3..ca61121979d 100644
--- a/spec/requests/api/v3/queries/update_form_api_spec.rb
+++ b/spec/requests/api/v3/queries/update_form_api_spec.rb
@@ -438,6 +438,14 @@ describe "POST /api/v3/queries/form" do
expect(form.dig("_embedded", "payload", "timestamps")).to eq timestamps
end
+ context 'with one timestamp is present only' do
+ let(:timestamps) { "PT0S" }
+
+ it 'has the timestamp set' do
+ expect(form.dig("_embedded", "payload", "timestamps")).to eq [timestamps]
+ end
+ end
+
context "with the project referred to by its identifier" do
let(:override_params) do
links = parameters[:_links]
@@ -544,17 +552,6 @@ describe "POST /api/v3/queries/form" do
.to eq "Timestamps contain invalid values: invalid, invalid2"
end
end
-
- context 'when there are too many values' do
- let(:override_params) do
- { timestamps: [2.weeks.ago.iso8601, 1.week.ago.iso8601, "P0D"] }
- end
-
- it "returns a validation error" do
- expect(form.dig("_embedded", "validationErrors", "timestamps", "message"))
- .to eq "Timestamps are too long (maximum is 2 values)."
- end
- end
end
context "with an unauthorized user trying to set the query public" do