@@ -45,37 +45,55 @@ public class SpecificDatumReader<T> extends GenericDatumReader<T> {
4545
4646 public SpecificDatumReader () {
4747 this (null , null , SpecificData .get ());
48+ initializeTrustedPackages ();
4849 }
4950
5051 /** Construct for reading instances of a class. */
5152 public SpecificDatumReader (Class <T > c ) {
5253 this (SpecificData .getForClass (c ));
5354 setSchema (getSpecificData ().getSchema (c ));
55+ initializeTrustedPackages ();
5456 }
5557
5658 /** Construct where the writer's and reader's schemas are the same. */
5759 public SpecificDatumReader (Schema schema ) {
5860 this (schema , schema , SpecificData .getForSchema (schema ));
61+ initializeTrustedPackages ();
5962 }
6063
6164 /** Construct given writer's and reader's schema. */
6265 public SpecificDatumReader (Schema writer , Schema reader ) {
6366 this (writer , reader , SpecificData .getForSchema (reader ));
67+ initializeTrustedPackages ();
6468 }
6569
6670 /**
6771 * Construct given writer's schema, reader's schema, and a {@link SpecificData}.
6872 */
6973 public SpecificDatumReader (Schema writer , Schema reader , SpecificData data ) {
7074 super (writer , reader , data );
71- trustedPackages . addAll ( Arrays . asList ( SERIALIZABLE_PACKAGES ) );
75+ initializeTrustedPackages ( );
7276 }
7377
7478 /** Construct given a {@link SpecificData}. */
7579 public SpecificDatumReader (SpecificData data ) {
7680 super (data );
81+ initializeTrustedPackages ();
7782 }
78-
83+
84+ /**
85+ * Initializes the trusted packages list by adding values from the system property
86+ * `org.apache.avro.SERIALIZABLE_PACKAGES`. This ensures that all constructors
87+ * of `SpecificDatumReader` correctly populate the `trustedPackages` list.
88+ *
89+ * Without this initialization, certain constructors may not add the expected
90+ * packages to the trusted list, leading to security check failures when deserializing
91+ * objects from custom packages.
92+ */
93+ private void initializeTrustedPackages () {
94+ trustedPackages .addAll (Arrays .asList (SERIALIZABLE_PACKAGES ));
95+ }
96+
7997 /** Return the contained {@link SpecificData}. */
8098 public SpecificData getSpecificData () {
8199 return (SpecificData ) getData ();
0 commit comments