Skip to content

Commit 7a38b35

Browse files
authored
Bundles Fix: OrderBy should be optional, not required (#7733)
* OrderBy should be optional, not required * String format
1 parent b926297 commit 7a38b35

File tree

2 files changed

+39
-2
lines changed

2 files changed

+39
-2
lines changed

Firestore/core/src/bundle/bundle_serializer.cc

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -239,7 +239,9 @@ Filter DecodeUnaryFilter(JsonReader& reader, const json& filter) {
239239

240240
OrderByList DecodeOrderBy(JsonReader& reader, const json& query) {
241241
OrderByList result;
242-
for (const auto& order_by : reader.RequiredArray("orderBy", query)) {
242+
std::vector<json> default_order_by;
243+
for (const auto& order_by :
244+
reader.OptionalArray("orderBy", query, default_order_by)) {
243245
FieldPath path =
244246
DecodeFieldReference(reader, reader.RequiredObject("field", order_by));
245247

@@ -349,7 +351,7 @@ const std::vector<json>& JsonReader::RequiredArray(const char* name,
349351
}
350352
}
351353

352-
Fail("'%s' is missing or is not a string", name);
354+
Fail("'%s' is missing or is not an array", name);
353355
return EmptyVector<json>();
354356
}
355357

Firestore/core/test/unit/bundle/bundle_serializer_test.cc

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
#include "Firestore/Protos/cpp/google/firestore/v1/document.pb.h"
2222
#include "Firestore/core/src/core/field_filter.h"
2323
#include "Firestore/core/src/core/query.h"
24+
#include "Firestore/core/src/core/target.h"
2425
#include "Firestore/core/src/local/local_serializer.h"
2526
#include "Firestore/core/src/model/database_id.h"
2627
#include "Firestore/core/src/nanopb/byte_string.h"
@@ -49,6 +50,7 @@ using ProtoMaybeDocument = ::firestore::client::MaybeDocument;
4950
using ProtoNamedQuery = ::firestore::NamedQuery;
5051
using ProtoValue = ::google::firestore::v1::Value;
5152
using core::Query;
53+
using core::Target;
5254
using local::LocalSerializer;
5355
using model::DatabaseId;
5456
using nanopb::ByteString;
@@ -924,6 +926,39 @@ TEST_F(BundleSerializerTest, DecodesOrderBys) {
924926
VerifyNamedQueryRoundtrip(original);
925927
}
926928

929+
// By default, the queries used for testing in this file always have default
930+
// OrderBy ("__name__") generated. We need to explicitly remove that for this
931+
// test.
932+
TEST_F(BundleSerializerTest, DecodeMissingOrderBysWorks) {
933+
// This is `NamedQueryToJson(testutil::Query("bundles/docs/colls"))` with
934+
// orderBy field manually removed.
935+
auto json_string = R"|(
936+
{
937+
"name":"query-1",
938+
"bundledQuery":{
939+
"parent":"projects/p/databases/default/documents/bundles/docs",
940+
"structuredQuery":{"from":[{"collectionId":"colls"}]}
941+
},
942+
"readTime":"2021-03-17T14:04:20.166729927Z"
943+
}
944+
)|";
945+
JsonReader reader;
946+
auto named_query =
947+
bundle_serializer.DecodeNamedQuery(reader, Parse(json_string));
948+
949+
EXPECT_OK(reader.status());
950+
EXPECT_EQ(named_query.query_name(), "query-1");
951+
952+
// Reconstruct a core::Query from the deserialized target, this is how
953+
// eventually the named query is used.
954+
const Target& target = named_query.bundled_query().target();
955+
Query query(target.path(), target.collection_group(), target.filters(),
956+
target.order_bys(), target.limit(),
957+
named_query.bundled_query().limit_type(), target.start_at(),
958+
target.end_at());
959+
EXPECT_EQ(query.ToTarget(), testutil::Query("bundles/docs/colls").ToTarget());
960+
}
961+
927962
TEST_F(BundleSerializerTest, DecodeInvalidOrderBysFails) {
928963
std::string json_string =
929964
NamedQueryJsonString(testutil::Query("colls")

0 commit comments

Comments
 (0)