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
add ability for Representers to parse ISO 8601 datetimes
Christophe Bliard5fe5fe239072
diff --git a/config/locales/en.yml b/config/locales/en.ymlindex 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.rbindex 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 APIdef 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)+ enddate_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 APIdate_and_time = DateTime.iso8601(value)rescue ArgumentErrorraise API::Errors::PropertyFormatError.new(property_name,- I18n.t('api_v3.errors.expected.date'),+ I18n.t('api_v3.errors.expected.datetime'),value)enddiff --git a/spec/lib/api/v3/utilities/date_time_formatter_spec.rb b/spec/lib/api/v3/utilities/date_time_formatter_spec.rbindex 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' doit '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)endit 'returns usual result for non-nils' doexpected = 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)endendshared_examples_for 'can parse nil' doit '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)endit 'returns usual result for non-nils' doexpected = 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)endenddescribe 'format_date' doit 'formats dates' do- expect(subject.format_date(date)).to eql(date.iso8601)+ expect(subject.format_date(date)).to eq(date.iso8601)endit '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)endit_behaves_like 'can format nil' do@@ -72,7 +73,7 @@ describe :DateTimeFormatter dodescribe 'parse_date' doit '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)endit 'rejects parsing non ISO date formats' do@@ -103,11 +104,11 @@ describe :DateTimeFormatter dodescribe 'format_datetime' doit '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)endit 'formats datetimes' do- expect(subject.format_datetime(datetime)).to eql(datetime.utc.iso8601)+ expect(subject.format_datetime(datetime)).to eq(datetime.utc.iso8601)endit_behaves_like 'can format nil' do@@ -116,21 +117,53 @@ describe :DateTimeFormatter doendend+ 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' doit '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')endit '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')endit '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')endit 'formats ints' do- expect(subject.format_duration_from_hours(5)).to eql('PT5H')+ expect(subject.format_duration_from_hours(5)).to eq('PT5H')endit_behaves_like 'can format nil' do@@ -141,15 +174,15 @@ describe :DateTimeFormatter dodescribe 'parse_duration_to_hours' doit '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)endit '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)endit '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)endit 'rejects parsing non sense' do