Skip to content

Commit 992815d

Browse files
nzhenrygcf-owl-bot[bot]
andauthoredMay 27, 2024··
fix: set the correct database id on the key parent when calling Key#getParent (#1457)
* fix: set database ID on parent key * 🦉 Updates from OwlBot post-processor See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md --------- Co-authored-by: Owl Bot <gcf-owl-bot[bot]@users.noreply.github.com>
1 parent ff5e397 commit 992815d

File tree

2 files changed

+14
-4
lines changed

2 files changed

+14
-4
lines changed
 

‎google-cloud-datastore/src/main/java/com/google/cloud/datastore/IncompleteKey.java

+4-2
Original file line numberDiff line numberDiff line change
@@ -91,9 +91,11 @@ public Key getParent() {
9191
PathElement parent = ancestors.get(ancestors.size() - 1);
9292
Key.Builder keyBuilder;
9393
if (parent.hasName()) {
94-
keyBuilder = Key.newBuilder(getProjectId(), parent.getKind(), parent.getName());
94+
keyBuilder =
95+
Key.newBuilder(getProjectId(), parent.getKind(), parent.getName(), getDatabaseId());
9596
} else {
96-
keyBuilder = Key.newBuilder(getProjectId(), parent.getKind(), parent.getId());
97+
keyBuilder =
98+
Key.newBuilder(getProjectId(), parent.getKind(), parent.getId(), getDatabaseId());
9799
}
98100
String namespace = getNamespace();
99101
if (namespace != null) {

‎google-cloud-datastore/src/test/java/com/google/cloud/datastore/IncompleteKeyTest.java

+10-2
Original file line numberDiff line numberDiff line change
@@ -25,15 +25,17 @@
2525

2626
public class IncompleteKeyTest {
2727

28-
private static IncompleteKey pk1, pk2, pk4;
29-
private static Key parent1;
28+
private static IncompleteKey pk1, pk2, pk4, pk5;
29+
private static Key parent1, parent2;
3030

3131
@Before
3232
public void setUp() {
3333
pk1 = IncompleteKey.newBuilder("ds", "kind1").build();
3434
parent1 = Key.newBuilder("ds", "kind2", 10).setNamespace("ns").build();
35+
parent2 = Key.newBuilder("ds", "kind2", 10, "test-db").setNamespace("ns").build();
3536
pk2 = IncompleteKey.newBuilder(parent1, "kind3").build();
3637
pk4 = IncompleteKey.newBuilderWithDatabaseId("ds", "kind3", "test-db").build();
38+
pk5 = IncompleteKey.newBuilder(parent2, "kind4").build();
3739
}
3840

3941
@Test
@@ -59,12 +61,18 @@ public void testBuilders() {
5961
assertEquals("test-db", pk4.getDatabaseId());
6062
assertEquals("kind3", pk4.getKind());
6163
assertTrue(pk4.getAncestors().isEmpty());
64+
65+
assertEquals("ds", pk5.getProjectId());
66+
assertEquals("test-db", pk5.getDatabaseId());
67+
assertEquals("kind4", pk5.getKind());
68+
assertEquals(parent2.getPath(), pk5.getAncestors());
6269
}
6370

6471
@Test
6572
public void testParent() {
6673
assertNull(pk1.getParent());
6774
assertEquals(parent1, pk2.getParent());
75+
assertEquals(parent2, pk5.getParent());
6876
Key parent2 = Key.newBuilder("ds", "kind3", "name").setNamespace("ns").build();
6977
IncompleteKey pk3 = IncompleteKey.newBuilder(parent2, "kind3").build();
7078
assertEquals(parent2, pk3.getParent());

0 commit comments

Comments
 (0)
Please sign in to comment.