@@ -46,55 +46,51 @@ public TestKafkaSerializer(JsonSerializerOptions options, JsonSerializerContext
46
46
: base ( options , context )
47
47
{
48
48
}
49
-
49
+
50
50
// Implementation of the abstract method for test purposes
51
- protected override object ? DeserializeComplexTypeFormat ( byte [ ] data ,
51
+ protected override object ? DeserializeComplexTypeFormat ( byte [ ] data ,
52
52
Type targetType , bool isKey , SchemaMetadata ? schemaMetadata = null )
53
53
{
54
- try
54
+ // Test implementation using JSON for all complex types
55
+ var jsonStr = Encoding . UTF8 . GetString ( data ) ;
56
+
57
+ if ( SerializerContext != null )
55
58
{
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 )
60
61
{
61
- var typeInfo = SerializerContext . GetTypeInfo ( targetType ) ;
62
- if ( typeInfo != null )
63
- {
64
- return JsonSerializer . Deserialize ( jsonStr , typeInfo ) ;
65
- }
62
+ return JsonSerializer . Deserialize ( jsonStr , typeInfo ) ;
66
63
}
67
-
68
- return JsonSerializer . Deserialize ( jsonStr , targetType , JsonOptions ) ;
69
- }
70
- catch
71
- {
72
- return null ;
73
64
}
65
+
66
+ return JsonSerializer . Deserialize ( jsonStr , targetType , JsonOptions ) ;
74
67
}
75
-
68
+
76
69
// 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 )
78
72
{
79
73
return DeserializeFormatSpecific ( data , targetType , isKey , schemaMetadata ) ;
80
74
}
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 )
83
78
{
84
79
return DeserializeComplexTypeFormat ( data , targetType , isKey , schemaMetadata ) ;
85
80
}
86
-
81
+
87
82
public object ? TestDeserializePrimitiveValue ( byte [ ] data , Type targetType )
88
83
{
89
84
return DeserializePrimitiveValue ( data , targetType ) ;
90
85
}
91
-
86
+
92
87
public bool TestIsPrimitiveOrSimpleType ( Type type )
93
88
{
94
89
return IsPrimitiveOrSimpleType ( type ) ;
95
90
}
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 )
98
94
{
99
95
return DeserializeValue ( base64Value , valueType , schemaMetadata ) ;
100
96
}
@@ -610,7 +606,9 @@ public void DeserializeFormatSpecific_PrimitiveType_UsesDeserializePrimitiveValu
610
606
var stringBytes = Encoding . UTF8 . GetBytes ( "primitive-test" ) ;
611
607
612
608
// 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 ) ;
614
612
615
613
// Assert
616
614
Assert . Equal ( "primitive-test" , result ) ;
@@ -625,7 +623,9 @@ public void DeserializeFormatSpecific_ComplexType_UsesDeserializeComplexTypeForm
625
623
var jsonBytes = Encoding . UTF8 . GetBytes ( JsonSerializer . Serialize ( complexObject ) ) ;
626
624
627
625
// 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 ) ;
629
629
630
630
// Assert
631
631
Assert . NotNull ( result ) ;
@@ -643,7 +643,9 @@ public void DeserializeComplexTypeFormat_ValidJson_DeserializesCorrectly()
643
643
var jsonBytes = Encoding . UTF8 . GetBytes ( JsonSerializer . Serialize ( complexObject ) ) ;
644
644
645
645
// 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 ) ;
647
649
648
650
// Assert
649
651
Assert . NotNull ( result ) ;
@@ -653,17 +655,19 @@ public void DeserializeComplexTypeFormat_ValidJson_DeserializesCorrectly()
653
655
}
654
656
655
657
[ Fact ]
656
- public void DeserializeComplexTypeFormat_InvalidJson_ReturnsNull ( )
658
+ public void DeserializeComplexTypeFormat_InvalidJson_ThrowsException ( )
657
659
{
658
660
// Arrange
659
661
var serializer = new TestKafkaSerializer ( ) ;
660
662
var invalidBytes = new byte [ ] { 0xDE , 0xAD , 0xBE , 0xEF } ; // Invalid JSON data
661
663
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 ) ) ;
664
669
665
- // Assert
666
- Assert . Null ( result ) ;
670
+ Assert . Contains ( "invalid" , ex . Message . ToLower ( ) ) ;
667
671
}
668
672
669
673
[ Fact ]
@@ -702,18 +706,18 @@ public void IsPrimitiveOrSimpleType_ChecksVariousTypes()
702
706
{
703
707
// Arrange
704
708
var serializer = new TestKafkaSerializer ( ) ;
705
-
709
+
706
710
// Act & Assert
707
711
// Primitive types
708
712
Assert . True ( serializer . TestIsPrimitiveOrSimpleType ( typeof ( int ) ) ) ;
709
713
Assert . True ( serializer . TestIsPrimitiveOrSimpleType ( typeof ( long ) ) ) ;
710
714
Assert . True ( serializer . TestIsPrimitiveOrSimpleType ( typeof ( bool ) ) ) ;
711
-
715
+
712
716
// Simple types
713
717
Assert . True ( serializer . TestIsPrimitiveOrSimpleType ( typeof ( string ) ) ) ;
714
718
Assert . True ( serializer . TestIsPrimitiveOrSimpleType ( typeof ( Guid ) ) ) ;
715
719
Assert . True ( serializer . TestIsPrimitiveOrSimpleType ( typeof ( DateTime ) ) ) ;
716
-
720
+
717
721
// Complex types
718
722
Assert . False ( serializer . TestIsPrimitiveOrSimpleType ( typeof ( TestModel ) ) ) ;
719
723
Assert . False ( serializer . TestIsPrimitiveOrSimpleType ( typeof ( Dictionary < string , int > ) ) ) ;
@@ -758,5 +762,4 @@ public class TestModel
758
762
public string Name { get ; set ; }
759
763
public int Value { get ; set ; }
760
764
}
761
- }
762
-
765
+ }
0 commit comments