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

Fix head requests #105

Closed
wants to merge 2 commits into from
Closed
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
3 changes: 2 additions & 1 deletion lib/Mojolicious/Plugin/OpenAPI.pm
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,8 @@ sub _helper_get_spec {
$jp ||= [paths => $s->{'openapi.path'}];
}

push @$jp, lc $c->req->method if $jp and $path ne 'for_path'; # Internal for now
my $method = lc $c->req->method;
push @$jp, ( $method eq 'head' ? 'get' : $method ) if $jp and $path ne 'for_path'; # Internal for now
return $jp ? $self->validator->get($jp) : undef;
}

Expand Down
89 changes: 89 additions & 0 deletions t/head_request.t
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
use Mojo::Base -strict;
use Test::Mojo;
use Test::More;

make_app();
make_controller();

my $t = Test::Mojo->new('Myapp');

$t->head_ok('/api')->status_is(200);
$t->head_ok('/api/pets')->status_is(200);

done_testing;

sub make_app {
eval <<'HERE' or die $@;
package Myapp;
use Mojo::Base "Mojolicious";

sub startup {
my $app = shift;
$app->plugin("OpenAPI" => {url => "data://main/myapi.json"});
}

$ENV{"Myapp.pm"} = 1;
HERE
}

sub make_controller {
eval <<'HERE' or die $@;
package Myapp::Controller::Pet;
use Mojo::Base "Mojolicious::Controller";

sub list {

# Do not continue on invalid input and render a default 400
# error document.
my $c = shift->openapi->valid_input or return;

# $c->openapi->valid_input copies valid data to validation object,
# and the normal Mojolicious api works as well.
my $input = $c->validation->output;
my $age = $c->param("age"); # same as $input->{age}
my $body = $c->req->json; # same as $input->{body}

# $output will be validated by the OpenAPI spec before rendered
my $output = {pets => [{name => "kit-e-cat"}]};
$c->render(openapi => $output);
}

$ENV{"Myapp/Controller/Pet.pm"} = 1;
HERE
}

__DATA__
@@ myapi.json
{
"swagger": "2.0",
"info": { "version": "1.0", "title": "Some awesome API" },
"basePath": "/api",
"paths": {
"/pets": {
"get": {
"operationId": "getPets",
"x-mojo-name": "get_pets",
"x-mojo-to": "pet#list",
"summary": "Finds pets in the system",
"parameters": [
{"in": "body", "name": "body", "schema": {"type": "object"}},
{"in": "query", "name": "age", "type": "integer"}
],
"responses": {
"200": {
"description": "Pet response",
"schema": {
"type": "object",
"properties": {
"pets": {
"type": "array",
"items": { "type": "object" }
}
}
}
}
}
}
}
}
}