Skip to content

Commit 89535db

Browse files
committed
fix: clone should return value if properties are in different order
1 parent 88c21f6 commit 89535db

File tree

2 files changed

+17
-1
lines changed

2 files changed

+17
-1
lines changed

src/impl/encoded-types/encoded-types.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1314,7 +1314,13 @@ export const getArc4Encoded = (value: DeliberateAny, sourceTypeInfoString?: stri
13141314
name: `Struct<${value.constructor.name}>`,
13151315
genericArgs: Object.fromEntries(Object.keys(value).map((x, i) => [x, genericArgs[i]])),
13161316
}
1317-
return new Struct(typeInfo, Object.fromEntries(Object.keys(value).map((x, i) => [x, result[i]])))
1317+
let s = Object.fromEntries(Object.keys(typeInfo.genericArgs).map((x, i) => [x, result[i]]))
1318+
// if source type info is provided, reorder the struct properties to match the expected type schema
1319+
if (propTypeInfos) {
1320+
s = Object.fromEntries(Object.keys(propTypeInfos).map((x) => [x, s[x]]))
1321+
typeInfo.genericArgs = propTypeInfos
1322+
}
1323+
return new Struct(typeInfo, s)
13181324
}
13191325

13201326
throw new CodeError(`Unsupported type for encoding: ${typeof value}`)

tests/native-mutable-object.algo.spec.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -789,4 +789,14 @@ describe('native mutable object', () => {
789789
assertMatch(decoded, obj)
790790
})
791791
})
792+
793+
describe('clone', () => {
794+
it('should work when property order is different', () => {
795+
const obj1: NestedObj = { a: 1, b: true, c: 'hello', d: { x: 10, y: 'world', z: true } }
796+
const obj2: NestedObj = { d: { x: 10, z: true, y: 'world' }, a: 1, c: 'hello', b: true }
797+
798+
expect(clone(obj2)).toEqual(obj2)
799+
expect(clone(obj2)).toEqual(obj1)
800+
})
801+
})
792802
})

0 commit comments

Comments
 (0)