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

separate spec-generation from mojo-ising #27

Merged
merged 1 commit into from
Nov 8, 2018
Merged
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
91 changes: 61 additions & 30 deletions lib/Mojolicious/Plugin/Yancy.pm
Original file line number Diff line number Diff line change
Expand Up @@ -433,9 +433,11 @@ sub register {
}

# Add OpenAPI spec
my $spec = $self->_openapi_spec_from_schema( $config );
$self->_openapi_spec_add_mojo( $spec, $config );
my $openapi = $app->plugin( OpenAPI => {
route => $route->any( '/api' )->name( 'yancy.api' ),
spec => $self->_build_openapi_spec( $config ),
spec => $spec,
} );
$app->helper( 'yancy.openapi' => sub { $openapi } );

Expand All @@ -446,7 +448,64 @@ sub register {
$formats->{ tel } = sub { 1 };
}

sub _build_openapi_spec {
# mutates $spec
sub _openapi_spec_add_mojo {
my ( $self, $spec, $config ) = @_;
for my $path ( keys %{ $spec->{paths} } ) {
my ($name) = $path =~ m#^/([^/]+)#;
die "No 'name' found in '$path'" if !length $name;
my $pathspec = $spec->{paths}{ $path };
my $parameters = $pathspec->{parameters} || [];
my @path_params = grep 'path' eq ($_->{in} // ''), @$parameters;
die "No more than one path param handled" if @path_params > 1;
for my $method ( grep $_ ne 'parameters', keys %{ $pathspec } ) {
my $op_spec = $pathspec->{ $method };
if ( $method eq 'get' ) {
# heuristic: is per-item if have a param in path
if ( @path_params ) {
# per-item - GET = "read"
$op_spec->{ 'x-mojo-to' } = {
controller => $config->{api_controller},
action => 'get_item',
collection => $name,
id_field => $path_params[0]{name},
};
} else {
# per-collection - GET = "list"
$op_spec->{ 'x-mojo-to' } = {
controller => $config->{api_controller},
action => 'list_items',
collection => $name,
};
}
} elsif ( $method eq 'post' ) {
$op_spec->{ 'x-mojo-to' } = {
controller => $config->{api_controller},
action => 'add_item',
collection => $name,
};
} elsif ( $method eq 'put' ) {
die "'$method' method needs path-param" if !@path_params;
$op_spec->{ 'x-mojo-to' } = {
controller => $config->{api_controller},
action => 'set_item',
collection => $name,
id_field => $path_params[0]{name},
};
} elsif ( $method eq 'delete' ) {
die "'$method' method needs path-param" if !@path_params;
$op_spec->{ 'x-mojo-to' } = {
controller => $config->{api_controller},
action => 'delete_item',
collection => $name,
id_field => $path_params[0]{name},
};
}
}
}
}

sub _openapi_spec_from_schema {
my ( $self, $config ) = @_;
my ( %definitions, %paths );
for my $name ( keys %{ $config->{collections} } ) {
Expand All @@ -465,11 +524,6 @@ sub _build_openapi_spec {

$paths{ '/' . $name } = {
get => {
'x-mojo-to' => {
controller => $config->{api_controller},
action => 'list_items',
collection => $name,
},
parameters => [
{
name => '$limit',
Expand Down Expand Up @@ -524,11 +578,6 @@ sub _build_openapi_spec {
},
},
post => {
'x-mojo-to' => {
controller => $config->{api_controller},
action => 'add_item',
collection => $name,
},
parameters => [
{
name => "newItem",
Expand Down Expand Up @@ -566,12 +615,6 @@ sub _build_openapi_spec {
],

get => {
'x-mojo-to' => {
controller => $config->{api_controller},
action => 'get_item',
collection => $name,
id_field => $id_field,
},
description => "Fetch a single item",
responses => {
200 => {
Expand All @@ -590,12 +633,6 @@ sub _build_openapi_spec {
},

put => {
'x-mojo-to' => {
controller => $config->{api_controller},
action => 'set_item',
collection => $name,
id_field => $id_field,
},
description => "Update a single item",
parameters => [
{
Expand All @@ -622,12 +659,6 @@ sub _build_openapi_spec {
},

delete => {
'x-mojo-to' => {
controller => $config->{api_controller},
action => 'delete_item',
collection => $name,
id_field => $id_field,
},
description => "Delete a single item",
responses => {
204 => {
Expand Down