Skip to content

Commit

Permalink
Fix HEAD requests, closes #105
Browse files Browse the repository at this point in the history
  • Loading branch information
Jan Henning Thorsen committed Feb 14, 2019
1 parent c91ff97 commit 425e2ef
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 7 deletions.
1 change: 1 addition & 0 deletions Changes
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
Revision history for perl distribution Mojolicious-Plugin-OpenAPI

2.12 Not Released
- Fix HEAD requests #105
- Bump JSON::Validator to 3.06
- Improved handling of Accept header in OpenAPI v3 #104
Can now handle wildcards, such as application/* and */*, even though not
Expand Down
16 changes: 9 additions & 7 deletions lib/Mojolicious/Plugin/OpenAPI.pm
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,8 @@ sub _add_routes {
$self->_add_default_response($op_spec, $_) for @{$self->{default_response_codes}};

$r->to(ref $to eq 'ARRAY' ? @$to : $to) if $to;
$r->to({'openapi.path' => $openapi_path});
$r->to({'openapi.method' => $http_method});
$r->to({'openapi.path' => $openapi_path});
warn "[OpenAPI] Add route $http_method @{[$r->to_string]} (@{[$r->name // '']})\n" if DEBUG;

push @routes, $r;
Expand Down Expand Up @@ -197,15 +198,16 @@ sub _helper_get_spec {
my $path = shift // 'for_current';
my $self = _self($c);

# Get spec by valid JSON pointer
return $self->validator->get($path) if ref $path or $path =~ m!^/! or !length $path;

my $jp;
for my $s (reverse @{$c->match->stack}) {
$jp ||= [paths => $s->{'openapi.path'}];
}
# Find spec by current request
my ($stash) = grep { $_->{'openapi.path'} } reverse @{$c->match->stack};
return undef unless $stash;

push @$jp, lc $c->req->method if $jp and $path ne 'for_path'; # Internal for now
return $jp ? $self->validator->get($jp) : undef;
my $jp = [paths => $stash->{'openapi.path'}];
push @$jp, $stash->{'openapi.method'} if $path ne 'for_path'; # Internal for now
return $self->validator->get($jp);
}

sub _helper_reply {
Expand Down
5 changes: 5 additions & 0 deletions t/spec.t
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ $t->get_ok('/api/spec')->status_is(200)
->json_is('/op_spec/responses/200/description', 'Spec response.')
->json_is('/info/version', '0.8');

$t->get_ok('/api/user/1')->status_is(200)->content_is('{}');

$t->options_ok('/api/spec')->status_is(200)->json_is('/get/operationId', 'Spec');
$t->options_ok('/api/spec?method=get')->status_is(200)->json_is('/operationId', 'Spec');
$t->options_ok('/api/spec?method=post')->status_is(404);
Expand All @@ -28,6 +30,9 @@ $t->options_ok('/api/user/1')->status_is(200)->json_is('/get/operationId', 'user

$t->get_ok('/api')->status_is(200)->json_is('/basePath', '/api');

$t->head_ok('/api')->status_is(200);
$t->head_ok('/api/user/1')->status_is(200)->content_is('');

hook before_dispatch => sub {
my $c = shift;
$c->req->url->base->path('/whatever');
Expand Down

0 comments on commit 425e2ef

Please sign in to comment.