MediaMath / catalyst-controller-swagger

5 stars 5 forks source link

Error: "A base path must be supplied in the configuration" #4

Open hoppfrosch opened 8 years ago

hoppfrosch commented 8 years ago

It's rather a question than an error (maybe missing documentation?). I'm a bloody newbie on Swagger and on integrating swagger into catalyst - therefore bare with me ... ;-)

I tried to run your example within my current catalyst environment:

package MyProject::Controller::TestSwagger;
use base 'Catalyst::Controller::Swagger';
use Swagger qw(add_meta);

__PACKAGE__->config(
  'default'   => 'application/json',
  'stash_key' => 'rest',
  'map'   => {
'text/html'  => [ 'View', 'TT', ],
'text/xml'   => 'XML::Simple',
'application/json'   => 'JSON'
  },
  'swagger' => {
 'api_version'  => '0.0.1',
 'info'  => {
    'title'   => 'test project',
    'description' => 'test description',
 },
   }
);

add_meta {
action => 'test_one',
params => [
  { name => 'start', type => 'integer' }
],
 };

sub test_one_base :Chained('/') :PathPart('test_one') :CaptureArgs(2) {
  my ( $self, $c ) = @_;
}

sub test_one :Chained('test_one_base') :PathPart('foo') :Args(1) :Swagger {
  my ($self, $c) = @_;
  $c->response->body("test_one");
}
# A swagger route can be flagged to be swagger with the :Swagger attribute
sub test_two :Local :Swagger {
  my ($self, $c) = @_;
  $c->response->body('test_two');
}

1;

Doing so everything starts up without errors - and additional pathspecs are provided (/testswagger/api_docs/... , /testswagger/test_two/..., /test_one/*/*/foo/*)

Opening the page http://127.0.0.1:3001/testswagger/api_docsin my browser, I get the error:

Caught exception in BDBInfo2::Controller::TestSwagger->api_docs "A base path must be supplied in the configuration at D:/Usr/programme/Strawberry/perl/site/lib/Swagger/V12.pm line 40."

Any idea what's wrong? Maybe the given example might be expanded to address this ...

geira commented 8 years ago

t/lib/TestApp/Controller/Root.pm has an undocumented key base_path which seems interesting:

{
  swagger_version => '1.2',
  api_version     => '2.2.3',
  info            => {
    title         => 'test project',
    description   => 'test description',
  },
  resource_path => "/",
  base_path => 'http://localhost:3000', #required
  authorizations => {},
};
hoppfrosch commented 8 years ago

Thx for the hint - at least I got it running with this hint. Nevertheless the documentation should be updated with this information

logie17 commented 8 years ago

:+1: Thank you for taking a look a this project - we will be happy to accept and PRs and/or suggestions. I will also re-review the documentation and update as you suggest.

singerko commented 8 years ago

I guess that issue is in "package Swagger::V12":

35 unless (@{$self->configuration}{qw(basePath apiVersion resourcePath)}) { 36 @{$self->configuration}{qw(basePath apiVersion resourcePath)} = 37 delete @{$self->configuration}{qw(base_path api_version resource_path)}; 38 }

Because here is delete from configuration, so when method _build_swag_data is called 2nd time, values are set to null. For instance (i call 2 times /doc/api_doc). First call is ok and returns valid json. Second call (reload page) fail.

HTTP::Server::PSGI: Accepting connections at http://0:3001/ $VAR1 = { 'info' => { 'title' => 'test project', 'description' => 'test description' }, 'resourcePath' => undef, 'apiVersion' => '2.2.3', 'basePath' => '/' }; 6305ms INFO [HP_InfrasecRest.Controller.Root] HP_InfrasecRest::Controller::Root::auto:87 - doc/api_docs $VAR1 = { 'info' => { 'title' => 'test project', 'description' => 'test description' }, 'resourcePath' => undef, 'apiVersion' => undef, 'basePath' => undef }; 8338ms INFO [HP_InfrasecRest.Controller.Root] HP_InfrasecRest::Controller::Root::auto:87 - doc/api_docs 8340ms ERROR [Catalyst] Catalyst::finalize:2076 - Caught exception in HP_InfrasecRest::Controller::Doc->api_docs "A base path must be supplied in the configuration at /usr/share/perl5/vendor_perl/Swagger/V12.pm line 42." 8341ms ERROR [Catalyst] Catalyst::finalize:2076 - Caught exception in HP_InfrasecRest::Controller::Root->end "Can't call method "handle" on an undefined value at /home/erm_users/hpemaspe/web-data/infrasec.git/infrasec-portal-rest/FILES/bin/../lib/Core/Controller/Root.pm line 126."