Skip to content

Commit

Permalink
Fix validating "nullable" for "array" and "object"
Browse files Browse the repository at this point in the history
  • Loading branch information
Jan Henning Thorsen committed Apr 28, 2021
1 parent 120b79a commit 0bdda95
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 1 deletion.
1 change: 1 addition & 0 deletions Changes
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ Revision history for perl distribution JSON-Validator
4.17 Not Released
- Add add_default_response() to OpenAPIv2 and OpenAPIv3
- Add base_url() to OpenAPIv2 and OpenAPIv3
- Fix validating "nullable" for "array" and "object"

4.16 2021-03-24T08:57:46+0900
- Fix handling OpenAPIv2 "responses" $ref when bundling
Expand Down
6 changes: 6 additions & 0 deletions lib/JSON/Validator/Schema/OpenAPIv3.pm
Original file line number Diff line number Diff line change
Expand Up @@ -286,6 +286,11 @@ sub _validate_body {
return;
}

sub _validate_type_array {
my $self = shift;
return $_[2]->{nullable} && !defined $_[0] ? () : $self->SUPER::_validate_type_array(@_);
}

sub _validate_type_boolean {
my $self = shift;
return $_[2]->{nullable} && !defined $_[0] ? () : $self->SUPER::_validate_type_boolean(@_);
Expand All @@ -303,6 +308,7 @@ sub _validate_type_number {

sub _validate_type_object {
my ($self, $data, $path, $schema) = @_;
return if $schema->{nullable} && !defined $data;
return E $path, [object => type => data_type $data] if ref $data ne 'HASH';
return shift->SUPER::_validate_type_object(@_) unless $self->{validate_request} or $self->{validate_response};

Expand Down
16 changes: 15 additions & 1 deletion t/openapiv3-nullable.t
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,18 @@ for my $path (qw(/nullable-data /nullable-ref)) {
is "@errors", "", "$path - name is undef";
}

for my $extra ({}, undef) {
$body = {exists => 1, value => {extra => $extra, id => 42, name => undef}};
@errors = $schema->validate_response([get => '/nullable-data'], {body => \&body});
is "@errors", "", sprintf 'extra %s', $extra ? 'object' : 'null';
}

for my $stuff ([], undef) {
$body = {exists => 1, value => {stuff => $stuff, id => 42, name => undef}};
@errors = $schema->validate_response([get => '/nullable-data'], {body => \&body});
is "@errors", "", sprintf 'stuff %s', $stuff ? 'array' : 'null';
}

$schema = JSON::Validator->new->schema('data://main/issue-241.json')->schema;
$body = {exists => 1, value => {name => undef}};
@errors = $schema->validate_response([get => '/test'], {body => \&body});
Expand Down Expand Up @@ -55,8 +67,10 @@ __DATA__
"WithNullable": {
"required": [ "id", "name" ],
"properties": {
"extra": { "type": "object", "nullable": true },
"id": { "type": "integer", "format": "int64" },
"name": { "type": "string", "nullable": true }
"name": { "type": "string", "nullable": true },
"stuff": { "type": "array", "nullable": true }
}
},
"WithNullableRef": {
Expand Down

0 comments on commit 0bdda95

Please sign in to comment.