Skip to content
This repository was archived by the owner on Mar 17, 2025. It is now read-only.

Firebase.readEvent() returns "type" instead "put" while getting data from Firebase #374

Merged
merged 16 commits into from
Aug 15, 2018
8 changes: 7 additions & 1 deletion src/FirebaseArduino.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,13 @@ FirebaseObject FirebaseArduino::readEvent() {
String event = client->readStringUntil('\n').substring(6);
client->readStringUntil('\n'); // consume separator
FirebaseObject obj = FirebaseObject(event.c_str());
obj.getJsonVariant().asObject()["type"] = type.c_str();

// required to have a copy of the string but use a char[] format which is
// the only supported format for JsonObject#set (it does not like the std::string of the test env)
char *cstr = new char[type.length() + 1];
strncpy(cstr, type.c_str(), type.length() + 1);
obj.getJsonVariant().as<JsonObject&>().set("type", cstr);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

wouldn't that leak?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ah missed the delete, I was thinking it might not be safe to delete if JsonVariant keep a ref on it.
But set() documentation seems to imply that it make a copy if you pass a char* https://arduinojson.org/v5/api/jsonobject/set/.

delete[] cstr;
return obj;
}

Expand Down