JsonObject::createNestedObject() returns JsonObject::invalid() if key is null (fixes #1891)

This commit is contained in:
Benoit Blanchon
2023-03-16 17:47:53 +01:00
parent 8340b36170
commit 63c89f166d
4 changed files with 34 additions and 3 deletions

View File

@ -1,6 +1,11 @@
ArduinoJson: change log
=======================
HEAD
----
* `JsonObject::createNestedObject()` returns `JsonObject::invalid()` if key is null (issue #1891)
v5.13.5
-------

View File

@ -22,7 +22,9 @@ template <typename TStringRef>
inline JsonObject &JsonObject::createNestedObject_impl(TStringRef key) {
if (!_buffer) return JsonObject::invalid();
JsonObject &object = _buffer->createObject();
set(key, object);
if (set(key, object))
return object;
else
return JsonObject::invalid();
}
} // namespace ArduinoJson

View File

@ -5,6 +5,7 @@
add_executable(JsonObjectTests
basics.cpp
containsKey.cpp
createNestedObject.cpp
get.cpp
invalid.cpp
iterator.cpp

View File

@ -0,0 +1,23 @@
// ArduinoJson - arduinojson.org
// Copyright Benoit Blanchon 2014-2023
// MIT License
#include <ArduinoJson.h>
#include <catch.hpp>
TEST_CASE("JsonObject::createNestedObject()") {
DynamicJsonBuffer _jsonBuffer;
JsonObject& _object = _jsonBuffer.createObject();
SECTION("success() should return true if key is non-null") {
JsonObject& obj = _object.createNestedObject("key");
REQUIRE(obj.success() == true);
}
SECTION("success() should return false if key is null") {
const char* null = 0;
JsonObject& obj = _object.createNestedObject(null);
REQUIRE(obj.success() == false);
}
}