Skip to content

Commit 27528c6

Browse files
authored
Merge pull request #10914 from eimann/bugfix/json-decode-single-argument-10913
Restore single-argument Json.decode() in the DSL
2 parents b16c7a9 + 326cbde commit 27528c6

3 files changed

Lines changed: 27 additions & 1 deletion

File tree

AUTHORS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ Dirk Goetz <dirk.goetz@icinga.com>
8383
Dirk Wening <dirk.wening@netways.de>
8484
Dirk Melchers <dirk@dirk-melchers.de>
8585
Dolf Schimmel <dolf@transip.nl>
86+
Dominik Bay <dominik@bay.sh>
8687
Dominik Riva <driva@protonmail.com>
8788
dominik-r-s <43005480+dominik-r-s@users.noreply.github.com>
8889
Edgar Fuß <ef@math.uni-bonn.de>

lib/base/json-script.cpp

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,22 @@ static String JsonEncodeShim(const Value& value)
1515
return JsonEncode(value);
1616
}
1717

18+
static Value JsonDecodeShim(const String& data)
19+
{
20+
/* Wrap JsonDecode() so that the DSL function keeps its single-argument
21+
* signature. JsonDecode()'s depthLimit parameter has a default value, but
22+
* function pointers don't carry defaults, so binding it directly would make
23+
* depthLimit a required argument and break Json.decode("..."). See #10913.
24+
*/
25+
return JsonDecode(data);
26+
}
27+
1828
INITIALIZE_ONCE([]() {
1929
Namespace::Ptr jsonNS = new Namespace(true);
2030

2131
/* Methods */
2232
jsonNS->Set("encode", new Function("Json#encode", JsonEncodeShim, { "value" }, true));
23-
jsonNS->Set("decode", new Function("Json#decode", JsonDecode, { "value" }, true));
33+
jsonNS->Set("decode", new Function("Json#decode", JsonDecodeShim, { "value" }, true));
2434

2535
jsonNS->Freeze();
2636

test/base-json.cpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#include "base/io-engine.hpp"
1111
#include "base/objectlock.hpp"
1212
#include "base/json.hpp"
13+
#include "base/scriptglobal.hpp"
1314
#include "test/utils.hpp"
1415
#include <boost/algorithm/string/replace.hpp>
1516
#include <BoostTestTargetConfig.h>
@@ -147,6 +148,20 @@ BOOST_AUTO_TEST_CASE(decode)
147148
BOOST_CHECK(uint.IsNumber() && uint.Get<double>() == 23.0);
148149
}
149150

151+
BOOST_AUTO_TEST_CASE(decode_dsl_single_argument)
152+
{
153+
/* Regression test for #10913: the Json.decode() DSL function must accept a
154+
* single argument. JsonDecode()'s optional depthLimit parameter must not
155+
* leak into the function binding as a required argument.
156+
*/
157+
Namespace::Ptr systemNS = ScriptGlobal::Get("System");
158+
Namespace::Ptr jsonNS = systemNS->Get("Json");
159+
Function::Ptr decode = jsonNS->Get("decode");
160+
161+
BOOST_REQUIRE(decode);
162+
BOOST_CHECK_EQUAL(decode->Invoke({ "2" }), Value(2));
163+
}
164+
150165
BOOST_AUTO_TEST_CASE(invalid1)
151166
{
152167
BOOST_CHECK_THROW(JsonDecode("\"1.7"), std::exception);

0 commit comments

Comments
 (0)