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 #634: add tests to verify that empty JSON Object as projection includes whole doc #642

Merged
merged 1 commit into from
Nov 15, 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 @@ -157,18 +157,16 @@ public void wrongNamespace() {

@Test
public void noFilter() {
String json =
"""
given()
.header(HttpConstants.AUTHENTICATION_TOKEN_HEADER_NAME, getAuthToken())
.contentType(ContentType.JSON)
.body(
"""
{
"find": {
}
}
""";

given()
.header(HttpConstants.AUTHENTICATION_TOKEN_HEADER_NAME, getAuthToken())
.contentType(ContentType.JSON)
.body(json)
""")
.when()
.post(CollectionResource.BASE_PATH, namespaceName, collectionName)
.then()
Expand All @@ -180,21 +178,19 @@ public void noFilter() {

@Test
public void noFilterWithOptions() {
String json =
"""
given()
.header(HttpConstants.AUTHENTICATION_TOKEN_HEADER_NAME, getAuthToken())
.contentType(ContentType.JSON)
.body(
"""
{
"find": {
"options" : {
"limit" : 2
}
}
}
""";

given()
.header(HttpConstants.AUTHENTICATION_TOKEN_HEADER_NAME, getAuthToken())
.contentType(ContentType.JSON)
.body(json)
""")
.when()
.post(CollectionResource.BASE_PATH, namespaceName, collectionName)
.then()
Expand All @@ -206,36 +202,71 @@ public void noFilterWithOptions() {

@Test
public void byId() {
String json =
"""
given()
.header(HttpConstants.AUTHENTICATION_TOKEN_HEADER_NAME, getAuthToken())
.contentType(ContentType.JSON)
.body(
"""
{
"find": {
"filter" : {"_id" : "doc1"}
}
}
""";

String expected =
"""
""")
.when()
.post(CollectionResource.BASE_PATH, namespaceName, collectionName)
.then()
.statusCode(200)
.body("status", is(nullValue()))
.body("errors", is(nullValue()))
.body("data.documents", hasSize(1))
.body(
"data.documents[0]",
jsonEquals(
"""
{
"_id": "doc1",
"username": "user1",
"active_user" : true,
"date" : {"$date": 1672531200000}
}
""";
"""));
}

// For [json-api#634]: empty Object as Projection should work same as missing one,
// that is, include everything
@Test
public void byIdEmptyProjection() {
given()
.header(HttpConstants.AUTHENTICATION_TOKEN_HEADER_NAME, getAuthToken())
.contentType(ContentType.JSON)
.body(json)
.body(
"""
{
"find": {
"filter" : {"_id" : "doc1"},
"projection": { }
}
}
""")
.when()
.post(CollectionResource.BASE_PATH, namespaceName, collectionName)
.then()
.statusCode(200)
.body("status", is(nullValue()))
.body("errors", is(nullValue()))
.body("data.documents[0]", jsonEquals(expected))
.body("data.documents", hasSize(1));
.body("data.documents", hasSize(1))
.body(
"data.documents[0]",
jsonEquals(
"""
{
"_id": "doc1",
"username": "user1",
"active_user" : true,
"date" : {"$date": 1672531200000}
}
"""));
}

// https://github.com/stargate/jsonapi/issues/572 -- is passing empty Object for "sort" ok?
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,25 @@ public void verifySliceDefinitionToReturnPositive() throws Exception {

@Nested
class ProjectorApplyInclusions {
// [json-api#634]: empty Object same as "include all"
@Test
public void testIncludeWithEmptyProject() throws Exception {
final JsonNode doc =
objectMapper.readTree(
"""
{
"_id" : 1,
"value1": 42
}
""");
DocumentProjector projection =
DocumentProjector.createFromDefinition(objectMapper.readTree("{ }"));
// Technically considered "Exclusion" but one that excludes nothing
assertThat(projection.isInclusion()).isFalse();
projection.applyProjection(doc);
assertThat(doc).isEqualTo(doc);
}

@Test
public void testSimpleIncludeWithId() throws Exception {
final JsonNode doc =
Expand Down
Loading