Skip to content

Commit

Permalink
fix wrong resolving of responses component using $ref
Browse files Browse the repository at this point in the history
Signed-off-by: Jihyeon Gim <[email protected]>
  • Loading branch information
potatogim committed Oct 12, 2024
1 parent 6677979 commit 03d222c
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 2 deletions.
3 changes: 1 addition & 2 deletions lib/JSON/Validator/Schema/OpenAPIv3.pm
Original file line number Diff line number Diff line change
Expand Up @@ -91,8 +91,7 @@ sub parameters_for_response {
my $cache_key = "parameters_for_response:$method:$path:$status";
return $self->{cache}{$cache_key} if $self->{cache}{$cache_key};

my $responses = $self->get([paths => $path, $method, 'responses']);
my $response = $responses->{$status} || $responses->{default};
my $response = $self->get([paths => $path, $method, 'responses', $status]) || $self->get([paths => $path, $method, 'responses', 'default']);
return undef unless $response;

my @parameters;
Expand Down
9 changes: 9 additions & 0 deletions t/openapiv3-basic.t
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,15 @@ subtest 'validate_response - accept' => sub {
'negotiated content type';
};

subtest 'validate_response - resusable response' => sub {
$body = {accept => 'application/*'};
@errors = $schema->validate_response([get => '/pets', 201], {body => \&body});
is "@errors", '', 'valid accept';
is_deeply $body,
{accept => 'application/*', content_type => 'application/json', in => 'body', name => 'body', valid => 1},
'negotiated content type';
};

subtest 'validate_response - content_type' => sub {
$body = {content_type => 'text/plain'};
@errors = $schema->validate_response([get => '/pets'], {body => \&body});
Expand Down
12 changes: 12 additions & 0 deletions t/spec/v3-petstore.json
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,9 @@
"application/xml": { "schema": { "$ref": "#/components/schemas/Pets" } }
}
},
"201": {
"$ref": "#/components/responses/Pets"
},
"default": {
"description": "unexpected error",
"content": {
Expand Down Expand Up @@ -119,6 +122,15 @@
"message": { "type": "string" }
}
}
},
"responses": {
"Pets": {
"description": "Reusable Pets response",
"content": {
"application/json": { "schema": { "$ref": "#/components/schemas/Pets" } },
"application/xml": { "schema": { "$ref": "#/components/schemas/Pets" } }
}
}
}
}
}

0 comments on commit 03d222c

Please sign in to comment.