Skip to content

Commit 00d3635

Browse files
committed
Gracefully fail on a non-tuple argument in Json!JsonSerialize and
Json!ndJsonSerialize. Related to microsoft/CCF#5807 [Feature][TLC]
1 parent 065e7b7 commit 00d3635

File tree

2 files changed

+33
-2
lines changed

2 files changed

+33
-2
lines changed

modules/tlc2/overrides/Json.java

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,10 @@
4242
import com.google.gson.JsonParser;
4343
import com.google.gson.JsonPrimitive;
4444

45+
import tlc2.output.EC;
46+
import tlc2.tool.EvalException;
4547
import tlc2.value.IValue;
48+
import tlc2.value.Values;
4649
import tlc2.value.impl.BoolValue;
4750
import tlc2.value.impl.FcnLambdaValue;
4851
import tlc2.value.impl.FcnRcdValue;
@@ -144,7 +147,11 @@ public static IValue deserialize(final StringValue path) throws IOException {
144147
*/
145148
@TLAPlusOperator(identifier = "ndJsonSerialize", module = "Json", warn = false)
146149
public synchronized static BoolValue ndSerialize(final StringValue path, final Value v) throws IOException {
147-
TupleValue value = (TupleValue) v.toTuple();
150+
final TupleValue value = (TupleValue) v.toTuple();
151+
if (value == null) {
152+
throw new EvalException(EC.TLC_MODULE_ARGUMENT_ERROR,
153+
new String[] { "second", "ndJsonSerialize", "sequence", Values.ppr(v.toString()) });
154+
}
148155
File file = new File(path.val.toString());
149156
if (file.getParentFile() != null) {file.getParentFile().mkdirs();} // Cannot create parent dir for relative path.
150157
try (BufferedWriter writer = new BufferedWriter(new FileWriter(new File(path.val.toString())))) {
@@ -164,7 +171,11 @@ public synchronized static BoolValue ndSerialize(final StringValue path, final V
164171
*/
165172
@TLAPlusOperator(identifier = "JsonSerialize", module = "Json", warn = false)
166173
public synchronized static BoolValue serialize(final StringValue path, final Value v) throws IOException {
167-
TupleValue value = (TupleValue) v.toTuple();
174+
final TupleValue value = (TupleValue) v.toTuple();
175+
if (value == null) {
176+
throw new EvalException(EC.TLC_MODULE_ARGUMENT_ERROR,
177+
new String[] { "second", "JsonSerialize", "sequence", Values.ppr(v.toString()) });
178+
}
168179
File file = new File(path.val.toString());
169180
if (file.getParentFile() != null) {file.getParentFile().mkdirs();} // Cannot create parent dir for relative path.
170181
try (BufferedWriter writer = new BufferedWriter(new FileWriter(new File(path.val.toString())))) {

tests/JsonTests.tla

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,4 +166,24 @@ ASSUME(RoundTrip)
166166
ASSUME LET input == <<[a |-> 3], <<<<1, 2>>, "look">>, << <<[b |-> [c |-> <<4, 5, 6>>]]>> >> >>
167167
IN input = ndJsonDeserialize("tests/JsonTests.ndjson")
168168

169+
-----
170+
171+
ASSUME AssertError(
172+
"The second argument of JsonSerialize should be a sequence, but instead it is:\n[a |-> 1, b |-> TRUE]",
173+
JsonSerialize("target/json/test.json", [a |-> 1, b |-> TRUE] ))
174+
175+
ASSUME AssertError(
176+
"The second argument of ndJsonSerialize should be a sequence, but instead it is:\n[a |-> 1, b |-> TRUE]",
177+
ndJsonSerialize("target/json/test.json", [a |-> 1, b |-> TRUE] ))
178+
179+
180+
ASSUME AssertError(
181+
"The second argument of JsonSerialize should be a sequence, but instead it is:\n42",
182+
JsonSerialize("target/json/test.json", 42 ))
183+
184+
ASSUME AssertError(
185+
"The second argument of ndJsonSerialize should be a sequence, but instead it is:\n42",
186+
ndJsonSerialize("target/json/test.json", 42 ))
187+
188+
169189
=============================================================================

0 commit comments

Comments
 (0)