|
|
@@ -1606,3 +1606,93 @@ TEST_F(ApiFixture, VectorSearchFilteredScoresMatchUnfilteredScores) {
|
|
|
ASSERT_GT(plain, 0.0);
|
|
|
EXPECT_NEAR(plain, filtered, 1e-6);
|
|
|
}
|
|
|
+
|
|
|
+// --- document body shape: {data:{...}} vs a bare document --------------------
|
|
|
+
|
|
|
+namespace {
|
|
|
+// The stored document minus the server's own _-prefixed metadata.
|
|
|
+nlohmann::json storedFields(const nlohmann::json& doc) {
|
|
|
+ nlohmann::json out = nlohmann::json::object();
|
|
|
+ for (auto it = doc.begin(); it != doc.end(); ++it)
|
|
|
+ if (it.key().rfind("_", 0) != 0) out[it.key()] = it.value();
|
|
|
+ return out;
|
|
|
+}
|
|
|
+} // namespace
|
|
|
+
|
|
|
+TEST_F(ApiFixture, DocumentWrapperIsUnwrappedOnEveryWriteVerb) {
|
|
|
+ auto c = admin();
|
|
|
+ const std::string base = "/api/v1/projects/" + project_ + "/collections";
|
|
|
+ ASSERT_EQ(c.Post(base.c_str(), nlohmann::json{{"name","shapes"},{"kind","json"}}.dump(),
|
|
|
+ "application/json")->status, 201);
|
|
|
+ const std::string docs = base + "/shapes/documents";
|
|
|
+ auto read = [&](const std::string& id) {
|
|
|
+ auto r = c.Get((docs + "/" + id).c_str());
|
|
|
+ EXPECT_EQ(r->status, 200);
|
|
|
+ const nlohmann::json parsed = nlohmann::json::parse(r->body);
|
|
|
+ return storedFields(parsed);
|
|
|
+ };
|
|
|
+
|
|
|
+ // POST accepts both shapes already.
|
|
|
+ ASSERT_EQ(c.Post(docs.c_str(),
|
|
|
+ nlohmann::json{{"id","w"},{"data",{{"k","x"},{"n",1}}}}.dump(), "application/json")->status, 201);
|
|
|
+ EXPECT_EQ(read("w"), (nlohmann::json{{"k","x"},{"n",1}}));
|
|
|
+
|
|
|
+ ASSERT_EQ(c.Post(docs.c_str(),
|
|
|
+ nlohmann::json{{"id","f"},{"k","x"},{"n",1}}.dump(), "application/json")->status, 201);
|
|
|
+ EXPECT_EQ(read("f"), (nlohmann::json{{"k","x"},{"n",1}}));
|
|
|
+
|
|
|
+ // PUT must unwrap too, instead of storing the wrapper as a literal field.
|
|
|
+ ASSERT_EQ(c.Put((docs + "/w").c_str(),
|
|
|
+ nlohmann::json{{"data",{{"k","x"},{"n",2}}}}.dump(), "application/json")->status, 200);
|
|
|
+ EXPECT_EQ(read("w"), (nlohmann::json{{"k","x"},{"n",2}}))
|
|
|
+ << "PUT stored the {data:...} wrapper instead of unwrapping it";
|
|
|
+
|
|
|
+ ASSERT_EQ(c.Put((docs + "/f").c_str(),
|
|
|
+ nlohmann::json{{"k","x"},{"n",3}}.dump(), "application/json")->status, 200);
|
|
|
+ EXPECT_EQ(read("f"), (nlohmann::json{{"k","x"},{"n",3}}));
|
|
|
+
|
|
|
+ // PATCH merges the unwrapped fields, not a nested `data` object.
|
|
|
+ ASSERT_EQ(c.Patch((docs + "/f").c_str(),
|
|
|
+ nlohmann::json{{"data",{{"n",4}}}}.dump(), "application/json")->status, 200);
|
|
|
+ EXPECT_EQ(read("f"), (nlohmann::json{{"k","x"},{"n",4}}))
|
|
|
+ << "PATCH merged a literal `data` field instead of the fields inside it";
|
|
|
+}
|
|
|
+
|
|
|
+TEST_F(ApiFixture, DataAlongsideSiblingKeysIsRejected) {
|
|
|
+ // Taking body["data"] and discarding the siblings loses data silently, and
|
|
|
+ // the intent is genuinely ambiguous. Refuse it on every verb.
|
|
|
+ auto c = admin();
|
|
|
+ const std::string base = "/api/v1/projects/" + project_ + "/collections";
|
|
|
+ ASSERT_EQ(c.Post(base.c_str(), nlohmann::json{{"name","amb"},{"kind","json"}}.dump(),
|
|
|
+ "application/json")->status, 201);
|
|
|
+ const std::string docs = base + "/amb/documents";
|
|
|
+ ASSERT_EQ(c.Post(docs.c_str(), nlohmann::json{{"id","seed"},{"k","x"}}.dump(),
|
|
|
+ "application/json")->status, 201);
|
|
|
+
|
|
|
+ const std::string ambiguous = nlohmann::json{{"data",{{"k","x"}}},{"extra",99}}.dump();
|
|
|
+ auto post = c.Post(docs.c_str(), ambiguous, "application/json");
|
|
|
+ EXPECT_EQ(post->status, 422) << post->body;
|
|
|
+ auto put = c.Put((docs + "/seed").c_str(), ambiguous, "application/json");
|
|
|
+ EXPECT_EQ(put->status, 422) << put->body;
|
|
|
+ auto patch = c.Patch((docs + "/seed").c_str(), ambiguous, "application/json");
|
|
|
+ EXPECT_EQ(patch->status, 422) << patch->body;
|
|
|
+
|
|
|
+ // The seed document is untouched by the rejected writes.
|
|
|
+ const nlohmann::json after = nlohmann::json::parse(c.Get((docs + "/seed").c_str())->body);
|
|
|
+ EXPECT_EQ(storedFields(after), (nlohmann::json{{"k","x"}}));
|
|
|
+}
|
|
|
+
|
|
|
+TEST_F(ApiFixture, ALiteralDataFieldCanStillBeStored) {
|
|
|
+ // The unwrap would otherwise make a top-level field named `data`
|
|
|
+ // unreachable; nesting it once is the escape hatch.
|
|
|
+ auto c = admin();
|
|
|
+ const std::string base = "/api/v1/projects/" + project_ + "/collections";
|
|
|
+ ASSERT_EQ(c.Post(base.c_str(), nlohmann::json{{"name","lit"},{"kind","json"}}.dump(),
|
|
|
+ "application/json")->status, 201);
|
|
|
+ const std::string docs = base + "/lit/documents";
|
|
|
+ ASSERT_EQ(c.Post(docs.c_str(),
|
|
|
+ nlohmann::json{{"id","d"},{"data",{{"data",{{"inner",1}}}}}}.dump(),
|
|
|
+ "application/json")->status, 201);
|
|
|
+ const nlohmann::json got = nlohmann::json::parse(c.Get((docs + "/d").c_str())->body);
|
|
|
+ EXPECT_EQ(storedFields(got), (nlohmann::json{{"data",{{"inner",1}}}}));
|
|
|
+}
|