@@ -55,7 +55,7 @@ public class PowertoolsKafkaAvroSerializer : PowertoolsKafkaSerializerBase
55
55
public PowertoolsKafkaAvroSerializer ( ) : base ( )
56
56
{
57
57
}
58
-
58
+
59
59
/// <summary>
60
60
/// Initializes a new instance of the <see cref="PowertoolsKafkaAvroSerializer"/> class
61
61
/// with custom JSON serialization options.
@@ -64,7 +64,7 @@ public PowertoolsKafkaAvroSerializer() : base()
64
64
public PowertoolsKafkaAvroSerializer ( JsonSerializerOptions jsonOptions ) : base ( jsonOptions )
65
65
{
66
66
}
67
-
67
+
68
68
/// <summary>
69
69
/// Initializes a new instance of the <see cref="PowertoolsKafkaAvroSerializer"/> class
70
70
/// with a JSON serializer context for AOT-compatible serialization.
@@ -73,62 +73,41 @@ public PowertoolsKafkaAvroSerializer(JsonSerializerOptions jsonOptions) : base(j
73
73
public PowertoolsKafkaAvroSerializer ( JsonSerializerContext serializerContext ) : base ( serializerContext )
74
74
{
75
75
}
76
-
77
- /// <summary>
78
- /// Gets the Avro schema for the specified type.
79
- /// The type must have a public static _SCHEMA field defined.
80
- /// </summary>
81
- /// <param name="payloadType">The type to get the Avro schema for.</param>
82
- /// <returns>The Avro Schema object.</returns>
83
- /// <exception cref="InvalidOperationException">Thrown if no schema is found for the type.</exception>
84
- [ RequiresDynamicCode ( "Avro schema access requires reflection which may be incompatible with AOT." ) ]
85
- [ RequiresUnreferencedCode ( "Avro schema access requires reflection which may be incompatible with trimming." ) ]
86
- private Schema ? GetAvroSchema ( [ DynamicallyAccessedMembers ( DynamicallyAccessedMemberTypes . PublicFields ) ] Type payloadType )
87
- {
88
- var schemaField = payloadType . GetField ( "_SCHEMA" ,
89
- BindingFlags . Public | BindingFlags . Static ) ;
90
-
91
- if ( schemaField == null )
92
- return null ;
93
-
94
- return schemaField . GetValue ( null ) as Schema ;
95
- }
96
76
97
77
/// <summary>
98
78
/// Deserializes complex (non-primitive) types using Avro format.
79
+ /// Requires types to have a public static _SCHEMA field.
99
80
/// </summary>
100
- /// <param name="data">The binary data to deserialize.</param>
101
- /// <param name="targetType">The type to deserialize to.</param>
102
- /// <param name="isKey">Whether this data represents a key (true) or a value (false).</param>
103
- /// <returns>The deserialized object.</returns>
104
81
[ RequiresDynamicCode ( "Avro deserialization might require runtime code generation." ) ]
105
82
[ RequiresUnreferencedCode ( "Avro deserialization might require types that cannot be statically analyzed." ) ]
106
- protected override object ? DeserializeComplexTypeFormat ( byte [ ] data ,
107
- [ DynamicallyAccessedMembers ( DynamicallyAccessedMemberTypes . PublicFields ) ]
108
- Type targetType , bool isKey )
83
+ protected override object ? DeserializeComplexTypeFormat ( byte [ ] data ,
84
+ [ DynamicallyAccessedMembers ( DynamicallyAccessedMemberTypes . PublicFields ) ]
85
+ Type targetType , bool isKey , SchemaMetadata ? schemaMetadata = null )
109
86
{
110
- try
87
+ var schema = GetAvroSchema ( targetType ) ;
88
+ if ( schema == null )
111
89
{
112
- // Try to get Avro schema for the type
113
- var schema = GetAvroSchema ( targetType ) ;
114
-
115
- if ( schema != null )
116
- {
117
- using var stream = new MemoryStream ( data ) ;
118
- var decoder = new BinaryDecoder ( stream ) ;
119
- var reader = new SpecificDatumReader < object > ( schema , schema ) ;
120
- return reader . Read ( null ! , decoder ) ;
121
- }
122
-
123
- // If no Avro schema was found, throw an exception
124
- throw new InvalidOperationException ( $ "Unsupported type for Avro deserialization: { targetType . Name } . " +
125
- "Avro deserialization requires a type with a static _SCHEMA field. " +
126
- "Consider using an alternative Deserializer." ) ;
127
- }
128
- catch ( Exception ex )
129
- {
130
- // Preserve the error message while wrapping in SerializationException for consistent error handling
131
- throw new System . Runtime . Serialization . SerializationException ( $ "Failed to deserialize { ( isKey ? "key" : "value" ) } data: { ex . Message } ", ex ) ;
90
+ throw new InvalidOperationException (
91
+ $ "Unsupported type for Avro deserialization: { targetType . Name } . " +
92
+ "Avro deserialization requires a type with a static _SCHEMA field. " +
93
+ "Consider using an alternative Deserializer." ) ;
132
94
}
95
+
96
+ using var stream = new MemoryStream ( data ) ;
97
+ var decoder = new BinaryDecoder ( stream ) ;
98
+ var reader = new SpecificDatumReader < object > ( schema , schema ) ;
99
+ return reader . Read ( null ! , decoder ) ;
100
+ }
101
+
102
+ /// <summary>
103
+ /// Gets the Avro schema for the specified type from its static _SCHEMA field.
104
+ /// </summary>
105
+ [ RequiresDynamicCode ( "Avro schema access requires reflection." ) ]
106
+ [ RequiresUnreferencedCode ( "Avro schema access requires reflection." ) ]
107
+ private Schema ? GetAvroSchema (
108
+ [ DynamicallyAccessedMembers ( DynamicallyAccessedMemberTypes . PublicFields ) ] Type payloadType )
109
+ {
110
+ var schemaField = payloadType . GetField ( "_SCHEMA" , BindingFlags . Public | BindingFlags . Static ) ;
111
+ return schemaField ? . GetValue ( null ) as Schema ;
133
112
}
134
- }
113
+ }
0 commit comments