Skip to content

Commit 7d1b4a3

Browse files
authored
fix wrong resolving of responses component using $ref (#277)
Signed-off-by: Jihyeon Gim <[email protected]>
1 parent 6677979 commit 7d1b4a3

File tree

3 files changed

+24
-3
lines changed

3 files changed

+24
-3
lines changed

lib/JSON/Validator/Schema/OpenAPIv3.pm

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ package JSON::Validator::Schema::OpenAPIv3;
22
use Mojo::Base 'JSON::Validator::Schema::Draft201909';
33

44
use JSON::Validator::Util qw(E data_type negotiate_content_type schema_type);
5-
use Mojo::JSON qw(false true);
5+
use Mojo::JSON qw(false true);
66
use Mojo::Path;
77

88
has moniker => 'openapiv3';
@@ -91,8 +91,8 @@ sub parameters_for_response {
9191
my $cache_key = "parameters_for_response:$method:$path:$status";
9292
return $self->{cache}{$cache_key} if $self->{cache}{$cache_key};
9393

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

9898
my @parameters;

t/openapiv3-basic.t

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,15 @@ subtest 'validate_response - accept' => sub {
127127
'negotiated content type';
128128
};
129129

130+
subtest 'validate_response - resusable response' => sub {
131+
$body = {accept => 'application/*'};
132+
@errors = $schema->validate_response([get => '/pets', 201], {body => \&body});
133+
is "@errors", '', 'valid accept';
134+
is_deeply $body,
135+
{accept => 'application/*', content_type => 'application/json', in => 'body', name => 'body', valid => 1},
136+
'negotiated content type';
137+
};
138+
130139
subtest 'validate_response - content_type' => sub {
131140
$body = {content_type => 'text/plain'};
132141
@errors = $schema->validate_response([get => '/pets'], {body => \&body});

t/spec/v3-petstore.json

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,9 @@
5757
"application/xml": { "schema": { "$ref": "#/components/schemas/Pets" } }
5858
}
5959
},
60+
"201": {
61+
"$ref": "#/components/responses/Pets"
62+
},
6063
"default": {
6164
"description": "unexpected error",
6265
"content": {
@@ -119,6 +122,15 @@
119122
"message": { "type": "string" }
120123
}
121124
}
125+
},
126+
"responses": {
127+
"Pets": {
128+
"description": "Reusable Pets response",
129+
"content": {
130+
"application/json": { "schema": { "$ref": "#/components/schemas/Pets" } },
131+
"application/xml": { "schema": { "$ref": "#/components/schemas/Pets" } }
132+
}
133+
}
122134
}
123135
}
124136
}

0 commit comments

Comments
 (0)