Skip to content

Commit 75ef900

Browse files
committed
Prevent undefined/null values from being used in optionals
1 parent 2af171a commit 75ef900

File tree

2 files changed

+38
-6
lines changed

2 files changed

+38
-6
lines changed

src/chain/struct.ts

+4-1
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,9 @@ export class Struct implements ABISerializableObject {
4949
constructor(object: any) {
5050
const self = this.constructor as typeof Struct
5151
for (const field of self.structFields) {
52-
this[field.name] = object[field.name]
52+
const value = object[field.name]
53+
if (field.optional && !value) continue
54+
this[field.name] = value
5355
}
5456
}
5557

@@ -76,6 +78,7 @@ export class Struct implements ABISerializableObject {
7678
const self = this.constructor as typeof Struct
7779
const rv: any = {}
7880
for (const field of self.structFields) {
81+
if (field.optional && !this[field.name]) continue
7982
rv[field.name] = this[field.name]
8083
}
8184
return rv

test/serializer.ts

+34-5
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,7 @@ suite('serializer', function () {
189189
const decoded = Serializer.decode({data: encoded, type: Test})
190190
assert.equal(
191191
JSON.stringify(decoded),
192-
'{"foo":"bar","things":["a","b","c"],"keys":null,"other":{"doeet":true}}'
192+
'{"foo":"bar","things":["a","b","c"],"other":{"doeet":true}}'
193193
)
194194
})
195195

@@ -769,7 +769,6 @@ suite('serializer', function () {
769769
things: ['do_you_even', 2],
770770
self: {
771771
things: ['do_you_even', '-170141183460469231731687303715884105727'],
772-
self: null,
773772
},
774773
},
775774
})
@@ -912,7 +911,6 @@ suite('serializer', function () {
912911
assert.deepEqual(JSON.parse(JSON.stringify(decoded)), {
913912
foo: 'hello',
914913
bar: [1, 'two', false],
915-
baz: null,
916914
account: 'foobar1234',
917915
})
918916
const abi = Serializer.synthesize(MyStruct)
@@ -1137,7 +1135,7 @@ suite('serializer', function () {
11371135
assert.equal(res1.asset.toString(), '0.0000 SYS')
11381136
assert.equal(res1.superInt.toNumber(), 42)
11391137
assert.equal(res1.jazz.value, 42)
1140-
assert.strictEqual(res1.maybeJazz, null)
1138+
assert.notProperty(res1, 'maybeJazz')
11411139
assert.strictEqual(res1.dumbBool, null)
11421140
assert.strictEqual(res1.bool, false)
11431141
const res2 = Serializer.decode({
@@ -1181,7 +1179,6 @@ suite('serializer', function () {
11811179
strictExtensions: true,
11821180
}) as any
11831181
assert.strictEqual(res5.bool, true)
1184-
11851182
abi.structs[1].fields[1].type = 'many_extensions$'
11861183
assert.throws(() => {
11871184
Serializer.decode({
@@ -1193,6 +1190,38 @@ suite('serializer', function () {
11931190
}, /Circular type reference/)
11941191
})
11951192

1193+
test('optional shouldnt return null', function () {
1194+
@Struct.type('permission_level')
1195+
class permission_level extends Struct {
1196+
@Struct.field(Name)
1197+
actor!: Name
1198+
@Struct.field(Name)
1199+
permission!: Name
1200+
}
1201+
1202+
@Struct.type('approve')
1203+
class approve extends Struct {
1204+
@Struct.field(Name)
1205+
proposer!: Name
1206+
@Struct.field(Name)
1207+
proposal_name!: Name
1208+
@Struct.field(permission_level)
1209+
level!: permission_level
1210+
@Struct.field(Checksum256, {optional: true})
1211+
proposal_hash?: Checksum256
1212+
}
1213+
const res1 = Serializer.decode({
1214+
object: {
1215+
proposer: 'foo',
1216+
proposal_name: 'bar',
1217+
level: {actor: 'foo', permission: 'bar'},
1218+
},
1219+
type: approve,
1220+
strictExtensions: true,
1221+
})
1222+
assert.notProperty(Serializer.objectify(res1), 'proposal_hash')
1223+
})
1224+
11961225
test('action_results', function () {
11971226
const raw = {
11981227
____comment: 'This file was generated with eosio-abigen. DO NOT EDIT ',

0 commit comments

Comments
 (0)