Deserialize type: Date values (#776)

* Be explicit about datetimes
* Parse Date objects as specified in the types
This commit is contained in:
Sampo Verkasalo 2019-05-10 21:09:40 +03:00 committed by Graham Paye
parent cb0b81f794
commit 812b374103
2 changed files with 43 additions and 8 deletions

View File

@ -80,6 +80,10 @@ module Google
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)

View File

@ -30,6 +30,8 @@ RSpec.describe Google::Apis::Core::JsonRepresentation do
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
@ -49,8 +51,10 @@ RSpec.describe Google::Apis::Core::JsonRepresentation do
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
@ -85,8 +89,16 @@ RSpec.describe Google::Apis::Core::JsonRepresentation do
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
@ -121,7 +133,10 @@ RSpec.describe Google::Apis::Core::JsonRepresentation do
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'
@ -129,7 +144,6 @@ RSpec.describe Google::Apis::Core::JsonRepresentation do
model.child = child_class.new
model.child.value = 'child'
model.children = [model.child]
model.nil_date_value = nil
model.big_value = 1208925819614629174706176
model
end
@ -145,7 +159,9 @@ RSpec.describe Google::Apis::Core::JsonRepresentation do
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,
@ -171,7 +187,10 @@ RSpec.describe Google::Apis::Core::JsonRepresentation do
"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],
@ -198,8 +217,20 @@ EOF
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