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 d2ced5c4281f..ba03f3944c51 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/ApiClient.mustache b/modules/swagger-codegen/src/main/resources/perl/ApiClient.mustache index 3a36ce140737..06aae8a0e55e 100644 --- a/modules/swagger-codegen/src/main/resources/perl/ApiClient.mustache +++ b/modules/swagger-codegen/src/main/resources/perl/ApiClient.mustache @@ -118,10 +118,12 @@ sub call_api { $self->{ua}->timeout($self->{http_timeout} || $WWW::{{moduleName}}::Configuration::http_timeout); $self->{ua}->agent($self->{http_user_agent} || $WWW::{{moduleName}}::Configuration::http_user_agent); + $log->debugf("REQUEST: %s", $_request->as_string); my $_response = $self->{ua}->request($_request); + $log->debugf("RESPONSE: %s", $_response->as_string); unless ($_response->is_success) { - croak("API Exception(".$_response->code."): ".$_response->message); + croak(sprintf "API Exception(%s): %s\n%s", $_response->code, $_response->message, $_response->content); } return $_response->content; @@ -294,13 +296,23 @@ sub get_api_key_with_prefix } } -# update hearder and query param based on authentication setting +# update header and query param based on authentication setting # # @param array $headerParams header parameters (by ref) # @param array $queryParams query parameters (by ref) # @param array $authSettings array of authentication scheme (e.g ['api_key']) sub update_params_for_auth { my ($self, $header_params, $query_params, $auth_settings) = @_; + + # we can defer to the application + if ($self->{auth_setup_handler} && ref($self->{auth_setup_handler}) eq 'CODE') { + $self->{auth_setup_handler}->( api_client => $self, + header_params => $header_params, + query_params => $query_params, + auth_settings => $auth_settings, # presumably this won't be defined if we're doing it this way + ); + return; + } return if (!defined($auth_settings) || scalar(@$auth_settings) == 0); 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 000000000000..fc309c91b9cf --- /dev/null +++ b/modules/swagger-codegen/src/main/resources/perl/ApiFactory.mustache @@ -0,0 +1,98 @@ +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}}'; + +=head1 new() + + All parameters are optional, and are passed to and stored on the api_client object. + + base_url: supply this to change the default base URL taken from the Swagger definition. + + auth_setup_handler: a coderef you can supply to set up authentication. + + The coderef receives a hashref with keys: api_client, query_params, header_params, auth_settings. + + my $api_factory = WWW::{{moduleName}}::ApiFactory->new( auth_setup_handler => \&setup_auth ); ); + + sub setup_auth { + my %p = @_; + $p{header_params}->{'X-SomeApp-FunkyKeyName'} = 'aaaaabbbbbcccccddddd'; + } + +=cut + +sub new { + my ($class, %p) = (shift, @_); + $p{api_client} = WWW::{{moduleName}}::ApiClient->new(%p); + return bless \%p, $class; +} + +=head1 get_api($which) + + Returns an API object of the requested type. + + $which is a nickname for the class: + + WWW::FooBarClient::BazApi has nickname 'Baz' + +=cut + +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); +} + +=head1 api_client() + + Returns the api_client object, should you ever need it. + +=cut + +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 4e9a4699ed3c..f2a58efdb68f 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", "Class::Data::Inheritable"); + # # @@ -18,6 +20,22 @@ use DateTime; #NOTE: This class is auto generated by the swagger code generator program. Do not edit the class manually. # +__PACKAGE__->mk_classdata('attribute_map'); +__PACKAGE__->mk_classdata('swagger_types'); + +# new object +sub new { + my ($class, %args) = @_; + + my $self = bless {}, $class; + + foreach my $attribute (keys %{$class->attribute_map}) { + my $args_key = $class->attribute_map->{$attribute}; + $self->$attribute( $args{ $args_key } ); + } + + return $self; +} # return perl hash sub to_hash { @@ -28,9 +46,9 @@ sub to_hash { sub TO_JSON { my $self = shift; my $_data = {}; - foreach my $_key (keys %{$self->get_attribute_map}) { + foreach my $_key (keys %{$self->attribute_map}) { if (defined $self->{$_key}) { - $_data->{$self->get_attribute_map->{$_key}} = $self->{$_key}; + $_data->{$self->attribute_map->{$_key}} = $self->{$_key}; } } return $_data; @@ -41,8 +59,8 @@ sub from_hash { my ($self, $hash) = @_; # loop through attributes and use swagger_types to deserialize the data - while ( my ($_key, $_type) = each %{$self->get_swagger_types} ) { - my $_json_attribute = $self->get_attribute_map->{$_key}; + while ( my ($_key, $_type) = each %{$self->swagger_types} ) { + my $_json_attribute = $self->attribute_map->{$_key}; if ($_type =~ /^array\[/i) { # array my $_subclass = substr($_type, 6, -1); my @_array = (); diff --git a/modules/swagger-codegen/src/main/resources/perl/api.mustache b/modules/swagger-codegen/src/main/resources/perl/api.mustache index e69e4e3066f6..d305d69b7faa 100644 --- a/modules/swagger-codegen/src/main/resources/perl/api.mustache +++ b/modules/swagger-codegen/src/main/resources/perl/api.mustache @@ -100,7 +100,7 @@ sub {{nickname}} { {{#formParams}}# form params if ( exists $args{'{{paramName}}'} ) { {{#isFile}}$form_params->{'{{baseName}}'} = [] unless defined $form_params->{'{{baseName}}'}; - push $form_params->{'{{baseName}}'}, $args{'{{paramName}}'}; + push @{$form_params->{'{{baseName}}'}}, $args{'{{paramName}}'}; {{/isFile}} {{^isFile}}$form_params->{'{{baseName}}'} = $self->{api_client}->to_form_value($args{'{{paramName}}'}); {{/isFile}} diff --git a/modules/swagger-codegen/src/main/resources/perl/object.mustache b/modules/swagger-codegen/src/main/resources/perl/object.mustache index d4d6961c646d..bbddd3c7f32c 100644 --- a/modules/swagger-codegen/src/main/resources/perl/object.mustache +++ b/modules/swagger-codegen/src/main/resources/perl/object.mustache @@ -21,37 +21,17 @@ use base "WWW::{{moduleName}}::Object::BaseObject"; #NOTE: This class is auto generated by the swagger code generator program. Do not edit the class manually. # -my $swagger_types = { +__PACKAGE__->swagger_types( { {{#vars}}'{{name}}' => '{{{datatype}}}'{{#hasMore}}, {{/hasMore}}{{/vars}} -}; +} ); -my $attribute_map = { +__PACKAGE__->attribute_map( { {{#vars}}'{{name}}' => '{{baseName}}'{{#hasMore}}, {{/hasMore}}{{/vars}} -}; +} ); -# new object -sub new { - my ($class, %args) = @_; - my $self = { - {{#vars}}#{{#description}}{{{description}}}{{/description}} - '{{name}}' => $args{'{{baseName}}'}{{#hasMore}}, - {{/hasMore}}{{/vars}} - }; - - return bless $self, $class; -} - -# get swagger type of the attribute -sub get_swagger_types { - return $swagger_types; -} - -# get attribute mappping -sub get_attribute_map { - return $attribute_map; -} +__PACKAGE__->mk_accessors(keys %{__PACKAGE__->attribute_map}); 1; {{/model}} diff --git a/samples/client/petstore/perl/lib/WWW/SwaggerClient/ApiClient.pm b/samples/client/petstore/perl/lib/WWW/SwaggerClient/ApiClient.pm index 6245329197a8..de4a3e2d2fb6 100644 --- a/samples/client/petstore/perl/lib/WWW/SwaggerClient/ApiClient.pm +++ b/samples/client/petstore/perl/lib/WWW/SwaggerClient/ApiClient.pm @@ -118,10 +118,12 @@ sub call_api { $self->{ua}->timeout($self->{http_timeout} || $WWW::SwaggerClient::Configuration::http_timeout); $self->{ua}->agent($self->{http_user_agent} || $WWW::SwaggerClient::Configuration::http_user_agent); + $log->debugf("REQUEST: %s", $_request->as_string); my $_response = $self->{ua}->request($_request); + $log->debugf("RESPONSE: %s", $_response->as_string); unless ($_response->is_success) { - croak("API Exception(".$_response->code."): ".$_response->message); + croak(sprintf "API Exception(%s): %s\n%s", $_response->code, $_response->message, $_response->content); } return $_response->content; @@ -153,6 +155,7 @@ sub to_query_value { } } + # Take value and turn it into a string suitable for inclusion in # the header. If it's a string, pass through unchanged # If it's a datetime object, format it in ISO8601 @@ -293,13 +296,23 @@ sub get_api_key_with_prefix } } -# update hearder and query param based on authentication setting +# update header and query param based on authentication setting # # @param array $headerParams header parameters (by ref) # @param array $queryParams query parameters (by ref) # @param array $authSettings array of authentication scheme (e.g ['api_key']) sub update_params_for_auth { my ($self, $header_params, $query_params, $auth_settings) = @_; + + # we can defer to the application + if ($self->{auth_setup_handler} && ref($self->{auth_setup_handler}) eq 'CODE') { + $self->{auth_setup_handler}->( api_client => $self, + header_params => $header_params, + query_params => $query_params, + auth_settings => $auth_settings, # presumably this won't be defined if we're doing it this way + ); + return; + } return if (!defined($auth_settings) || scalar(@$auth_settings) == 0); 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 000000000000..701a22603c53 --- /dev/null +++ b/samples/client/petstore/perl/lib/WWW/SwaggerClient/ApiFactory.pm @@ -0,0 +1,98 @@ +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'; + +=head1 new() + + All parameters are optional, and are passed to and stored on the api_client object. + + base_url: supply this to change the default base URL taken from the Swagger definition. + + auth_setup_handler: a coderef you can supply to set up authentication. + + The coderef receives a hashref with keys: api_client, query_params, header_params, auth_settings. + + my $api_factory = WWW::SwaggerClient::ApiFactory->new( auth_setup_handler => \&setup_auth ); ); + + sub setup_auth { + my %p = @_; + $p{header_params}->{'X-SomeApp-FunkyKeyName'} = 'aaaaabbbbbcccccddddd'; + } + +=cut + +sub new { + my ($class, %p) = (shift, @_); + $p{api_client} = WWW::SwaggerClient::ApiClient->new(%p); + return bless \%p, $class; +} + +=head1 get_api($which) + + Returns an API object of the requested type. + + $which is a nickname for the class: + + WWW::FooBarClient::BazApi has nickname 'Baz' + +=cut + +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); +} + +=head1 api_client() + + Returns the api_client object, should you ever need it. + +=cut + +sub api_client { $_[0]->{api_client} } + +1; 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 af1ac1c60174..fee2b06fb0ee 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", "Class::Data::Inheritable"); + # # @@ -18,8 +20,24 @@ use DateTime; #NOTE: This class is auto generated by the swagger code generator program. Do not edit the class manually. # +__PACKAGE__->mk_classdata('attribute_map'); +__PACKAGE__->mk_classdata('swagger_types'); -# return json string +# new object +sub new { + my ($class, %args) = @_; + + my $self = bless {}, $class; + + foreach my $attribute (keys %{$class->attribute_map}) { + my $args_key = $class->attribute_map->{$attribute}; + $self->$attribute( $args{ $args_key } ); + } + + return $self; +} + +# return perl hash sub to_hash { return decode_json(JSON->new->convert_blessed->encode( shift )); } @@ -28,36 +46,38 @@ sub to_hash { sub TO_JSON { my $self = shift; my $_data = {}; - foreach my $_key (keys %{$self->get_attribute_map}) { + foreach my $_key (keys %{$self->attribute_map}) { if (defined $self->{$_key}) { - $_data->{$self->get_attribute_map->{$_key}} = $self->{$_key}; + $_data->{$self->attribute_map->{$_key}} = $self->{$_key}; } } return $_data; } -# from json string +# from Perl hashref sub from_hash { my ($self, $hash) = @_; + # loop through attributes and use swagger_types to deserialize the data - while ( my ($_key, $_type) = each %{$self->get_swagger_types} ) { + while ( my ($_key, $_type) = each %{$self->swagger_types} ) { + my $_json_attribute = $self->attribute_map->{$_key}; if ($_type =~ /^array\[/i) { # array my $_subclass = substr($_type, 6, -1); my @_array = (); - foreach my $_element (@{$hash->{$self->get_attribute_map->{$_key}}}) { + foreach my $_element (@{$hash->{$_json_attribute}}) { push @_array, $self->_deserialize($_subclass, $_element); } $self->{$_key} = \@_array; - } elsif (defined $hash->{$_key}) { #hash(model), primitive, datetime - $self->{$_key} = $self->_deserialize($_type, $hash->{$_key}); + } elsif (exists $hash->{$_json_attribute}) { #hash(model), primitive, datetime + $self->{$_key} = $self->_deserialize($_type, $hash->{$_json_attribute}); } else { - $log->debugf("warning: %s not defined\n", $_key); + $log->debugf("Warning: %s (%s) does not exist in input hash\n", $_key, $_json_attribute); } } return $self; } - + # deserialize non-array data sub _deserialize { my ($self, $type, $data) = @_; 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 2b2c0beceac7..1e0629250377 100644 --- a/samples/client/petstore/perl/lib/WWW/SwaggerClient/Object/Category.pm +++ b/samples/client/petstore/perl/lib/WWW/SwaggerClient/Object/Category.pm @@ -19,37 +19,16 @@ use base "WWW::SwaggerClient::Object::BaseObject"; #NOTE: This class is auto generated by the swagger code generator program. Do not edit the class manually. # -my $swagger_types = { +__PACKAGE__->swagger_types( { 'id' => 'int', 'name' => 'string' -}; +} ); -my $attribute_map = { +__PACKAGE__->attribute_map( { 'id' => 'id', 'name' => 'name' -}; +} ); -# new object -sub new { - my ($class, %args) = @_; - my $self = { - # - 'id' => $args{'id'}, - # - 'name' => $args{'name'} - }; - - return bless $self, $class; -} - -# get swagger type of the attribute -sub get_swagger_types { - return $swagger_types; -} - -# get attribute mappping -sub get_attribute_map { - return $attribute_map; -} +__PACKAGE__->mk_accessors(keys %{__PACKAGE__->attribute_map}); 1; 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 14da4a5f3af2..70b3db2cfc00 100644 --- a/samples/client/petstore/perl/lib/WWW/SwaggerClient/Object/Order.pm +++ b/samples/client/petstore/perl/lib/WWW/SwaggerClient/Object/Order.pm @@ -19,53 +19,24 @@ use base "WWW::SwaggerClient::Object::BaseObject"; #NOTE: This class is auto generated by the swagger code generator program. Do not edit the class manually. # -my $swagger_types = { +__PACKAGE__->swagger_types( { 'id' => 'int', 'pet_id' => 'int', 'quantity' => 'int', 'ship_date' => 'DateTime', 'status' => 'string', 'complete' => 'boolean' -}; +} ); -my $attribute_map = { +__PACKAGE__->attribute_map( { 'id' => 'id', 'pet_id' => 'petId', 'quantity' => 'quantity', 'ship_date' => 'shipDate', 'status' => 'status', 'complete' => 'complete' -}; +} ); -# new object -sub new { - my ($class, %args) = @_; - my $self = { - # - 'id' => $args{'id'}, - # - 'pet_id' => $args{'petId'}, - # - 'quantity' => $args{'quantity'}, - # - 'ship_date' => $args{'shipDate'}, - #Order Status - 'status' => $args{'status'}, - # - 'complete' => $args{'complete'} - }; - - return bless $self, $class; -} - -# get swagger type of the attribute -sub get_swagger_types { - return $swagger_types; -} - -# get attribute mappping -sub get_attribute_map { - return $attribute_map; -} +__PACKAGE__->mk_accessors(keys %{__PACKAGE__->attribute_map}); 1; 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 eb74ad3f368a..3ff3f50b3999 100644 --- a/samples/client/petstore/perl/lib/WWW/SwaggerClient/Object/Pet.pm +++ b/samples/client/petstore/perl/lib/WWW/SwaggerClient/Object/Pet.pm @@ -19,53 +19,24 @@ use base "WWW::SwaggerClient::Object::BaseObject"; #NOTE: This class is auto generated by the swagger code generator program. Do not edit the class manually. # -my $swagger_types = { +__PACKAGE__->swagger_types( { 'id' => 'int', 'category' => 'Category', 'name' => 'string', 'photo_urls' => 'ARRAY[string]', 'tags' => 'ARRAY[Tag]', 'status' => 'string' -}; +} ); -my $attribute_map = { +__PACKAGE__->attribute_map( { 'id' => 'id', 'category' => 'category', 'name' => 'name', 'photo_urls' => 'photoUrls', 'tags' => 'tags', 'status' => 'status' -}; +} ); -# new object -sub new { - my ($class, %args) = @_; - my $self = { - # - 'id' => $args{'id'}, - # - 'category' => $args{'category'}, - # - 'name' => $args{'name'}, - # - 'photo_urls' => $args{'photoUrls'}, - # - 'tags' => $args{'tags'}, - #pet status in the store - 'status' => $args{'status'} - }; - - return bless $self, $class; -} - -# get swagger type of the attribute -sub get_swagger_types { - return $swagger_types; -} - -# get attribute mappping -sub get_attribute_map { - return $attribute_map; -} +__PACKAGE__->mk_accessors(keys %{__PACKAGE__->attribute_map}); 1; 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 1b136d5fbfc3..bf97f210a379 100644 --- a/samples/client/petstore/perl/lib/WWW/SwaggerClient/Object/Tag.pm +++ b/samples/client/petstore/perl/lib/WWW/SwaggerClient/Object/Tag.pm @@ -19,37 +19,16 @@ use base "WWW::SwaggerClient::Object::BaseObject"; #NOTE: This class is auto generated by the swagger code generator program. Do not edit the class manually. # -my $swagger_types = { +__PACKAGE__->swagger_types( { 'id' => 'int', 'name' => 'string' -}; +} ); -my $attribute_map = { +__PACKAGE__->attribute_map( { 'id' => 'id', 'name' => 'name' -}; +} ); -# new object -sub new { - my ($class, %args) = @_; - my $self = { - # - 'id' => $args{'id'}, - # - 'name' => $args{'name'} - }; - - return bless $self, $class; -} - -# get swagger type of the attribute -sub get_swagger_types { - return $swagger_types; -} - -# get attribute mappping -sub get_attribute_map { - return $attribute_map; -} +__PACKAGE__->mk_accessors(keys %{__PACKAGE__->attribute_map}); 1; 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 1beb8f0b2017..36fc7baf6ee5 100644 --- a/samples/client/petstore/perl/lib/WWW/SwaggerClient/Object/User.pm +++ b/samples/client/petstore/perl/lib/WWW/SwaggerClient/Object/User.pm @@ -19,7 +19,7 @@ use base "WWW::SwaggerClient::Object::BaseObject"; #NOTE: This class is auto generated by the swagger code generator program. Do not edit the class manually. # -my $swagger_types = { +__PACKAGE__->swagger_types( { 'id' => 'int', 'username' => 'string', 'first_name' => 'string', @@ -28,9 +28,9 @@ my $swagger_types = { 'password' => 'string', 'phone' => 'string', 'user_status' => 'int' -}; +} ); -my $attribute_map = { +__PACKAGE__->attribute_map( { 'id' => 'id', 'username' => 'username', 'first_name' => 'firstName', @@ -39,41 +39,8 @@ my $attribute_map = { 'password' => 'password', 'phone' => 'phone', 'user_status' => 'userStatus' -}; +} ); -# new object -sub new { - my ($class, %args) = @_; - my $self = { - # - 'id' => $args{'id'}, - # - 'username' => $args{'username'}, - # - 'first_name' => $args{'firstName'}, - # - 'last_name' => $args{'lastName'}, - # - 'email' => $args{'email'}, - # - 'password' => $args{'password'}, - # - 'phone' => $args{'phone'}, - #User Status - 'user_status' => $args{'userStatus'} - }; - - return bless $self, $class; -} - -# get swagger type of the attribute -sub get_swagger_types { - return $swagger_types; -} - -# get attribute mappping -sub get_attribute_map { - return $attribute_map; -} +__PACKAGE__->mk_accessors(keys %{__PACKAGE__->attribute_map}); 1; diff --git a/samples/client/petstore/perl/lib/WWW/SwaggerClient/PetApi.pm b/samples/client/petstore/perl/lib/WWW/SwaggerClient/PetApi.pm index 19a3d88b2994..26b9f42e620e 100644 --- a/samples/client/petstore/perl/lib/WWW/SwaggerClient/PetApi.pm +++ b/samples/client/petstore/perl/lib/WWW/SwaggerClient/PetApi.pm @@ -300,7 +300,7 @@ sub get_pet_by_id { # authentication setting, if any - my $auth_settings = ['api_key', 'petstore_auth']; + my $auth_settings = ['api_key']; # make the API Call my $response = $self->{api_client}->call_api($_resource_path, $_method, @@ -495,7 +495,7 @@ sub upload_file { }# form params if ( exists $args{'file'} ) { $form_params->{'file'} = [] unless defined $form_params->{'file'}; - push $form_params->{'file'}, $args{'file'}; + push @{$form_params->{'file'}}, $args{'file'}; } diff --git a/samples/client/petstore/perl/pom.xml b/samples/client/petstore/perl/pom.xml index 00d192b1e548..4034cf0a6871 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/01_pet_api.t b/samples/client/petstore/perl/t/01_pet_api.t index bbd1aae4c1df..5460c3282b26 100644 --- a/samples/client/petstore/perl/t/01_pet_api.t +++ b/samples/client/petstore/perl/t/01_pet_api.t @@ -1,4 +1,4 @@ -use Test::More tests => 35; +use Test::More tests => 37; use Test::Exception; use lib 'lib'; 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 000000000000..f96bffa7a842 --- /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';