Skip to content

Commit 89481dd

Browse files
committed
fixes #136 writeObjectRef is not implemented
1 parent e2acefe commit 89481dd

File tree

2 files changed

+67
-0
lines changed

2 files changed

+67
-0
lines changed

kmongo-jackson-mapping/src/main/kotlin/org/litote/kmongo/jackson/KMongoBsonFactory.kt

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,14 @@ internal class KMongoBsonFactory : BsonFactory() {
6262
}
6363
}
6464

65+
override fun writeObjectRef(id: Any?) {
66+
if (id is String) {
67+
writeString(id)
68+
} else {
69+
super.writeObjectRef(id)
70+
}
71+
}
72+
6573
fun writeBinary(binary: Binary) {
6674
_writeArrayFieldNameIfNeeded()
6775
_verifyValueWrite("write binary")
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
/*
2+
* Copyright (C) 2017/2019 Litote
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 org.litote.kmongo.issues
18+
19+
import com.fasterxml.jackson.annotation.JsonIdentityInfo
20+
import com.fasterxml.jackson.annotation.JsonIdentityReference
21+
import com.fasterxml.jackson.annotation.ObjectIdGenerators
22+
import org.junit.Test
23+
import org.litote.kmongo.AllCategoriesKMongoBaseTest
24+
import org.litote.kmongo.findOne
25+
import org.litote.kmongo.save
26+
import java.util.Collections
27+
import kotlin.test.assertEquals
28+
29+
/**
30+
*
31+
*/
32+
class Issue136WriteObjectRef : AllCategoriesKMongoBaseTest<Issue136WriteObjectRef.Data>() {
33+
34+
class Data(val entries: List<Entry>)
35+
36+
@JsonIdentityInfo(generator = ObjectIdGenerators.PropertyGenerator::class, property = "uri")
37+
class Entry() {
38+
39+
constructor(uri: String) : this() {
40+
this.uri = uri
41+
}
42+
43+
lateinit var uri: String
44+
45+
@JsonIdentityReference(alwaysAsId = true)
46+
var contains: MutableSet<Entry> = Collections.synchronizedSet(HashSet())
47+
48+
}
49+
50+
@Test
51+
fun saveWithWriteObjectRefDoesNotFail() {
52+
val entry = Entry("http://litote.org/kmongo")
53+
col.save(Data(listOf(entry, entry)))
54+
val data2 = col.findOne()
55+
assertEquals(2, data2!!.entries.size)
56+
assertEquals(entry.uri, data2.entries.first().uri)
57+
assertEquals(entry.uri, data2.entries.last().uri)
58+
}
59+
}

0 commit comments

Comments
 (0)