@@ -46,55 +46,51 @@ public TestKafkaSerializer(JsonSerializerOptions options, JsonSerializerContext
4646 : base ( options , context )
4747 {
4848 }
49-
49+
5050 // Implementation of the abstract method for test purposes
51- protected override object ? DeserializeComplexTypeFormat ( byte [ ] data ,
51+ protected override object ? DeserializeComplexTypeFormat ( byte [ ] data ,
5252 Type targetType , bool isKey , SchemaMetadata ? schemaMetadata = null )
5353 {
54- try
54+ // Test implementation using JSON for all complex types
55+ var jsonStr = Encoding . UTF8 . GetString ( data ) ;
56+
57+ if ( SerializerContext != null )
5558 {
56- // Test implementation using JSON for all complex types
57- var jsonStr = Encoding . UTF8 . GetString ( data ) ;
58-
59- if ( SerializerContext != null )
59+ var typeInfo = SerializerContext . GetTypeInfo ( targetType ) ;
60+ if ( typeInfo != null )
6061 {
61- var typeInfo = SerializerContext . GetTypeInfo ( targetType ) ;
62- if ( typeInfo != null )
63- {
64- return JsonSerializer . Deserialize ( jsonStr , typeInfo ) ;
65- }
62+ return JsonSerializer . Deserialize ( jsonStr , typeInfo ) ;
6663 }
67-
68- return JsonSerializer . Deserialize ( jsonStr , targetType , JsonOptions ) ;
69- }
70- catch
71- {
72- return null ;
7364 }
65+
66+ return JsonSerializer . Deserialize ( jsonStr , targetType , JsonOptions ) ;
7467 }
75-
68+
7669 // Expose protected methods for direct testing
77- public object ? TestDeserializeFormatSpecific ( byte [ ] data , Type targetType , bool isKey , SchemaMetadata ? schemaMetadata = null )
70+ public object ? TestDeserializeFormatSpecific ( byte [ ] data , Type targetType , bool isKey ,
71+ SchemaMetadata ? schemaMetadata = null )
7872 {
7973 return DeserializeFormatSpecific ( data , targetType , isKey , schemaMetadata ) ;
8074 }
81-
82- public object ? TestDeserializeComplexTypeFormat ( byte [ ] data , Type targetType , bool isKey , SchemaMetadata ? schemaMetadata = null )
75+
76+ public object ? TestDeserializeComplexTypeFormat ( byte [ ] data , Type targetType , bool isKey ,
77+ SchemaMetadata ? schemaMetadata = null )
8378 {
8479 return DeserializeComplexTypeFormat ( data , targetType , isKey , schemaMetadata ) ;
8580 }
86-
81+
8782 public object ? TestDeserializePrimitiveValue ( byte [ ] data , Type targetType )
8883 {
8984 return DeserializePrimitiveValue ( data , targetType ) ;
9085 }
91-
86+
9287 public bool TestIsPrimitiveOrSimpleType ( Type type )
9388 {
9489 return IsPrimitiveOrSimpleType ( type ) ;
9590 }
96-
97- public object TestDeserializeValue ( string base64Value , Type valueType , SchemaMetadata ? schemaMetadata = null )
91+
92+ public object TestDeserializeValue ( string base64Value , Type valueType ,
93+ SchemaMetadata ? schemaMetadata = null )
9894 {
9995 return DeserializeValue ( base64Value , valueType , schemaMetadata ) ;
10096 }
@@ -610,7 +606,9 @@ public void DeserializeFormatSpecific_PrimitiveType_UsesDeserializePrimitiveValu
610606 var stringBytes = Encoding . UTF8 . GetBytes ( "primitive-test" ) ;
611607
612608 // Act
613- var result = serializer . TestDeserializeFormatSpecific ( stringBytes , typeof ( string ) , isKey : false , schemaMetadata : null ) ;
609+ var result =
610+ serializer . TestDeserializeFormatSpecific ( stringBytes , typeof ( string ) , isKey : false ,
611+ schemaMetadata : null ) ;
614612
615613 // Assert
616614 Assert . Equal ( "primitive-test" , result ) ;
@@ -625,7 +623,9 @@ public void DeserializeFormatSpecific_ComplexType_UsesDeserializeComplexTypeForm
625623 var jsonBytes = Encoding . UTF8 . GetBytes ( JsonSerializer . Serialize ( complexObject ) ) ;
626624
627625 // Act
628- var result = serializer . TestDeserializeFormatSpecific ( jsonBytes , typeof ( TestModel ) , isKey : false , schemaMetadata : null ) ;
626+ var result =
627+ serializer . TestDeserializeFormatSpecific ( jsonBytes , typeof ( TestModel ) , isKey : false ,
628+ schemaMetadata : null ) ;
629629
630630 // Assert
631631 Assert . NotNull ( result ) ;
@@ -643,7 +643,9 @@ public void DeserializeComplexTypeFormat_ValidJson_DeserializesCorrectly()
643643 var jsonBytes = Encoding . UTF8 . GetBytes ( JsonSerializer . Serialize ( complexObject ) ) ;
644644
645645 // Act
646- var result = serializer . TestDeserializeComplexTypeFormat ( jsonBytes , typeof ( TestModel ) , isKey : true , schemaMetadata : null ) ;
646+ var result =
647+ serializer . TestDeserializeComplexTypeFormat ( jsonBytes , typeof ( TestModel ) , isKey : true ,
648+ schemaMetadata : null ) ;
647649
648650 // Assert
649651 Assert . NotNull ( result ) ;
@@ -653,17 +655,19 @@ public void DeserializeComplexTypeFormat_ValidJson_DeserializesCorrectly()
653655 }
654656
655657 [ Fact ]
656- public void DeserializeComplexTypeFormat_InvalidJson_ReturnsNull ( )
658+ public void DeserializeComplexTypeFormat_InvalidJson_ThrowsException ( )
657659 {
658660 // Arrange
659661 var serializer = new TestKafkaSerializer ( ) ;
660662 var invalidBytes = new byte [ ] { 0xDE , 0xAD , 0xBE , 0xEF } ; // Invalid JSON data
661663
662- // Act
663- var result = serializer . TestDeserializeComplexTypeFormat ( invalidBytes , typeof ( TestModel ) , isKey : true , schemaMetadata : null ) ;
664+ // Act & Assert
665+ // The TestKafkaSerializer throws JsonException directly for invalid JSON
666+ var ex = Assert . Throws < JsonException > ( ( ) =>
667+ serializer . TestDeserializeComplexTypeFormat ( invalidBytes , typeof ( TestModel ) , isKey : true ,
668+ schemaMetadata : null ) ) ;
664669
665- // Assert
666- Assert . Null ( result ) ;
670+ Assert . Contains ( "invalid" , ex . Message . ToLower ( ) ) ;
667671 }
668672
669673 [ Fact ]
@@ -702,18 +706,18 @@ public void IsPrimitiveOrSimpleType_ChecksVariousTypes()
702706 {
703707 // Arrange
704708 var serializer = new TestKafkaSerializer ( ) ;
705-
709+
706710 // Act & Assert
707711 // Primitive types
708712 Assert . True ( serializer . TestIsPrimitiveOrSimpleType ( typeof ( int ) ) ) ;
709713 Assert . True ( serializer . TestIsPrimitiveOrSimpleType ( typeof ( long ) ) ) ;
710714 Assert . True ( serializer . TestIsPrimitiveOrSimpleType ( typeof ( bool ) ) ) ;
711-
715+
712716 // Simple types
713717 Assert . True ( serializer . TestIsPrimitiveOrSimpleType ( typeof ( string ) ) ) ;
714718 Assert . True ( serializer . TestIsPrimitiveOrSimpleType ( typeof ( Guid ) ) ) ;
715719 Assert . True ( serializer . TestIsPrimitiveOrSimpleType ( typeof ( DateTime ) ) ) ;
716-
720+
717721 // Complex types
718722 Assert . False ( serializer . TestIsPrimitiveOrSimpleType ( typeof ( TestModel ) ) ) ;
719723 Assert . False ( serializer . TestIsPrimitiveOrSimpleType ( typeof ( Dictionary < string , int > ) ) ) ;
@@ -758,5 +762,4 @@ public class TestModel
758762 public string Name { get ; set ; }
759763 public int Value { get ; set ; }
760764 }
761- }
762-
765+ }
0 commit comments