@@ -26,7 +26,7 @@ import org.bson.codecs.{ Codec, DecoderContext, EncoderContext }
26
26
import org .bson .io .{ BasicOutputBuffer , ByteBufferBsonInput , OutputBuffer }
27
27
import org .bson .types .ObjectId
28
28
import org .mongodb .scala .bson .BaseSpec
29
- import org .mongodb .scala .bson .annotations .BsonProperty
29
+ import org .mongodb .scala .bson .annotations .{ BsonIgnore , BsonProperty }
30
30
import org .mongodb .scala .bson .codecs .Macros .{ createCodecProvider , createCodecProviderIgnoreNone }
31
31
import org .mongodb .scala .bson .codecs .Registry .DEFAULT_CODEC_REGISTRY
32
32
import org .mongodb .scala .bson .collection .immutable .Document
@@ -43,6 +43,7 @@ class MacrosSpec extends BaseSpec {
43
43
case class SeqOfStrings (name : String , value : Seq [String ])
44
44
case class RecursiveSeq (name : String , value : Seq [RecursiveSeq ])
45
45
case class AnnotatedClass (@ BsonProperty (" annotated_name" ) name : String )
46
+ case class IgnoredFieldClass (name : String , @ BsonIgnore meta : String = " ignored_default" )
46
47
47
48
case class Binary (binary : Array [Byte ]) {
48
49
@@ -103,6 +104,11 @@ class MacrosSpec extends BaseSpec {
103
104
case class Branch (@ BsonProperty (" l1" ) b1 : Tree , @ BsonProperty (" r1" ) b2 : Tree , value : Int ) extends Tree
104
105
case class Leaf (value : Int ) extends Tree
105
106
107
+ sealed trait WithIgnored
108
+ case class MetaIgnoredField (data : String , @ BsonIgnore meta : Seq [String ] = Vector (" ignore_me" )) extends WithIgnored
109
+ case class LeafCountIgnoredField (branchCount : Int , @ BsonIgnore leafCount : Int = 100 ) extends WithIgnored
110
+ case class ContainsIgnoredField (list : Seq [WithIgnored ])
111
+
106
112
case class ContainsADT (name : String , tree : Tree )
107
113
case class ContainsSeqADT (name : String , trees : Seq [Tree ])
108
114
case class ContainsNestedSeqADT (name : String , trees : Seq [Seq [Tree ]])
@@ -270,6 +276,23 @@ class MacrosSpec extends BaseSpec {
270
276
)
271
277
}
272
278
279
+ it should " be able to ignore fields" in {
280
+ roundTrip(
281
+ IgnoredFieldClass (" Bob" , " singer" ),
282
+ IgnoredFieldClass (" Bob" ),
283
+ """ {name: "Bob"}""" ,
284
+ classOf [IgnoredFieldClass ]
285
+ )
286
+
287
+ roundTrip(
288
+ ContainsIgnoredField (Vector (MetaIgnoredField (" Bob" , List (" singer" )), LeafCountIgnoredField (1 , 10 ))),
289
+ ContainsIgnoredField (Vector (MetaIgnoredField (" Bob" ), LeafCountIgnoredField (1 ))),
290
+ """ {"list" : [{"_t" : "MetaIgnoredField", "data" : "Bob" }, {"_t" : "LeafCountIgnoredField", "branchCount": 1}]}""" ,
291
+ classOf [ContainsIgnoredField ],
292
+ classOf [WithIgnored ]
293
+ )
294
+ }
295
+
273
296
it should " be able to round trip polymorphic nested case classes in a sealed class" in {
274
297
roundTrip(
275
298
ContainsSealedClass (List (SealedClassA (" test" ), SealedClassB (12 ))),
@@ -657,6 +680,15 @@ class MacrosSpec extends BaseSpec {
657
680
roundTripCodec(value, Document (expected), codec)
658
681
}
659
682
683
+ def roundTrip [T ](value : T , decodedValue : T , expected : String , provider : CodecProvider , providers : CodecProvider * )(
684
+ implicit ct : ClassTag [T ]
685
+ ): Unit = {
686
+ val codecProviders : util.List [CodecProvider ] = (provider +: providers).asJava
687
+ val registry = CodecRegistries .fromRegistries(CodecRegistries .fromProviders(codecProviders), DEFAULT_CODEC_REGISTRY )
688
+ val codec = registry.get(ct.runtimeClass).asInstanceOf [Codec [T ]]
689
+ roundTripCodec(value, decodedValue, Document (expected), codec)
690
+ }
691
+
660
692
def roundTripCodec [T ](value : T , expected : Document , codec : Codec [T ]): Unit = {
661
693
val encoded = encode(codec, value)
662
694
val actual = decode(documentCodec, encoded)
@@ -666,6 +698,18 @@ class MacrosSpec extends BaseSpec {
666
698
assert(roundTripped == value, s " Round Tripped case class: ( $roundTripped) did not equal the original: ( $value) " )
667
699
}
668
700
701
+ def roundTripCodec [T ](value : T , decodedValue : T , expected : Document , codec : Codec [T ]): Unit = {
702
+ val encoded = encode(codec, value)
703
+ val actual = decode(documentCodec, encoded)
704
+ assert(expected == actual, s " Encoded document: ( ${actual.toJson()}) did not equal: ( ${expected.toJson()}) " )
705
+
706
+ val roundTripped = decode(codec, encode(codec, value))
707
+ assert(
708
+ roundTripped == decodedValue,
709
+ s " Round Tripped case class: ( $roundTripped) did not equal the expected: ( $decodedValue) "
710
+ )
711
+ }
712
+
669
713
def encode [T ](codec : Codec [T ], value : T ): OutputBuffer = {
670
714
val buffer = new BasicOutputBuffer ()
671
715
val writer = new BsonBinaryWriter (buffer)
0 commit comments