Skip to content

Commit e2acefe

Browse files
committed
fixes #135 JsonIdentityInfo support
1 parent 87a9762 commit e2acefe

File tree

2 files changed

+75
-6
lines changed

2 files changed

+75
-6
lines changed

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

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -46,13 +46,20 @@ internal class KMongoBsonFactory : BsonFactory() {
4646

4747
override fun writeObjectId(objectId: Any) {
4848
if (objectId !is ObjectId) {
49-
error("$objectId has to be ${ObjectId::class}")
49+
//generation with @JsonIdentityInfo - see https://github.com/Litote/kmongo/issues/135
50+
if (objectId is String) {
51+
writeFieldName("_id")
52+
writeString(objectId)
53+
} else {
54+
error("$objectId has to be ${ObjectId::class}")
55+
}
56+
} else {
57+
_writeArrayFieldNameIfNeeded()
58+
_verifyValueWrite("write object id")
59+
_buffer.putByte(_typeMarker, BsonConstants.TYPE_OBJECTID)
60+
objectId.toByteArray().forEach { _buffer.putByte(it) }
61+
flushBuffer()
5062
}
51-
_writeArrayFieldNameIfNeeded()
52-
_verifyValueWrite("write object id")
53-
_buffer.putByte(_typeMarker, BsonConstants.TYPE_OBJECTID)
54-
objectId.toByteArray().forEach { _buffer.putByte(it) }
55-
flushBuffer()
5663
}
5764

5865
fun writeBinary(binary: Binary) {
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
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.issues.Issue135JsonIdentityInfo.Entry
26+
import org.litote.kmongo.save
27+
import java.util.Collections
28+
import kotlin.test.assertEquals
29+
30+
/**
31+
*
32+
*/
33+
class Issue135JsonIdentityInfo : AllCategoriesKMongoBaseTest<Entry>() {
34+
35+
@JsonIdentityInfo(generator = ObjectIdGenerators.PropertyGenerator::class, property = "uri")
36+
class Entry() {
37+
38+
constructor(uri: String) : this() {
39+
this.uri = uri
40+
}
41+
42+
lateinit var uri: String
43+
44+
@JsonIdentityReference(alwaysAsId = true)
45+
var contains: MutableSet<Entry> = Collections.synchronizedSet(HashSet())
46+
47+
}
48+
49+
@JsonIdentityInfo(generator = ObjectIdGenerators.PropertyGenerator::class, property = "uri")
50+
class OriginalEntry(val uri: String) {
51+
@JsonIdentityReference(alwaysAsId = true)
52+
var contains: MutableSet<OriginalEntry> = Collections.synchronizedSet(HashSet())
53+
}
54+
55+
@Test
56+
fun saveDoesNotFail() {
57+
val entry = Entry("http://litote.org/kmongo")
58+
col.save(entry)
59+
assertEquals(entry.uri, col.findOne()?.uri)
60+
}
61+
62+
}

0 commit comments

Comments
 (0)