Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Deserialize type: Date values #776

Merged
merged 2 commits into from
May 10, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions lib/google/apis/core/json_representation.rb
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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