@@ -55,7 +55,7 @@ public class PowertoolsKafkaAvroSerializer : PowertoolsKafkaSerializerBase
5555 public PowertoolsKafkaAvroSerializer ( ) : base ( )
5656 {
5757 }
58-
58+
5959 /// <summary>
6060 /// Initializes a new instance of the <see cref="PowertoolsKafkaAvroSerializer"/> class
6161 /// with custom JSON serialization options.
@@ -64,7 +64,7 @@ public PowertoolsKafkaAvroSerializer() : base()
6464 public PowertoolsKafkaAvroSerializer ( JsonSerializerOptions jsonOptions ) : base ( jsonOptions )
6565 {
6666 }
67-
67+
6868 /// <summary>
6969 /// Initializes a new instance of the <see cref="PowertoolsKafkaAvroSerializer"/> class
7070 /// with a JSON serializer context for AOT-compatible serialization.
@@ -73,63 +73,49 @@ public PowertoolsKafkaAvroSerializer(JsonSerializerOptions jsonOptions) : base(j
7373 public PowertoolsKafkaAvroSerializer ( JsonSerializerContext serializerContext ) : base ( serializerContext )
7474 {
7575 }
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- }
9676
9777 /// <summary>
9878 /// Deserializes complex (non-primitive) types using Avro format.
79+ /// Requires types to have a public static _SCHEMA field.
9980 /// </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- /// <param name="schemaMetadata">Optional schema metadata for the data.</param>
104- /// <returns>The deserialized object.</returns>
10581 [ RequiresDynamicCode ( "Avro deserialization might require runtime code generation." ) ]
10682 [ RequiresUnreferencedCode ( "Avro deserialization might require types that cannot be statically analyzed." ) ]
107- protected override object ? DeserializeComplexTypeFormat ( byte [ ] data ,
108- [ DynamicallyAccessedMembers ( DynamicallyAccessedMemberTypes . PublicFields ) ]
83+ protected override object ? DeserializeComplexTypeFormat ( byte [ ] data ,
84+ [ DynamicallyAccessedMembers ( DynamicallyAccessedMemberTypes . PublicFields ) ]
10985 Type targetType , bool isKey , SchemaMetadata ? schemaMetadata = null )
11086 {
111- try
87+ var schema = GetAvroSchema ( targetType ) ;
88+ if ( schema == null )
11289 {
113- // Try to get Avro schema for the type
114- var schema = GetAvroSchema ( targetType ) ;
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." ) ;
94+ }
11595
116- if ( schema != null )
117- {
118- using var stream = new MemoryStream ( data ) ;
119- var decoder = new BinaryDecoder ( stream ) ;
120- var reader = new SpecificDatumReader < object > ( schema , schema ) ;
121- return reader . Read ( null ! , decoder ) ;
122- }
123-
124- // If no Avro schema was found, throw an exception
125- throw new InvalidOperationException ( $ "Unsupported type for Avro deserialization: { targetType . Name } . " +
126- "Avro deserialization requires a type with a static _SCHEMA field. " +
127- "Consider using an alternative Deserializer." ) ;
96+ try
97+ {
98+ using var stream = new MemoryStream ( data ) ;
99+ var decoder = new BinaryDecoder ( stream ) ;
100+ var reader = new SpecificDatumReader < object > ( schema , schema ) ;
101+ return reader . Read ( null ! , decoder ) ;
128102 }
129103 catch ( Exception ex )
130104 {
131- // Preserve the error message while wrapping in SerializationException for consistent error handling
132- throw new System . Runtime . Serialization . SerializationException ( $ "Failed to deserialize { ( isKey ? "key" : "value" ) } data: { ex . Message } ", ex ) ;
105+ throw new System . Runtime . Serialization . SerializationException (
106+ $ "Failed to deserialize { ( isKey ? "key" : "value" ) } data: { ex . Message } ", ex ) ;
133107 }
134108 }
135- }
109+
110+ /// <summary>
111+ /// Gets the Avro schema for the specified type from its static _SCHEMA field.
112+ /// </summary>
113+ [ RequiresDynamicCode ( "Avro schema access requires reflection." ) ]
114+ [ RequiresUnreferencedCode ( "Avro schema access requires reflection." ) ]
115+ private Schema ? GetAvroSchema (
116+ [ DynamicallyAccessedMembers ( DynamicallyAccessedMemberTypes . PublicFields ) ] Type payloadType )
117+ {
118+ var schemaField = payloadType . GetField ( "_SCHEMA" , BindingFlags . Public | BindingFlags . Static ) ;
119+ return schemaField ? . GetValue ( null ) as Schema ;
120+ }
121+ }
0 commit comments