Skip to content

Commit d547989

Browse files
committed
Add support for /:index/_bulk with JSON object as body
1 parent c468cb4 commit d547989

File tree

2 files changed

+40
-7
lines changed

2 files changed

+40
-7
lines changed

src/server/http.cpp

+20-6
Original file line numberDiff line numberDiff line change
@@ -226,17 +226,31 @@ static HttpResponse handleSearchRequest(const HttpRequest &request, const QShare
226226
static HttpResponse handleBulkRequest(const HttpRequest &request, const QSharedPointer<Index> &indexes) {
227227
auto index = getIndex(request, indexes);
228228

229-
auto body = request.json();
230-
if (!body.isArray()) {
231-
return errBadRequest("invalid_bulk_operation", "invalid bulk operation");
229+
QJsonArray opsJsonArray;
230+
231+
auto doc = request.json();
232+
if (doc.isObject()) {
233+
auto obj = doc.object();
234+
if (obj.contains("operations")) {
235+
auto value = obj.value("operations");
236+
if (value.isArray()) {
237+
opsJsonArray = value.toArray();
238+
} else {
239+
return errBadRequest("invalid_bulk_operation", "'operations' must be an array");
240+
}
241+
}
242+
} else if (doc.isArray()) {
243+
opsJsonArray = doc.array();
244+
} else {
245+
return errBadRequest("invalid_bulk_operation",
246+
"request body must be either an array or an object with 'operations' key in it");
232247
}
233-
auto operations = body.array();
234248

235249
try {
236250
auto writer = index->openWriter(true, 1000);
237-
for (auto operation : operations) {
251+
for (auto operation : opsJsonArray) {
238252
if (!operation.isObject()) {
239-
return errBadRequest("invalid_bulk_operation", "invalid bulk operation");
253+
return errBadRequest("invalid_bulk_operation", "operation must be an object");
240254
}
241255
auto operationObj = operation.toObject();
242256
if (operationObj.contains("upsert")) {

src/server/http_test.cpp

+20-1
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ TEST_F(HttpTest, TestSearchNoResults) {
135135
ASSERT_EQ(response.body().toStdString(), "{\"results\":[]}");
136136
}
137137
138-
TEST_F(HttpTest, TestBulk) {
138+
TEST_F(HttpTest, TestBulkArray) {
139139
indexes->createIndex("testidx");
140140
indexes->getIndex("testidx")->insertOrUpdateDocument(112, {31, 41, 51});
141141
indexes->getIndex("testidx")->insertOrUpdateDocument(113, {31, 41, 51});
@@ -157,3 +157,22 @@ TEST_F(HttpTest, TestBulk) {
157157
ASSERT_FALSE(indexes->getIndex("testidx")->containsDocument(113));
158158
ASSERT_EQ(indexes->getIndex("testidx")->getAttribute("foo").toStdString(), "bar");
159159
}*/
160+
161+
TEST_F(HttpTest, TestBulkObject) {
162+
auto request = HttpRequest(HTTP_POST, QUrl("/main/_bulk"));
163+
request.setBody(QJsonDocument(QJsonObject{
164+
{"operations", QJsonArray{
165+
QJsonObject{{"upsert", QJsonObject{{"id", 111}, {"terms", QJsonArray{1, 2, 3}}}}},
166+
QJsonObject{{"upsert", QJsonObject{{"id", 112}, {"terms", QJsonArray{3, 4, 5}}}}},
167+
QJsonObject{{"set", QJsonObject{{"name", "foo"}, {"value", "bar"}}}},
168+
}},
169+
}));
170+
171+
auto response = handler->router().handle(request);
172+
ASSERT_EQ(response.body().toStdString(), "{}");
173+
ASSERT_EQ(response.status(), HTTP_OK);
174+
175+
// ASSERT_TRUE(index->containsDocument(111));
176+
// ASSERT_TRUE(index->containsDocument(112));
177+
ASSERT_EQ(index->info().attribute("foo").toStdString(), "bar");
178+
}

0 commit comments

Comments
 (0)