Skip to content

Commit 51c0e4d

Browse files
committed
Fix serialize/deserialize circular entries
Fixes: #204
1 parent b5efd2c commit 51c0e4d

File tree

3 files changed

+17
-4
lines changed

3 files changed

+17
-4
lines changed

lib/temporal/json.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ module Temporal
55
module JSON
66
OJ_OPTIONS = {
77
mode: :object,
8+
circular: true,
89
# use ruby's built-in serialization. If nil, OJ seems to default to ~15 decimal places of precision
910
float_precision: 0
1011
}.freeze

spec/unit/lib/temporal/connection/serializer/failure_spec.rb

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ def initialize(message)
8080
expect(avoids_truncation_error).to eq(old_style_deserialized_error)
8181
end
8282

83-
it 'logs a helpful error when the payload is too large' do
83+
it 'logs a helpful error when the payload is too large' do
8484
e = MyBigError.new('Uh oh!')
8585

8686
allow(Temporal.logger).to receive(:error)
@@ -90,10 +90,9 @@ def initialize(message)
9090
.to have_received(:error)
9191
.with(
9292
"Could not serialize exception because it's too large, so we are using a fallback that may not deserialize "\
93-
"correctly on the client. First #{max_bytes} bytes:\n{\"^o\":\"MyBigError\",\"big_payload\":\"1234567890123456",
93+
"correctly on the client. First #{max_bytes} bytes:\n{\"^o\":\"MyBigError\",\"^i\":1,\"big_payload\":\"123456789",
9494
{ unserializable_error: 'MyBigError' }
9595
)
96-
9796
end
9897

9998
class MyArglessError < RuntimeError

spec/unit/lib/temporal/json.rb

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,25 @@
22

33
describe Temporal::JSON do
44
let(:hash) { { 'one' => 'one', two: :two, ':three' => ':three' } }
5-
let(:json) { '{"one":"one",":two":":two","\u003athree":"\u003athree"}' }
5+
let(:json) { '{"^i":1,"one":"one",":two":":two","\u003athree":"\u003athree"}' }
66

77
describe '.serialize' do
88
it 'generates JSON string' do
99
expect(described_class.serialize(hash)).to eq(json)
1010
end
11+
12+
it 'does not raise error on circular entries' do
13+
author = Struct.new('Author', :name, :books)
14+
book = Struct.new('Book', :author)
15+
author_entry = author.new(name: 'David')
16+
book_entry = book.new(title: 'test', author: author)
17+
18+
author_entry.books = [book_entry]
19+
json = described_class.serialize(author_entry)
20+
21+
result = described_class.deserialize(json)
22+
expect(result).to eq(author_entry)
23+
end
1124
end
1225

1326
describe '.deserialize' do

0 commit comments

Comments
 (0)