diff --git a/modules/swagger-codegen/src/main/java/io/swagger/codegen/languages/PerlClientCodegen.java b/modules/swagger-codegen/src/main/java/io/swagger/codegen/languages/PerlClientCodegen.java index d2ced5c4281..ba03f3944c5 100644 --- a/modules/swagger-codegen/src/main/java/io/swagger/codegen/languages/PerlClientCodegen.java +++ b/modules/swagger-codegen/src/main/java/io/swagger/codegen/languages/PerlClientCodegen.java @@ -94,6 +94,7 @@ public class PerlClientCodegen extends DefaultCodegen implements CodegenConfig { supportingFiles.add(new SupportingFile("ApiClient.mustache", ("lib/WWW/" + moduleName).replace('/', File.separatorChar), "ApiClient.pm")); supportingFiles.add(new SupportingFile("Configuration.mustache", ("lib/WWW/" + moduleName).replace('/', File.separatorChar), "Configuration.pm")); supportingFiles.add(new SupportingFile("BaseObject.mustache", ("lib/WWW/" + moduleName).replace('/', File.separatorChar), "Object/BaseObject.pm")); + supportingFiles.add(new SupportingFile("ApiFactory.mustache", ("lib/WWW/" + moduleName).replace('/', File.separatorChar), "ApiFactory.pm")); } public CodegenType getTag() { diff --git a/modules/swagger-codegen/src/main/resources/perl/ApiFactory.mustache b/modules/swagger-codegen/src/main/resources/perl/ApiFactory.mustache new file mode 100644 index 00000000000..2ea2db72402 --- /dev/null +++ b/modules/swagger-codegen/src/main/resources/perl/ApiFactory.mustache @@ -0,0 +1,63 @@ +package WWW::{{moduleName}}::ApiFactory; + +use strict; +use warnings; +use utf8; + +use Carp; +use Module::Find; + +usesub WWW::{{moduleName}}::Object; + +use WWW::{{moduleName}}::ApiClient; + +=head1 Name + + WWW::{{moduleName}}::ApiFactory - constructs APIs to retrieve {{moduleName}} objects + +=head1 Synopsis + + package My::Petstore::App; + + use WWW::{{moduleName}}::ApiFactory; + + my $api_factory = WWW::{{moduleName}}::ApiFactory->new( base_url => 'http://petstore.swagger.io/v2', + ..., # other args for ApiClient constructor + ); + + # later... + my $pet_api = $api_factory->get_api('Pet'); + + # $pet_api isa WWW::{{moduleName}}::PetApi + + my $pet = $pet_api->get_pet_by_id(pet_id => $pet_id); + + # object attributes have proper accessors: + printf "Pet's name is %s", $pet->name; + + # change the value stored on the object: + $pet->name('Dave'); + +=cut + +# Load all the API classes and construct a lookup table at startup time +my %_apis = map { $_ =~ /^WWW::{{moduleName}}::(.*)$/; $1 => $_ } + grep {$_ =~ /Api$/} + usesub 'WWW::{{moduleName}}'; + +sub new { + my ($class, %p) = (shift, @_); + $p{api_client} = WWW::{{moduleName}}::ApiClient->new(%p); + return bless \%p, $class; +} + +sub get_api { + my ($self, $which) = @_; + croak "API not specified" unless $which; + my $api_class = $_apis{"${which}Api"} || croak "No known API for '$which'"; + return $api_class->new(api_client => $self->_api_client); +} + +sub _api_client { $_[0]->{api_client} } + +1; diff --git a/modules/swagger-codegen/src/main/resources/perl/BaseObject.mustache b/modules/swagger-codegen/src/main/resources/perl/BaseObject.mustache index 4e9a4699ed3..58c047f49a4 100644 --- a/modules/swagger-codegen/src/main/resources/perl/BaseObject.mustache +++ b/modules/swagger-codegen/src/main/resources/perl/BaseObject.mustache @@ -11,6 +11,8 @@ use Log::Any qw($log); use Date::Parse; use DateTime; +use base "Class::Accessor"; + # # diff --git a/modules/swagger-codegen/src/main/resources/perl/object.mustache b/modules/swagger-codegen/src/main/resources/perl/object.mustache index d4d6961c646..fc418e79ce4 100644 --- a/modules/swagger-codegen/src/main/resources/perl/object.mustache +++ b/modules/swagger-codegen/src/main/resources/perl/object.mustache @@ -31,6 +31,8 @@ my $attribute_map = { {{/hasMore}}{{/vars}} }; +__PACKAGE__->mk_accessors(keys %$attribute_map); + # new object sub new { my ($class, %args) = @_; diff --git a/samples/client/petstore/perl/lib/WWW/SwaggerClient/ApiFactory.pm b/samples/client/petstore/perl/lib/WWW/SwaggerClient/ApiFactory.pm new file mode 100644 index 00000000000..811031b06a3 --- /dev/null +++ b/samples/client/petstore/perl/lib/WWW/SwaggerClient/ApiFactory.pm @@ -0,0 +1,63 @@ +package WWW::SwaggerClient::ApiFactory; + +use strict; +use warnings; +use utf8; + +use Carp; +use Module::Find; + +usesub WWW::SwaggerClient::Object; + +use WWW::SwaggerClient::ApiClient; + +=head1 Name + + WWW::SwaggerClient::ApiFactory - constructs APIs to retrieve SwaggerClient objects + +=head1 Synopsis + + package My::Petstore::App; + + use WWW::SwaggerClient::ApiFactory; + + my $api_factory = WWW::SwaggerClient::ApiFactory->new( base_url => 'http://petstore.swagger.io/v2', + ..., # other args for ApiClient constructor + ); + + # later... + my $pet_api = $api_factory->get_api('Pet'); + + # $pet_api isa WWW::SwaggerClient::PetApi + + my $pet = $pet_api->get_pet_by_id(pet_id => $pet_id); + + # object attributes have proper accessors: + printf "Pet's name is %s", $pet->name; + + # change the value stored on the object: + $pet->name('Dave'); + +=cut + +# Load all the API classes and construct a lookup table at startup time +my %_apis = map { $_ =~ /^WWW::SwaggerClient::(.*)$/; $1 => $_ } + grep {$_ =~ /Api$/} + usesub 'WWW::SwaggerClient'; + +sub new { + my ($class, %p) = (shift, @_); + $p{api_client} = WWW::SwaggerClient::ApiClient->new(%p); + return bless \%p, $class; +} + +sub get_api { + my ($self, $which) = @_; + croak "API not specified" unless $which; + my $api_class = $_apis{"${which}Api"} || croak "No known API for '$which'"; + return $api_class->new(api_client => $self->_api_client); +} + +sub _api_client { $_[0]->{api_client} } + +1; diff --git a/samples/client/petstore/perl/lib/WWW/SwaggerClient/Object/ApiResponse.pm b/samples/client/petstore/perl/lib/WWW/SwaggerClient/Object/ApiResponse.pm index 02c36f17262..029aad32b9b 100644 --- a/samples/client/petstore/perl/lib/WWW/SwaggerClient/Object/ApiResponse.pm +++ b/samples/client/petstore/perl/lib/WWW/SwaggerClient/Object/ApiResponse.pm @@ -31,6 +31,8 @@ my $attribute_map = { 'message' => 'message' }; +__PACKAGE__->mk_accessors(keys %$attribute_map); + # new object sub new { my ($class, %args) = @_; diff --git a/samples/client/petstore/perl/lib/WWW/SwaggerClient/Object/BaseObject.pm b/samples/client/petstore/perl/lib/WWW/SwaggerClient/Object/BaseObject.pm index 1c17b55976c..fcdbe3fc14e 100644 --- a/samples/client/petstore/perl/lib/WWW/SwaggerClient/Object/BaseObject.pm +++ b/samples/client/petstore/perl/lib/WWW/SwaggerClient/Object/BaseObject.pm @@ -11,6 +11,8 @@ use Log::Any qw($log); use Date::Parse; use DateTime; +use base "Class::Accessor"; + # # diff --git a/samples/client/petstore/perl/lib/WWW/SwaggerClient/Object/Category.pm b/samples/client/petstore/perl/lib/WWW/SwaggerClient/Object/Category.pm index 2b2c0beceac..7aa9b642824 100644 --- a/samples/client/petstore/perl/lib/WWW/SwaggerClient/Object/Category.pm +++ b/samples/client/petstore/perl/lib/WWW/SwaggerClient/Object/Category.pm @@ -29,6 +29,8 @@ my $attribute_map = { 'name' => 'name' }; +__PACKAGE__->mk_accessors(keys %$attribute_map); + # new object sub new { my ($class, %args) = @_; diff --git a/samples/client/petstore/perl/lib/WWW/SwaggerClient/Object/Order.pm b/samples/client/petstore/perl/lib/WWW/SwaggerClient/Object/Order.pm index 14da4a5f3af..f7016179e3a 100644 --- a/samples/client/petstore/perl/lib/WWW/SwaggerClient/Object/Order.pm +++ b/samples/client/petstore/perl/lib/WWW/SwaggerClient/Object/Order.pm @@ -37,6 +37,8 @@ my $attribute_map = { 'complete' => 'complete' }; +__PACKAGE__->mk_accessors(keys %$attribute_map); + # new object sub new { my ($class, %args) = @_; diff --git a/samples/client/petstore/perl/lib/WWW/SwaggerClient/Object/Pet.pm b/samples/client/petstore/perl/lib/WWW/SwaggerClient/Object/Pet.pm index eb74ad3f368..20b9262e49f 100644 --- a/samples/client/petstore/perl/lib/WWW/SwaggerClient/Object/Pet.pm +++ b/samples/client/petstore/perl/lib/WWW/SwaggerClient/Object/Pet.pm @@ -37,6 +37,8 @@ my $attribute_map = { 'status' => 'status' }; +__PACKAGE__->mk_accessors(keys %$attribute_map); + # new object sub new { my ($class, %args) = @_; diff --git a/samples/client/petstore/perl/lib/WWW/SwaggerClient/Object/Tag.pm b/samples/client/petstore/perl/lib/WWW/SwaggerClient/Object/Tag.pm index 1b136d5fbfc..2c585b1ec80 100644 --- a/samples/client/petstore/perl/lib/WWW/SwaggerClient/Object/Tag.pm +++ b/samples/client/petstore/perl/lib/WWW/SwaggerClient/Object/Tag.pm @@ -29,6 +29,8 @@ my $attribute_map = { 'name' => 'name' }; +__PACKAGE__->mk_accessors(keys %$attribute_map); + # new object sub new { my ($class, %args) = @_; diff --git a/samples/client/petstore/perl/lib/WWW/SwaggerClient/Object/User.pm b/samples/client/petstore/perl/lib/WWW/SwaggerClient/Object/User.pm index 1beb8f0b201..271f5e91ef7 100644 --- a/samples/client/petstore/perl/lib/WWW/SwaggerClient/Object/User.pm +++ b/samples/client/petstore/perl/lib/WWW/SwaggerClient/Object/User.pm @@ -41,6 +41,8 @@ my $attribute_map = { 'user_status' => 'userStatus' }; +__PACKAGE__->mk_accessors(keys %$attribute_map); + # new object sub new { my ($class, %args) = @_; diff --git a/samples/client/petstore/perl/pom.xml b/samples/client/petstore/perl/pom.xml index 00d192b1e54..4034cf0a687 100644 --- a/samples/client/petstore/perl/pom.xml +++ b/samples/client/petstore/perl/pom.xml @@ -52,6 +52,19 @@ + + Test::More for ApiFactory + integration-test + + exec + + + perl + + t/03_api_factory.t + + + diff --git a/samples/client/petstore/perl/t/03_api_factory.t b/samples/client/petstore/perl/t/03_api_factory.t new file mode 100644 index 00000000000..f96bffa7a84 --- /dev/null +++ b/samples/client/petstore/perl/t/03_api_factory.t @@ -0,0 +1,34 @@ +use Test::More tests => 13; +use Test::Exception; + +use lib 'lib'; +use strict; +use warnings; + +use_ok('WWW::SwaggerClient::ApiFactory'); + +my $api_factory = WWW::SwaggerClient::ApiFactory->new('base_url' => 'http://testing'); +my $pet_api = $api_factory->get_api('Pet'); +isa_ok($pet_api, 'WWW::SwaggerClient::PetApi'); +is $pet_api->{api_client}->{base_url}, 'http://testing', 'get the proper base URL from api client'; + +$api_factory = WWW::SwaggerClient::ApiFactory->new; +$pet_api = $api_factory->get_api('Pet'); + +is $pet_api->{api_client}->{base_url}, 'http://petstore.swagger.io/v2', 'get the default base URL from api client'; + +# test accessor methods +my $pet_id = 10008; +# note - we don't need to 'use' these modules because they've already been loaded by ApiFactory +my ($category, $tag, $pet); +lives_ok { $category = WWW::SwaggerClient::Object::Category->new('id' => '22', 'name' => 'perl') } 'Category.pm loaded OK'; +lives_ok { $tag = WWW::SwaggerClient::Object::Tag->new('id' => '11', 'name' => 'just kidding') } 'Tag.pm loaded OK'; +lives_ok { $pet = WWW::SwaggerClient::Object::Pet->new('id' => $pet_id, 'name' => 'perl test', + "photoUrls" => ['123', 'oop'], 'tags' => [$tag], 'status' => 'pending', 'category' => $category) } 'Pet.pm loaded OK'; + +is $pet->id, '10008', 'got the proper pet id'; +is $pet->name, 'perl test', 'got the proper pet name'; +is $pet->category->id, '22', 'got the proper category id'; +is $pet->category->name, 'perl', 'got the proper category name'; +is $pet->tags->[0]->name, 'just kidding', 'got the proper tag name'; +is $pet->tags->[0]->id, '11', 'got the proper tag id';