Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix #572: add ITs to verify empty JSON Object is valid for sort of find/findOne, fix as necessary #576

Merged
merged 2 commits into from
Oct 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -25,18 +25,15 @@ public record SortClause(@Valid List<SortExpression> sortExpressions) {

public boolean hasVsearchClause() {
return sortExpressions != null
&& sortExpressions.stream()
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Problem was that not only is null check needed, but also isEmpty() for specific case of explicitly empty sort.
But we also don't need to create stream() etc as it's a List can just get the first (and only, for vector/vectorize case as no combinations allowed).

.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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down