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

add ability for Representers to parse ISO 8601 datetimes

Christophe Bliard5fe5fe239072
diff --git a/config/locales/en.yml b/config/locales/en.yml
index 5aac76cf14d..8f03f45ed39 100644
--- a/config/locales/en.yml
+++ b/config/locales/en.yml
@@ -2455,7 +2455,7 @@ en:
setting_apiv3_max_page_size: "Maximum API page size"
setting_apiv3_max_page_instructions_html: >
Set the maximum page size the API will respond with.
- It will not be possible to perform API requests that return more values on a single page.
+ It will not be possible to perform API requests that return more values on a single page.
<br/>
<strong>Warning:</strong> Please only change this value if you are sure why you need it.
Setting to a high value will result in significant performance impacts, while a value lower than the per page options
@@ -3048,6 +3048,7 @@ en:
work_package: "The work package you are looking for cannot be found or has been deleted."
expected:
date: "YYYY-MM-DD (ISO 8601 date only)"
+ datetime: "YYYY-MM-DDThh:mm:ss[.lll][+hh:mm] (any compatible ISO 8601 datetime)"
duration: "ISO 8601 duration"
invalid_content_type: "Expected CONTENT-TYPE to be '%{content_type}' but got '%{actual}'."
invalid_format: "Invalid format for property '%{property}': Expected format like '%{expected_format}', but got '%{actual}'."
diff --git a/lib/api/v3/utilities/date_time_formatter.rb b/lib/api/v3/utilities/date_time_formatter.rb
index 51360494e4c..5aec3f0f06c 100644
--- a/lib/api/v3/utilities/date_time_formatter.rb
+++ b/lib/api/v3/utilities/date_time_formatter.rb
@@ -41,10 +41,15 @@ module API
def self.parse_date(value, property_name, allow_nil: false)
return nil if value.nil? && allow_nil
- date_and_time = parse_datetime(value, property_name, allow_nil: allow_nil)
+ begin
+ date_and_time = DateTime.iso8601(value)
+ rescue ArgumentError
+ raise API::Errors::PropertyFormatError.new(property_name,
+ I18n.t('api_v3.errors.expected.date'),
+ value)
+ end
date_only = date_and_time.to_date
-
# we only want to accept "timeless" dates, e.g. "2015-01-31",
# but not "2015-01-31T01:02:03".
# However Date.iso8601 is too generous and would accept that
@@ -64,7 +69,7 @@ module API
date_and_time = DateTime.iso8601(value)
rescue ArgumentError
raise API::Errors::PropertyFormatError.new(property_name,
- I18n.t('api_v3.errors.expected.date'),
+ I18n.t('api_v3.errors.expected.datetime'),
value)
end
diff --git a/spec/lib/api/v3/utilities/date_time_formatter_spec.rb b/spec/lib/api/v3/utilities/date_time_formatter_spec.rb
index 68ef36e3472..8e6d7430598 100644
--- a/spec/lib/api/v3/utilities/date_time_formatter_spec.rb
+++ b/spec/lib/api/v3/utilities/date_time_formatter_spec.rb
@@ -28,40 +28,41 @@
require 'spec_helper'
-describe :DateTimeFormatter do
- subject { ::API::V3::Utilities::DateTimeFormatter }
- let(:date) { Date.today }
+describe ::API::V3::Utilities::DateTimeFormatter do
+ subject { described_class }
+
+ let(:date) { Time.zone.today }
let(:datetime) { DateTime.now }
shared_examples_for 'can format nil' do
it 'accepts nil if asked to' do
- expect(subject.send(method, nil, allow_nil: true)).to eql(nil)
+ expect(subject.send(method, nil, allow_nil: true)).to be(nil)
end
it 'returns usual result for non-nils' do
expected = subject.send(method, input)
- expect(subject.send(method, input, allow_nil: true)).to eql(expected)
+ expect(subject.send(method, input, allow_nil: true)).to eq(expected)
end
end
shared_examples_for 'can parse nil' do
it 'accepts nil if asked to' do
- expect(subject.send(method, nil, 'prop', allow_nil: true)).to eql(nil)
+ expect(subject.send(method, nil, 'prop', allow_nil: true)).to be(nil)
end
it 'returns usual result for non-nils' do
expected = subject.send(method, input, 'prop')
- expect(subject.send(method, input, 'prop', allow_nil: true)).to eql(expected)
+ expect(subject.send(method, input, 'prop', allow_nil: true)).to eq(expected)
end
end
describe 'format_date' do
it 'formats dates' do
- expect(subject.format_date(date)).to eql(date.iso8601)
+ expect(subject.format_date(date)).to eq(date.iso8601)
end
it 'formats datetimes' do
- expect(subject.format_date(datetime)).to eql(datetime.to_date.iso8601)
+ expect(subject.format_date(datetime)).to eq(datetime.to_date.iso8601)
end
it_behaves_like 'can format nil' do
@@ -72,7 +73,7 @@ describe :DateTimeFormatter do
describe 'parse_date' do
it 'parses ISO 8601 dates' do
- expect(subject.parse_date(date.iso8601, 'prop')).to eql(date)
+ expect(subject.parse_date(date.iso8601, 'prop')).to eq(date)
end
it 'rejects parsing non ISO date formats' do
@@ -103,11 +104,11 @@ describe :DateTimeFormatter do
describe 'format_datetime' do
it 'formats dates' do
- expect(subject.format_datetime(date)).to eql(date.to_datetime.utc.iso8601)
+ expect(subject.format_datetime(date)).to eq(date.to_datetime.utc.iso8601)
end
it 'formats datetimes' do
- expect(subject.format_datetime(datetime)).to eql(datetime.utc.iso8601)
+ expect(subject.format_datetime(datetime)).to eq(datetime.utc.iso8601)
end
it_behaves_like 'can format nil' do
@@ -116,21 +117,53 @@ describe :DateTimeFormatter do
end
end
+ describe 'parse_datetime' do
+ it 'parses ISO 8601 datetimes' do
+ expect(subject.parse_datetime(datetime.utc.iso8601(9), 'prop')).to eq(datetime)
+ expect(subject.parse_datetime('1999-12-31T14:15:16', 'prop'))
+ .to eq(DateTime.new(1999, 12, 31, 14, 15, 16))
+ expect(subject.parse_datetime('1999-12-31', 'prop'))
+ .to eq(DateTime.new(1999, 12, 31, 0, 0, 0))
+ expect(subject.parse_datetime('1999-12-31T14:15:16Z', 'prop'))
+ .to eq(DateTime.new(1999, 12, 31, 14, 15, 16, '+00:00'))
+ expect(subject.parse_datetime('1999-12-31T14:15:16+02:30', 'prop'))
+ .to eq(DateTime.new(1999, 12, 31, 11, 45, 16, '+00:00'))
+ expect(subject.parse_datetime('1999-12-31T14:15:16.4242', 'prop'))
+ .to eq(DateTime.new(1999, 12, 31, 14, 15, 16.4242))
+ end
+
+ it 'rejects parsing non ISO date formats' do
+ bad_format = datetime.strftime('%d.%m.%Y')
+ expect do
+ subject.parse_datetime(bad_format, 'prop')
+ end.to raise_error(API::Errors::PropertyFormatError) do |error| # rubocop:disable Style/MultilineBlockChain
+ expect(error.message).to include("Invalid format for property 'prop'")
+ expect(error.message).to include("Expected format like 'YYYY-MM-DDThh:mm:ss[.lll][+hh:mm] " +
+ "(any compatible ISO 8601 datetime)'")
+ end
+ end
+
+ it_behaves_like 'can parse nil' do
+ let(:method) { :parse_datetime }
+ let(:input) { datetime.iso8601(9) }
+ end
+ end
+
describe 'format_duration_from_hours' do
it 'formats floats' do
- expect(subject.format_duration_from_hours(5.0)).to eql('PT5H')
+ expect(subject.format_duration_from_hours(5.0)).to eq('PT5H')
end
it 'formats fractional floats' do
- expect(subject.format_duration_from_hours(5.5)).to eql('PT5H30M')
+ expect(subject.format_duration_from_hours(5.5)).to eq('PT5H30M')
end
it 'includes seconds' do
- expect(subject.format_duration_from_hours(5.501)).to eql('PT5H30M3S')
+ expect(subject.format_duration_from_hours(5.501)).to eq('PT5H30M3S')
end
it 'formats ints' do
- expect(subject.format_duration_from_hours(5)).to eql('PT5H')
+ expect(subject.format_duration_from_hours(5)).to eq('PT5H')
end
it_behaves_like 'can format nil' do
@@ -141,15 +174,15 @@ describe :DateTimeFormatter do
describe 'parse_duration_to_hours' do
it 'parses ISO 8601 durations of full hours' do
- expect(subject.parse_duration_to_hours('PT5H', 'prop')).to eql(5.0)
+ expect(subject.parse_duration_to_hours('PT5H', 'prop')).to eq(5.0)
end
it 'parses ISO 8601 durations of fractional hours' do
- expect(subject.parse_duration_to_hours('PT5H30M', 'prop')).to eql(5.5)
+ expect(subject.parse_duration_to_hours('PT5H30M', 'prop')).to eq(5.5)
end
it 'parses ISO 8601 durations of days' do
- expect(subject.parse_duration_to_hours('P1D', 'prop')).to eql(24.0)
+ expect(subject.parse_duration_to_hours('P1D', 'prop')).to eq(24.0)
end
it 'rejects parsing non sense' do