Skip to content

Commit

Permalink
Prevent undefined/null values from being used in optionals (#110)
Browse files Browse the repository at this point in the history
  • Loading branch information
aaroncox authored Jul 30, 2024
1 parent e001047 commit a4601e8
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 6 deletions.
5 changes: 4 additions & 1 deletion src/chain/struct.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,9 @@ export class Struct implements ABISerializableObject {
constructor(object: any) {
const self = this.constructor as typeof Struct
for (const field of self.structFields) {
this[field.name] = object[field.name]
const value = object[field.name]
if (field.optional && !value) continue
this[field.name] = value
}
}

Expand All @@ -76,6 +78,7 @@ export class Struct implements ABISerializableObject {
const self = this.constructor as typeof Struct
const rv: any = {}
for (const field of self.structFields) {
if (field.optional && !this[field.name]) continue
rv[field.name] = this[field.name]
}
return rv
Expand Down
39 changes: 34 additions & 5 deletions test/serializer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ suite('serializer', function () {
const decoded = Serializer.decode({data: encoded, type: Test})
assert.equal(
JSON.stringify(decoded),
'{"foo":"bar","things":["a","b","c"],"keys":null,"other":{"doeet":true}}'
'{"foo":"bar","things":["a","b","c"],"other":{"doeet":true}}'
)
})

Expand Down Expand Up @@ -769,7 +769,6 @@ suite('serializer', function () {
things: ['do_you_even', 2],
self: {
things: ['do_you_even', '-170141183460469231731687303715884105727'],
self: null,
},
},
})
Expand Down Expand Up @@ -912,7 +911,6 @@ suite('serializer', function () {
assert.deepEqual(JSON.parse(JSON.stringify(decoded)), {
foo: 'hello',
bar: [1, 'two', false],
baz: null,
account: 'foobar1234',
})
const abi = Serializer.synthesize(MyStruct)
Expand Down Expand Up @@ -1137,7 +1135,7 @@ suite('serializer', function () {
assert.equal(res1.asset.toString(), '0.0000 SYS')
assert.equal(res1.superInt.toNumber(), 42)
assert.equal(res1.jazz.value, 42)
assert.strictEqual(res1.maybeJazz, null)
assert.notProperty(res1, 'maybeJazz')
assert.strictEqual(res1.dumbBool, null)
assert.strictEqual(res1.bool, false)
const res2 = Serializer.decode({
Expand Down Expand Up @@ -1181,7 +1179,6 @@ suite('serializer', function () {
strictExtensions: true,
}) as any
assert.strictEqual(res5.bool, true)

abi.structs[1].fields[1].type = 'many_extensions$'
assert.throws(() => {
Serializer.decode({
Expand All @@ -1193,6 +1190,38 @@ suite('serializer', function () {
}, /Circular type reference/)
})

test('optional shouldnt return null', function () {
@Struct.type('permission_level')
class permission_level extends Struct {
@Struct.field(Name)
actor!: Name
@Struct.field(Name)
permission!: Name
}

@Struct.type('approve')
class approve extends Struct {
@Struct.field(Name)
proposer!: Name
@Struct.field(Name)
proposal_name!: Name
@Struct.field(permission_level)
level!: permission_level
@Struct.field(Checksum256, {optional: true})
proposal_hash?: Checksum256
}
const res1 = Serializer.decode({
object: {
proposer: 'foo',
proposal_name: 'bar',
level: {actor: 'foo', permission: 'bar'},
},
type: approve,
strictExtensions: true,
})
assert.notProperty(Serializer.objectify(res1), 'proposal_hash')
})

test('action_results', function () {
const raw = {
____comment: 'This file was generated with eosio-abigen. DO NOT EDIT ',
Expand Down

0 comments on commit a4601e8

Please sign in to comment.