From 695695478f497b347defec30fb58c9bf2c7a134d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Joaqu=C3=ADn=20Atria?= Date: Tue, 25 Jun 2024 11:29:40 +0100 Subject: [PATCH] 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 84cbf73c58d..67805602843 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 160f85ded50..3451b0b4d71 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 660ecf127dd..f844ef0f619 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";