Skip to content

Commit 0e90968

Browse files
pasinborrrden
authored andcommitted
CBL-8389 : Fix build and static analysis issues in kt-serialization API changes (#498)
* Replace var with explicit FLDict type in Document.setContent (common is compiled at Java 8) * Suppress PMD SingularField and SpotBugs URF_UNREAD_FIELD on Document.extraBackingStore (keep-alive field) * Add null check on FLValue.fromData() result in Document.setContent (NP_NULL_ON_SOME_PATH) * Move serialization tests from test/java to test/kotlin so that only the android-ktx test builds compile them : - FleeceSerializationTest.kt moved as-is - ResultTest.kt renamed to ResultSerializationTest.kt - Serialization test and TestModel extracted from CollectionTest.kt into new CollectionSerializationTest.kt
1 parent abd6dc4 commit 0e90968

5 files changed

Lines changed: 80 additions & 53 deletions

File tree

common/main/java/com/couchbase/lite/Document.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,8 @@ static Document getDocumentWithRevisions(@NonNull Collection collection, @NonNul
136136

137137
// Set by setData(FLSliceResult,boolean) to keep the Fleece backing store from being GC'd.
138138
// (This is kind of a hack, and it's only used ephemerally by Kotlin serialization.)
139+
@SuppressFBWarnings("URF_UNREAD_FIELD")
140+
@SuppressWarnings("PMD.SingularField")
139141
@GuardedBy("lock")
140142
@Nullable
141143
private FLSliceResult extraBackingStore;
@@ -632,9 +634,10 @@ private void setC4Document(@Nullable C4Document c4doc, boolean mutable) {
632634
// for use by CollectionExtensions.kt
633635
void setContent(@NonNull FLSliceResult fleeceData, boolean mutable) {
634636
synchronized (lock) {
635-
var data = FLValue.fromData(fleeceData).asFLDict();
637+
final FLValue body = FLValue.fromData(fleeceData);
638+
if (body == null) { throw new CouchbaseLiteError("Failed parsing fleece data"); }
636639
extraBackingStore = fleeceData;
637-
setContentLocked(data, mutable);
640+
setContentLocked(body.asFLDict(), mutable);
638641
}
639642
}
640643

common/test/java/com/couchbase/lite/CollectionTest.kt

Lines changed: 0 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,6 @@
1717
package com.couchbase.lite
1818

1919
import com.couchbase.lite.internal.utils.SlowTest
20-
import kotlinx.serialization.Serializable
21-
import kotlinx.serialization.Transient
2220
import org.junit.Assert
2321
import org.junit.Test
2422

@@ -1215,52 +1213,4 @@ class CollectionTest : BaseDbTest() {
12151213

12161214
Assert.assertEquals(scope, testDatabase.getScope(Scope.DEFAULT_NAME))
12171215
}
1218-
1219-
//---------------------------------------------
1220-
// Model-based (serialization) API
1221-
//---------------------------------------------
1222-
1223-
@Test
1224-
fun saveNewDocInCollectionFromModel() {
1225-
val id = getUniqueName("test_doc")
1226-
1227-
// Create a new model and save it:
1228-
val model = TestModel("Nigel", 12)
1229-
testCollection.save(model, id)
1230-
Assert.assertEquals(testCollection, model.documentMeta?.collection)
1231-
Assert.assertEquals(id, model.documentMeta?.id)
1232-
1233-
// Read it back:
1234-
Assert.assertEquals(1, testCollection.count)
1235-
val gotModel = testCollection.getDocumentAs<TestModel>(id)!!
1236-
Assert.assertEquals(model, gotModel)
1237-
1238-
// Modify and save again:
1239-
gotModel.favorites = listOf("XTC", "Elvis Costello")
1240-
Assert.assertTrue(testCollection.save(gotModel))
1241-
Assert.assertNotEquals(model.documentMeta?.revisionID, gotModel.documentMeta?.revisionID)
1242-
1243-
// Get it as a regular Document and verify the contents:
1244-
val doc = testCollection.getDocument(id)!!
1245-
Assert.assertEquals(gotModel.documentMeta?.revisionID, doc.revisionID)
1246-
Assert.assertEquals("Nigel", doc.getString("name"))
1247-
Assert.assertEquals(12, doc.getInt("age"))
1248-
val faves = doc.getArray("favorites")!!
1249-
Assert.assertEquals(2, faves.count())
1250-
Assert.assertEquals("XTC", faves.getString(0))
1251-
Assert.assertEquals("Elvis Costello", faves.getString(1))
1252-
1253-
// Delete the model. `model` is out of date, so it will fail, but `gotModel` is OK:
1254-
Assert.assertFalse(testCollection.delete(model, ConcurrencyControl.FAIL_ON_CONFLICT))
1255-
Assert.assertTrue(testCollection.delete(gotModel, ConcurrencyControl.FAIL_ON_CONFLICT))
1256-
}
1257-
}
1258-
1259-
1260-
// Simple Model class for tests
1261-
@Serializable
1262-
data class TestModel(var name: String,
1263-
var age: Int,
1264-
var favorites: List<String>? = null): DocumentModel {
1265-
@Transient override var documentMeta: DocumentMeta? = null
12661216
}
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
//
2+
// Copyright (c) 2026 Couchbase, Inc.
3+
//
4+
// Licensed under the Apache License, Version 2.0 (the "License");
5+
// you may not use this file except in compliance with the License.
6+
// You may obtain a copy of the License at
7+
//
8+
// http:// www.apache.org/licenses/LICENSE-2.0
9+
//
10+
// Unless required by applicable law or agreed to in writing, software
11+
// distributed under the License is distributed on an "AS IS" BASIS,
12+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
// See the License for the specific language governing permissions and
14+
// limitations under the License.
15+
//
16+
17+
package com.couchbase.lite
18+
19+
import kotlinx.serialization.Serializable
20+
import kotlinx.serialization.Transient
21+
import org.junit.Assert
22+
import org.junit.Test
23+
24+
25+
/** Tests serialization-based Collection accessors implemented in CollectionExtensions.kt. */
26+
class CollectionSerializationTest : BaseDbTest() {
27+
//---------------------------------------------
28+
// Model-based (serialization) API
29+
//---------------------------------------------
30+
31+
@Test
32+
fun saveNewDocInCollectionFromModel() {
33+
val id = getUniqueName("test_doc")
34+
35+
// Create a new model and save it:
36+
val model = TestModel("Nigel", 12)
37+
testCollection.save(model, id)
38+
Assert.assertEquals(testCollection, model.documentMeta?.collection)
39+
Assert.assertEquals(id, model.documentMeta?.id)
40+
41+
// Read it back:
42+
Assert.assertEquals(1, testCollection.count)
43+
val gotModel = testCollection.getDocumentAs<TestModel>(id)!!
44+
Assert.assertEquals(model, gotModel)
45+
46+
// Modify and save again:
47+
gotModel.favorites = listOf("XTC", "Elvis Costello")
48+
Assert.assertTrue(testCollection.save(gotModel))
49+
Assert.assertNotEquals(model.documentMeta?.revisionID, gotModel.documentMeta?.revisionID)
50+
51+
// Get it as a regular Document and verify the contents:
52+
val doc = testCollection.getDocument(id)!!
53+
Assert.assertEquals(gotModel.documentMeta?.revisionID, doc.revisionID)
54+
Assert.assertEquals("Nigel", doc.getString("name"))
55+
Assert.assertEquals(12, doc.getInt("age"))
56+
val faves = doc.getArray("favorites")!!
57+
Assert.assertEquals(2, faves.count())
58+
Assert.assertEquals("XTC", faves.getString(0))
59+
Assert.assertEquals("Elvis Costello", faves.getString(1))
60+
61+
// Delete the model. `model` is out of date, so it will fail, but `gotModel` is OK:
62+
Assert.assertFalse(testCollection.delete(model, ConcurrencyControl.FAIL_ON_CONFLICT))
63+
Assert.assertTrue(testCollection.delete(gotModel, ConcurrencyControl.FAIL_ON_CONFLICT))
64+
}
65+
}
66+
67+
68+
// Simple Model class for tests
69+
@Serializable
70+
data class TestModel(var name: String,
71+
var age: Int,
72+
var favorites: List<String>? = null): DocumentModel {
73+
@Transient override var documentMeta: DocumentMeta? = null
74+
}

common/test/java/com/couchbase/lite/ResultTest.kt renamed to common/test/kotlin/com/couchbase/lite/ResultSerializationTest.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,4 +67,4 @@ class ResultSerializationTest : BaseQueryTest() {
6767

6868
assertFalse(iter.hasNext())
6969
}
70-
}
70+
}

common/test/java/com/couchbase/lite/internal/fleece/FleeceSerializationTest.kt renamed to common/test/kotlin/com/couchbase/lite/internal/fleece/FleeceSerializationTest.kt

File renamed without changes.

0 commit comments

Comments
 (0)