Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

AVRO-1463 [Perl] Quietly validate undefined values #2975

Merged
merged 1 commit into from
Jun 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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