diff --git a/src/main/java/io/stargate/sgv2/jsonapi/api/model/command/clause/sort/SortClause.java b/src/main/java/io/stargate/sgv2/jsonapi/api/model/command/clause/sort/SortClause.java index 92fcddcd64..4724f56c11 100644 --- a/src/main/java/io/stargate/sgv2/jsonapi/api/model/command/clause/sort/SortClause.java +++ b/src/main/java/io/stargate/sgv2/jsonapi/api/model/command/clause/sort/SortClause.java @@ -25,18 +25,15 @@ public record SortClause(@Valid List sortExpressions) { public boolean hasVsearchClause() { return sortExpressions != null - && sortExpressions.stream() - .findFirst() - .get() - .path() - .equals(DocumentConstants.Fields.VECTOR_EMBEDDING_FIELD); + && !sortExpressions.isEmpty() + && sortExpressions.get(0).path().equals(DocumentConstants.Fields.VECTOR_EMBEDDING_FIELD); } public boolean hasVectorizeSearchClause() { return sortExpressions != null - && sortExpressions.stream() - .findFirst() - .get() + && !sortExpressions.isEmpty() + && sortExpressions + .get(0) .path() .equals(DocumentConstants.Fields.VECTOR_EMBEDDING_TEXT_FIELD); } diff --git a/src/test/java/io/stargate/sgv2/jsonapi/api/v1/FindIntegrationTest.java b/src/test/java/io/stargate/sgv2/jsonapi/api/v1/FindIntegrationTest.java index f310b40df4..834e39513d 100644 --- a/src/test/java/io/stargate/sgv2/jsonapi/api/v1/FindIntegrationTest.java +++ b/src/test/java/io/stargate/sgv2/jsonapi/api/v1/FindIntegrationTest.java @@ -241,6 +241,43 @@ public void byId() { .body("data.documents", hasSize(1)); } + // https://github.com/stargate/jsonapi/issues/572 -- is passing empty Object for "sort" ok? + @Test + public void byIdEmptySort() { + given() + .header(HttpConstants.AUTHENTICATION_TOKEN_HEADER_NAME, getAuthToken()) + .contentType(ContentType.JSON) + .body( + """ + { + "find": { + "filter": {"username" : "user1"}, + "projection": {}, + "options": {}, + "sort": { } + } + } + """) + .when() + .post(CollectionResource.BASE_PATH, namespaceName, collectionName) + .then() + .statusCode(200) + .body("status", is(nullValue())) + .body("errors", is(nullValue())) + .body( + "data.documents[0]", + jsonEquals( + """ + { + "_id": "doc1", + "username": "user1", + "active_user" : true, + "date" : {"$date": 1672531200000} + } + """)) + .body("data.documents", hasSize(1)); + } + @Test public void byDateId() { String json = diff --git a/src/test/java/io/stargate/sgv2/jsonapi/api/v1/FindOneIntegrationTest.java b/src/test/java/io/stargate/sgv2/jsonapi/api/v1/FindOneIntegrationTest.java index be811883ff..49d9996178 100644 --- a/src/test/java/io/stargate/sgv2/jsonapi/api/v1/FindOneIntegrationTest.java +++ b/src/test/java/io/stargate/sgv2/jsonapi/api/v1/FindOneIntegrationTest.java @@ -209,19 +209,42 @@ public void noFilterSortDescending() { @Test public void byId() { - String json = - """ + given() + .header(HttpConstants.AUTHENTICATION_TOKEN_HEADER_NAME, getAuthToken()) + .contentType(ContentType.JSON) + .body( + """ { "findOne": { "filter" : {"_id" : "doc1"} } } - """; + """) + .when() + .post(CollectionResource.BASE_PATH, namespaceName, collectionName) + .then() + .statusCode(200) + .body("data.document", is(not(nullValue()))) + .body("data.document", jsonEquals(DOC1_JSON)) + .body("status", is(nullValue())) + .body("errors", is(nullValue())); + } + // https://github.com/stargate/jsonapi/issues/572 -- is passing empty Object for "sort" ok? + @Test + public void byIdEmptySort() { given() .header(HttpConstants.AUTHENTICATION_TOKEN_HEADER_NAME, getAuthToken()) .contentType(ContentType.JSON) - .body(json) + .body( + """ + { + "findOne": { + "filter": {"_id" : "doc1"}, + "sort": {} + } + } + """) .when() .post(CollectionResource.BASE_PATH, namespaceName, collectionName) .then()