Skip to content

Commit

Permalink
Deserialize type: Date values (#776)
Browse files Browse the repository at this point in the history
* Be explicit about datetimes
* Parse Date objects as specified in the types
  • Loading branch information
Sampo Verkasalo authored and TheRoyalTnetennba committed May 10, 2019
1 parent cb0b81f commit 812b374
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 8 deletions.
4 changes: 4 additions & 0 deletions lib/google/apis/core/json_representation.rb
Expand Up @@ -80,6 +80,10 @@ def set_default_options(name, options)
options[:render_filter] = ->(value, _doc, *_args) { value.nil? ? nil : value.is_a?(DateTime) ? value.rfc3339(3) : value.to_s }
options[:parse_filter] = ->(fragment, _doc, *_args) { DateTime.parse(fragment) }
end
if options[:type] == Date
options[:render_filter] = ->(value, _doc, *_args) { value.nil? ? nil : value.to_s}
options[:parse_filter] = ->(fragment, _doc, *_args) { Date.parse(fragment) }
end

options[:render_nil] = true
options[:getter] = getter_fn(name)
Expand Down
47 changes: 39 additions & 8 deletions spec/google/apis/core/json_representation_spec.rb
Expand Up @@ -30,6 +30,8 @@
attr_accessor :string_value
attr_accessor :boolean_value_true
attr_accessor :boolean_value_false
attr_accessor :datetime_value
attr_accessor :nil_datetime_value
attr_accessor :date_value
attr_accessor :nil_date_value
attr_accessor :bytes_value
Expand All @@ -49,8 +51,10 @@
property :string_value, as: 'stringValue'
property :boolean_value_true, as: 'booleanValueTrue'
property :boolean_value_false, as: 'booleanValueFalse'
property :date_value, as: 'dateValue', type: DateTime
property :nil_date_value, as: 'nullDateValue', type: DateTime
property :datetime_value, as: 'dateTimeValue', type: DateTime
property :nil_datetime_value, as: 'nullDateTimeValue', type: DateTime
property :date_value, as: 'dateValue', type: Date
property :nil_date_value, as: 'nullDateValue', type: Date
property :bytes_value, as: 'bytesValue', base64: true
property :big_value, as: 'bigValue', numeric_string: true
property :items
Expand Down Expand Up @@ -85,8 +89,16 @@
expect(json).to be_json_eql(%(false)).at_path('booleanValueFalse')
end

it 'serializes datetime values' do
expect(json).to be_json_eql(%("2015-05-01T12:00:00.000+00:00")).at_path('dateTimeValue')
end

it 'allows nil datetime values' do
expect(json).to be_json_eql(%(null)).at_path('nullDateTimeValue')
end

it 'serializes date values' do
expect(json).to be_json_eql(%("2015-05-01T12:00:00.000+00:00")).at_path('dateValue')
expect(json).to be_json_eql(%("2015-05-01")).at_path('dateValue')
end

it 'allows nil date values' do
Expand Down Expand Up @@ -121,15 +133,17 @@
model.nil_value = nil
model.numeric_value = 123
model.string_value = 'test'
model.date_value = DateTime.new(2015, 5, 1, 12)
model.datetime_value = DateTime.new(2015, 5, 1, 12)
model.nil_datetime_value = nil
model.date_value = Date.new(2015, 5, 1)
model.nil_date_value = nil
model.boolean_value_true = true
model.boolean_value_false = false
model.bytes_value = 'Hello world'
model.items = [1, 2, 3]
model.child = child_class.new
model.child.value = 'child'
model.children = [model.child]
model.nil_date_value = nil
model.big_value = 1208925819614629174706176
model
end
Expand All @@ -145,7 +159,9 @@
nil_value: nil,
string_value: 'test',
numeric_value: 123,
date_value: DateTime.new(2015, 5, 1, 12),
datetime_value: DateTime.new(2015, 5, 1, 12),
nil_datetime_value: nil,
date_value: Date.new(2015, 5, 1),
nil_date_value: nil,
boolean_value_true: true,
boolean_value_false: false,
Expand All @@ -171,7 +187,10 @@
"booleanValueTrue": true,
"booleanValueFalse": false,
"numericValue": 123,
"dateValue": "2015-05-01T12:00:00+00:00",
"dateTimeValue": "2015-05-01T12:00:00+00:00",
"nullDateTimeValue": null,
"dateValue": "2015-05-01",
"nullDateValue": null,
"bytesValue": "SGVsbG8gd29ybGQ=",
"bigValue": "1208925819614629174706176",
"items": [1,2,3],
Expand All @@ -198,8 +217,20 @@
expect(model.boolean_value_false).to be_falsey
end

it 'deserializes datetime values' do
expect(model.datetime_value).to eql DateTime.new(2015, 5, 1, 12)
end

it 'deserializes null datetime values' do
expect(model.nil_datetime_value).to be_nil
end

it 'deserializes date values' do
expect(model.date_value).to eql DateTime.new(2015, 5, 1, 12)
expect(model.date_value).to eql Date.new(2015, 5, 1)
end

it 'deserializes null date values' do
expect(model.nil_date_value).to be_nil
end

it 'deserializes basic collections' do
Expand Down

0 comments on commit 812b374

Please sign in to comment.