@@ -433,9 +433,11 @@ sub register {
433433 }
434434
435435 # Add OpenAPI spec
436+ my $spec = $self -> _openapi_spec_from_schema( $config );
437+ $self -> _openapi_spec_add_mojo( $spec , $config );
436438 my $openapi = $app -> plugin( OpenAPI => {
437439 route => $route -> any( ' /api' )-> name( ' yancy.api' ),
438- spec => $self -> _build_openapi_spec( $config ) ,
440+ spec => $spec ,
439441 } );
440442 $app -> helper( ' yancy.openapi' => sub { $openapi } );
441443
@@ -446,7 +448,64 @@ sub register {
446448 $formats -> { tel } = sub { 1 };
447449}
448450
449- sub _build_openapi_spec {
451+ # mutates $spec
452+ sub _openapi_spec_add_mojo {
453+ my ( $self , $spec , $config ) = @_ ;
454+ for my $path ( keys %{ $spec -> {paths } } ) {
455+ my ($name ) = $path =~ m # ^/([^/]+)# ;
456+ die " No 'name' found in '$path '" if !length $name ;
457+ my $pathspec = $spec -> {paths }{ $path };
458+ my $parameters = $pathspec -> {parameters } || [];
459+ my @path_params = grep ' path' eq ($_ -> {in } // ' ' ), @$parameters ;
460+ die " No more than one path param handled" if @path_params > 1;
461+ for my $method ( grep $_ ne ' parameters' , keys %{ $pathspec } ) {
462+ my $op_spec = $pathspec -> { $method };
463+ if ( $method eq ' get' ) {
464+ # heuristic: is per-item if have a param in path
465+ if ( @path_params ) {
466+ # per-item - GET = "read"
467+ $op_spec -> { ' x-mojo-to' } = {
468+ controller => $config -> {api_controller },
469+ action => ' get_item' ,
470+ collection => $name ,
471+ id_field => $path_params [0]{name },
472+ };
473+ } else {
474+ # per-collection - GET = "list"
475+ $op_spec -> { ' x-mojo-to' } = {
476+ controller => $config -> {api_controller },
477+ action => ' list_items' ,
478+ collection => $name ,
479+ };
480+ }
481+ } elsif ( $method eq ' post' ) {
482+ $op_spec -> { ' x-mojo-to' } = {
483+ controller => $config -> {api_controller },
484+ action => ' add_item' ,
485+ collection => $name ,
486+ };
487+ } elsif ( $method eq ' put' ) {
488+ die " '$method ' method needs path-param" if !@path_params ;
489+ $op_spec -> { ' x-mojo-to' } = {
490+ controller => $config -> {api_controller },
491+ action => ' set_item' ,
492+ collection => $name ,
493+ id_field => $path_params [0]{name },
494+ };
495+ } elsif ( $method eq ' delete' ) {
496+ die " '$method ' method needs path-param" if !@path_params ;
497+ $op_spec -> { ' x-mojo-to' } = {
498+ controller => $config -> {api_controller },
499+ action => ' delete_item' ,
500+ collection => $name ,
501+ id_field => $path_params [0]{name },
502+ };
503+ }
504+ }
505+ }
506+ }
507+
508+ sub _openapi_spec_from_schema {
450509 my ( $self , $config ) = @_ ;
451510 my ( %definitions , %paths );
452511 for my $name ( keys %{ $config -> {collections } } ) {
@@ -465,11 +524,6 @@ sub _build_openapi_spec {
465524
466525 $paths { ' /' . $name } = {
467526 get => {
468- ' x-mojo-to' => {
469- controller => $config -> {api_controller },
470- action => ' list_items' ,
471- collection => $name ,
472- },
473527 parameters => [
474528 {
475529 name => ' $limit' ,
@@ -524,11 +578,6 @@ sub _build_openapi_spec {
524578 },
525579 },
526580 post => {
527- ' x-mojo-to' => {
528- controller => $config -> {api_controller },
529- action => ' add_item' ,
530- collection => $name ,
531- },
532581 parameters => [
533582 {
534583 name => " newItem" ,
@@ -566,12 +615,6 @@ sub _build_openapi_spec {
566615 ],
567616
568617 get => {
569- ' x-mojo-to' => {
570- controller => $config -> {api_controller },
571- action => ' get_item' ,
572- collection => $name ,
573- id_field => $id_field ,
574- },
575618 description => " Fetch a single item" ,
576619 responses => {
577620 200 => {
@@ -590,12 +633,6 @@ sub _build_openapi_spec {
590633 },
591634
592635 put => {
593- ' x-mojo-to' => {
594- controller => $config -> {api_controller },
595- action => ' set_item' ,
596- collection => $name ,
597- id_field => $id_field ,
598- },
599636 description => " Update a single item" ,
600637 parameters => [
601638 {
@@ -622,12 +659,6 @@ sub _build_openapi_spec {
622659 },
623660
624661 delete => {
625- ' x-mojo-to' => {
626- controller => $config -> {api_controller },
627- action => ' delete_item' ,
628- collection => $name ,
629- id_field => $id_field ,
630- },
631662 description => " Delete a single item" ,
632663 responses => {
633664 204 => {
0 commit comments