Skip to content

Commit 1d657db

Browse files
committed
[tontester] Use unmangled name for JSON fields in TL objects
1 parent 537f20e commit 1d657db

File tree

5 files changed

+127
-7
lines changed

5 files changed

+127
-7
lines changed

test/tontester/src/tl/generator/generators/tlobject.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ def _write_to_dict(tlobject: ParsedTLObject, builder: SourceBuilder, ctx: Import
150150
for arg in tlobject.args:
151151
builder.writeln(",")
152152
if arg.type in BASE_TYPES:
153-
builder.write(f"'{arg.name}': ")
153+
builder.write(f"'{arg.unmangled_name}': ")
154154
if arg.type in BASE64_ENCODED_TYPES:
155155
ctx.base64_needed = True
156156
if arg.is_vector:
@@ -164,11 +164,11 @@ def _write_to_dict(tlobject: ParsedTLObject, builder: SourceBuilder, ctx: Import
164164
builder.write("self.{}", arg.name)
165165
else:
166166
if arg.is_vector:
167-
builder.write(f"'{arg.name}': ")
167+
builder.write(f"'{arg.unmangled_name}': ")
168168
builder.write(f"[x.to_dict() for x in self.{arg.name}]")
169169
else:
170170
builder.write(
171-
f"**({{'{arg.name}': self.{arg.name}.to_dict()}} "
171+
f"**({{'{arg.unmangled_name}': self.{arg.name}.to_dict()}} "
172172
+ f"if self.{arg.name} is not None else {{}})"
173173
) # do not write object in json if it's None
174174

@@ -231,11 +231,11 @@ def _write_from_dict(
231231
if not arg.is_vector:
232232
builder.writeln(
233233
f"_{arg.name} = "
234-
+ f"{deserializer}(d.get('{arg.name}', {arg.default_value_for_from_dict()}))"
234+
+ f"{deserializer}(d.get('{arg.unmangled_name}', {arg.default_value_for_from_dict()}))"
235235
)
236236
else:
237237
builder.writeln(
238-
f"_{arg.name} = tl.deserialize_list(d.get('{arg.name}', []), {deserializer})"
238+
f"_{arg.name} = tl.deserialize_list(d.get('{arg.unmangled_name}', []), {deserializer})"
239239
)
240240

241241
builder.writeln(

test/tontester/src/tl/generator/parsers/tlobject/tlarg.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ def __init__(self, name: str, arg_type: str, generic_definition: bool):
1919
"bytes": "bytes_",
2020
}
2121

22+
self.unmangled_name: str = name
2223
self.name: str = MANGLING.get(name, name)
2324

2425
# Default values

test/tontester/tests/tl/test_codegen.py

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22

33
from generated.test_schema import (
44
AllPrimitives,
5+
MangledFieldsBaseTypes,
6+
MangledFieldsObjects,
7+
MangledFieldsVectorBaseTypes,
8+
MangledFieldsVectorObjects,
59
NestedObject,
610
ObjectQueryRequest,
711
OptionalFields,
@@ -504,3 +508,90 @@ def test_object_request_defaults():
504508
req = ObjectQueryRequest.from_dict(d)
505509

506510
assert req.obj is None
511+
512+
513+
def test_mangled_fields_all_cases():
514+
test_cases = [
515+
(
516+
MangledFieldsBaseTypes(
517+
from_=42,
518+
list_="test string",
519+
self_=b"test bytes",
520+
bytes_=b"\x01" * 16,
521+
),
522+
{
523+
"@type": "mangledFieldsBaseTypes",
524+
"from": 42,
525+
"list": "test string",
526+
"self": base64.b64encode(b"test bytes").decode(),
527+
"bytes": base64.b64encode(b"\x01" * 16).decode(),
528+
},
529+
),
530+
(
531+
MangledFieldsVectorBaseTypes(
532+
from_=[1, 2, 3],
533+
list_=["a", "b", "c"],
534+
self_=[b"x", b"y"],
535+
bytes_=[b"\x01" * 32, b"\x02" * 32],
536+
),
537+
{
538+
"@type": "mangledFieldsVectorBaseTypes",
539+
"from": [1, 2, 3],
540+
"list": ["a", "b", "c"],
541+
"self": [
542+
base64.b64encode(b"x").decode(),
543+
base64.b64encode(b"y").decode(),
544+
],
545+
"bytes": [
546+
base64.b64encode(b"\x01" * 32).decode(),
547+
base64.b64encode(b"\x02" * 32).decode(),
548+
],
549+
},
550+
),
551+
(
552+
MangledFieldsObjects(
553+
from_=SimpleObject(id=1, name="obj1"),
554+
list_=VariantA(value=100),
555+
self_=SimpleObject(id=2, name="obj2"),
556+
bytes_=VariantB(text="test"),
557+
),
558+
{
559+
"@type": "mangledFieldsObjects",
560+
"from": {"@type": "simpleObject", "id": 1, "name": "obj1"},
561+
"list": {"@type": "variantA", "value": 100},
562+
"self": {"@type": "simpleObject", "id": 2, "name": "obj2"},
563+
"bytes": {"@type": "variantB", "text": "test"},
564+
},
565+
),
566+
(
567+
MangledFieldsVectorObjects(
568+
from_=[SimpleObject(id=1, name="a"), SimpleObject(id=2, name="b")],
569+
list_=[VariantA(value=10), VariantB(text="x")],
570+
self_=[SimpleObject(id=3, name="c")],
571+
bytes_=[VariantC(flag=True), VariantA(value=20)],
572+
),
573+
{
574+
"@type": "mangledFieldsVectorObjects",
575+
"from": [
576+
{"@type": "simpleObject", "id": 1, "name": "a"},
577+
{"@type": "simpleObject", "id": 2, "name": "b"},
578+
],
579+
"list": [
580+
{"@type": "variantA", "value": 10},
581+
{"@type": "variantB", "text": "x"},
582+
],
583+
"self": [
584+
{"@type": "simpleObject", "id": 3, "name": "c"},
585+
],
586+
"bytes": [
587+
{"@type": "variantC", "flag": True},
588+
{"@type": "variantA", "value": 20},
589+
],
590+
},
591+
),
592+
]
593+
594+
for obj, expected_dict in test_cases:
595+
assert obj.to_dict() == expected_dict
596+
roundtrip_dict = type(obj).from_dict(expected_dict).to_dict()
597+
assert roundtrip_dict == expected_dict

test/tontester/tests/tl/test_schema.tl

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,34 @@ optionalFields
5656

5757
trueField has_feature:true other:int = TrueField;
5858

59+
mangledFieldsBaseTypes
60+
from:int
61+
list:string
62+
self:bytes
63+
bytes:int128
64+
= MangledFieldsBaseTypes;
65+
66+
mangledFieldsVectorBaseTypes
67+
from:vector<int>
68+
list:vector<string>
69+
self:vector<bytes>
70+
bytes:vector<int256>
71+
= MangledFieldsVectorBaseTypes;
72+
73+
mangledFieldsObjects
74+
from:SimpleObject
75+
list:TestVariant
76+
self:SimpleObject
77+
bytes:TestVariant
78+
= MangledFieldsObjects;
79+
80+
mangledFieldsVectorObjects
81+
from:vector<SimpleObject>
82+
list:vector<TestVariant>
83+
self:vector<SimpleObject>
84+
bytes:vector<TestVariant>
85+
= MangledFieldsVectorObjects;
86+
5987
---functions---
6088

6189
query id:int data:string = AllPrimitives;

test/tontester/tests/tontester/test_tl.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ def test_to_dict(pubk: Pub_unenc, block: TonNode_blockIdExt):
147147
"@type": "adnl.packetContents",
148148
"rand1": "AQE=",
149149
"flags": 1,
150-
"from_": {"@type": "pub.unenc", "data": "EhISEhISEhISEhISEhISEhISEhISEhISEhISEhISEhI="},
150+
"from": {"@type": "pub.unenc", "data": "EhISEhISEhISEhISEhISEhISEhISEhISEhISEhISEhI="},
151151
"messages": [],
152152
"seqno": 0,
153153
"confirm_seqno": 0,
@@ -182,7 +182,7 @@ def test_from_dict(pubk: Pub_unenc):
182182
"@type": "adnl.packetContents",
183183
"rand1": "AQE=",
184184
"flags": 1,
185-
"from_": {"@type": "pub.unenc", "data": "EhISEhISEhISEhISEhISEhISEhISEhISEhISEhISEhI="},
185+
"from": {"@type": "pub.unenc", "data": "EhISEhISEhISEhISEhISEhISEhISEhISEhISEhISEhI="},
186186
"rand2": "AQE=",
187187
}
188188
)

0 commit comments

Comments
 (0)