This is an automated email from the ASF dual-hosted git repository.

mgrigorov pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/avro.git


The following commit(s) were added to refs/heads/main by this push:
     new 695695478 AVRO-1463 [Perl] Quietly validate undefined values (#2975)
695695478 is described below

commit 695695478f497b347defec30fb58c9bf2c7a134d
Author: José Joaquín Atria <[email protected]>
AuthorDate: Tue Jun 25 11:29:40 2024 +0100

    AVRO-1463 [Perl] Quietly validate undefined values (#2975)
---
 lang/perl/Changes            |  2 ++
 lang/perl/lib/Avro/Schema.pm | 13 +++++++++----
 lang/perl/t/01_schema.t      |  8 +++++++-
 3 files changed, 18 insertions(+), 5 deletions(-)

diff --git a/lang/perl/Changes b/lang/perl/Changes
index 84cbf73c5..678056028 100644
--- a/lang/perl/Changes
+++ b/lang/perl/Changes
@@ -9,6 +9,8 @@ Revision history for Perl extension Avro
           by the spec.
         - Fixed an issue that meant the minimum accepted values
           for int and long types were off by one
+        - Silenced a spurious warning that was raised when
+          validating an undefined value for some data types
 
 1.00  Fri Jan 17 15:00:00 2014
         - Relicense under apache license 2.0
diff --git a/lang/perl/lib/Avro/Schema.pm b/lang/perl/lib/Avro/Schema.pm
index 160f85ded..3451b0b4d 100644
--- a/lang/perl/lib/Avro/Schema.pm
+++ b/lang/perl/lib/Avro/Schema.pm
@@ -276,6 +276,14 @@ sub is_data_valid {
     my $schema = shift;
     my $data = shift;
     my $type = $schema->{type};
+
+    if ($type eq 'null') {
+        return ! defined $data;
+    }
+
+    # undef isn't valid for any other types
+    return 0 unless defined $data;
+
     if ($type eq 'int') {
         no warnings;
         my $packed_int = pack "l", $data;
@@ -306,10 +314,7 @@ sub is_data_valid {
         $data =~ /^$RE{num}{real}$/ ? return 1 : 0;
     }
     if ($type eq "bytes" or $type eq "string") {
-        return 1 unless !defined $data or ref $data;
-    }
-    if ($type eq 'null') {
-        return defined $data ? 0 : 1;
+        return 1 unless ref $data;
     }
     if ($type eq 'boolean') {
         return 0 if ref $data; # sometimes risky
diff --git a/lang/perl/t/01_schema.t b/lang/perl/t/01_schema.t
index 660ecf127..f844ef0f6 100644
--- a/lang/perl/t/01_schema.t
+++ b/lang/perl/t/01_schema.t
@@ -19,7 +19,7 @@ use strict;
 use warnings;
 
 use Test::More;
-plan tests => 130;
+plan tests => 137;
 use Test::Exception;
 use_ok 'Avro::Schema';
 
@@ -457,6 +457,12 @@ EOJ
     isa_ok $s->fields->[0]{type}, 'Avro::Schema::Union';
 }
 
+## is_data_valid for primitives
+for my $type ( qw( int long float double string bytes boolean ) ) {
+    my $schema = Avro::Schema->parse(qq[{ "type": "$type" }]);
+    is $schema->is_data_valid(undef), 0, "$type is_data_valid undef";
+}
+
 sub match_ok {
     my ($w, $r, $msg) = @_;
     $msg ||= "match_ok";

Reply via email to