[go: nahoru, domu]

Skip to content

Commit

Permalink
[#9407]Enable Firestore data bundle to support empty array (#9467)
Browse files Browse the repository at this point in the history
* Change the require condition to optional in some data bundle decode functions
  • Loading branch information
cherylEnkidu committed Mar 23, 2022
1 parent 64c81c0 commit 6911d52
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 14 deletions.
37 changes: 31 additions & 6 deletions Firestore/core/src/bundle/bundle_serializer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless Requiredd by applicable law or agreed to in writing, software
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
Expand Down Expand Up @@ -393,6 +393,16 @@ const nlohmann::json& JsonReader::RequiredObject(const char* child_name,
return json_object.at(child_name);
}

const nlohmann::json& JsonReader::OptionalObject(
const char* child_name,
const json& json_object,
const nlohmann::json& default_value) {
if (json_object.contains(child_name)) {
return json_object.at(child_name);
}
return default_value;
}

double JsonReader::RequiredDouble(const char* name, const json& json_object) {
if (json_object.contains(name)) {
double result = DecodeDouble(json_object.at(name));
Expand Down Expand Up @@ -614,11 +624,22 @@ FilterList BundleSerializer::DecodeCompositeFilter(JsonReader& reader,
return {};
}

auto filters = reader.RequiredArray("filters", filter);
const std::vector<json> default_filters;
const auto& filters =
reader.OptionalArray("filters", filter, default_filters);

const json default_objects;
FilterList result;
for (const auto& f : filters) {
result = result.push_back(
DecodeFieldFilter(reader, reader.RequiredObject("fieldFilter", f)));
const json& field_filter =
reader.OptionalObject("fieldFilter", f, default_objects);
if (!field_filter.empty()) {
result = result.push_back(DecodeFieldFilter(reader, field_filter));
} else {
result = result.push_back(DecodeUnaryFilter(
reader, reader.OptionalObject("unaryFilter", f, default_objects)));
}

if (!reader.ok()) {
return {};
}
Expand All @@ -636,8 +657,10 @@ Bound BundleSerializer::DecodeBound(JsonReader& reader,
return default_bound;
}

std::vector<json> default_values;
const json& bound_json = reader.RequiredObject(bound_name, query);
std::vector<json> values = reader.RequiredArray("values", bound_json);
std::vector<json> values =
reader.OptionalArray("values", bound_json, default_values);
bool before = reader.OptionalBool("before", bound_json);

auto positions = MakeSharedMessage<google_firestore_v1_ArrayValue>({});
Expand Down Expand Up @@ -737,7 +760,9 @@ Message<google_firestore_v1_MapValue> BundleSerializer::DecodeMapValue(

Message<google_firestore_v1_ArrayValue> BundleSerializer::DecodeArrayValue(
JsonReader& reader, const json& array_json) const {
const auto& values = reader.RequiredArray("values", array_json);
std::vector<json> default_values;
const auto& values =
reader.OptionalArray("values", array_json, default_values);

Message<google_firestore_v1_ArrayValue> array_value;
SetRepeatedField(
Expand Down
4 changes: 4 additions & 0 deletions Firestore/core/src/bundle/bundle_serializer.h
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,12 @@ class JsonReader : public util::ReadContext {
const char* name,
const nlohmann::json& json_object,
const std::vector<nlohmann::json>& default_value);

const nlohmann::json& RequiredObject(const char* child_name,
const nlohmann::json& json_object);
const nlohmann::json& OptionalObject(const char* child_name,
const nlohmann::json& json_object,
const nlohmann::json& default_value);

double RequiredDouble(const char* name, const nlohmann::json& json_object);
double OptionalDouble(const char* name,
Expand Down
3 changes: 3 additions & 0 deletions Firestore/core/test/unit/bundle/bundle_reader_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -229,9 +229,12 @@ class BundleReaderTest : public ::testing::Test {
value2.set_string_value("okok");
ProtoValue value3;
value3.set_null_value(google::protobuf::NULL_VALUE);
ProtoValue value4;
value4.mutable_array_value();
document.mutable_fields()->insert({"\0\ud7ff\ue000\uffff\"", value1});
document.mutable_fields()->insert({"\"(╯°□°)╯︵ ┻━┻\"", value2});
document.mutable_fields()->insert({"nValue", value3});
document.mutable_fields()->insert({"emptyArray", value4});

return document;
}
Expand Down
23 changes: 15 additions & 8 deletions Firestore/core/test/unit/bundle/bundle_serializer_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -826,14 +826,6 @@ TEST_F(BundleSerializerTest, DecodeInvalidFieldFilterOperatorFails) {
EXPECT_NOT_OK(reader.status());
}

{
auto json_copy =
ReplacedCopy(json_string, "\"stringValue\"", "\"arrayValue\"");
JsonReader reader;
bundle_serializer.DecodeNamedQuery(reader, Parse(json_copy));
EXPECT_NOT_OK(reader.status());
}

{
auto json_copy = ReplacedCopy(json_string, "\"op\"", "\"Op\"");
JsonReader reader;
Expand All @@ -850,6 +842,14 @@ TEST_F(BundleSerializerTest, DecodeInvalidFieldFilterOperatorFails) {
}

TEST_F(BundleSerializerTest, DecodesCompositeFilter) {
core::Query original = testutil::Query("colls")
.AddingFilter(Filter("f1", "==", nullptr))
.AddingFilter(Filter("f2", "==", true))
.AddingFilter(Filter("f3", "==", 50.3));
VerifyNamedQueryRoundtrip(original);
}

TEST_F(BundleSerializerTest, DecodesCompositeNotNullFilter) {
core::Query original =
testutil::Query("colls")
.AddingFilter(Filter("f1", "not-in", Array(1, "2", 3.0)))
Expand All @@ -858,6 +858,13 @@ TEST_F(BundleSerializerTest, DecodesCompositeFilter) {
VerifyNamedQueryRoundtrip(original);
}

TEST_F(BundleSerializerTest, DecodesCompositeNullFilter) {
core::Query original = testutil::Query("colls")
.AddingFilter(Filter("f1", "==", nullptr))
.AddingFilter(Filter("f2", "==", nullptr));
VerifyNamedQueryRoundtrip(original);
}

TEST_F(BundleSerializerTest, DecodeInvalidCompositeFilterOperatorFails) {
std::string json_string = NamedQueryJsonString(
testutil::Query("colls")
Expand Down

0 comments on commit 6911d52

Please sign in to comment.