Skip to content

Commit

Permalink
AVRO-1463 [Perl] Quietly validate undefined values (#2975)
Browse files Browse the repository at this point in the history
  • Loading branch information
jjatria authored Jun 25, 2024
1 parent 4958755 commit 6956954
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 5 deletions.
2 changes: 2 additions & 0 deletions lang/perl/Changes
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
13 changes: 9 additions & 4 deletions lang/perl/lib/Avro/Schema.pm
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand Down
8 changes: 7 additions & 1 deletion lang/perl/t/01_schema.t
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ use strict;
use warnings;

use Test::More;
plan tests => 130;
plan tests => 137;
use Test::Exception;
use_ok 'Avro::Schema';

Expand Down Expand Up @@ -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";
Expand Down

0 comments on commit 6956954

Please sign in to comment.