From 8428e7963bde1269d59d43b7a88f9b3d906e87b1 Mon Sep 17 00:00:00 2001 From: Dave Baird Date: Tue, 3 Nov 2015 16:46:47 +0100 Subject: [PATCH 1/5] Rebuild perl petstore client after previous updates At least 2 previous updates were committed without rebuilding the perl petstore client. One was my fault (BaseObject.pm changes), the other changes come from an upstream update or updates I can't identify. --- .../perl/lib/WWW/SwaggerClient/ApiClient.pm | 1 + .../WWW/SwaggerClient/Object/ApiResponse.pm | 59 ++++++++++++++++++ .../WWW/SwaggerClient/Object/BaseObject.pm | 16 ++--- .../perl/lib/WWW/SwaggerClient/PetApi.pm | 61 +++++++++++++------ .../perl/lib/WWW/SwaggerClient/StoreApi.pm | 17 ++++-- .../perl/lib/WWW/SwaggerClient/UserApi.pm | 58 +++++++++++++----- samples/client/petstore/perl/t/01_pet_api.t | 6 +- 7 files changed, 169 insertions(+), 49 deletions(-) create mode 100644 samples/client/petstore/perl/lib/WWW/SwaggerClient/Object/ApiResponse.pm diff --git a/samples/client/petstore/perl/lib/WWW/SwaggerClient/ApiClient.pm b/samples/client/petstore/perl/lib/WWW/SwaggerClient/ApiClient.pm index 6245329197a8..f75bf24e0310 100644 --- a/samples/client/petstore/perl/lib/WWW/SwaggerClient/ApiClient.pm +++ b/samples/client/petstore/perl/lib/WWW/SwaggerClient/ApiClient.pm @@ -153,6 +153,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 diff --git a/samples/client/petstore/perl/lib/WWW/SwaggerClient/Object/ApiResponse.pm b/samples/client/petstore/perl/lib/WWW/SwaggerClient/Object/ApiResponse.pm new file mode 100644 index 000000000000..02c36f172620 --- /dev/null +++ b/samples/client/petstore/perl/lib/WWW/SwaggerClient/Object/ApiResponse.pm @@ -0,0 +1,59 @@ +package WWW::SwaggerClient::Object::ApiResponse; + +require 5.6.0; +use strict; +use warnings; +use utf8; +use JSON qw(decode_json); +use Data::Dumper; +use Module::Runtime qw(use_module); +use Log::Any qw($log); +use Date::Parse; +use DateTime; + +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 = { + 'code' => 'int', + 'type' => 'string', + 'message' => 'string' +}; + +my $attribute_map = { + 'code' => 'code', + 'type' => 'type', + 'message' => 'message' +}; + +# new object +sub new { + my ($class, %args) = @_; + my $self = { + # + 'code' => $args{'code'}, + # + 'type' => $args{'type'}, + # + 'message' => $args{'message'} + }; + + 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; +} + +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..1c17b55976c8 100644 --- a/samples/client/petstore/perl/lib/WWW/SwaggerClient/Object/BaseObject.pm +++ b/samples/client/petstore/perl/lib/WWW/SwaggerClient/Object/BaseObject.pm @@ -19,7 +19,7 @@ use DateTime; # -# return json string +# return perl hash sub to_hash { return decode_json(JSON->new->convert_blessed->encode( shift )); } @@ -36,28 +36,30 @@ sub TO_JSON { 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} ) { + my $_json_attribute = $self->get_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/PetApi.pm b/samples/client/petstore/perl/lib/WWW/SwaggerClient/PetApi.pm index 19a3d88b2994..c8f0fc98ff30 100644 --- a/samples/client/petstore/perl/lib/WWW/SwaggerClient/PetApi.pm +++ b/samples/client/petstore/perl/lib/WWW/SwaggerClient/PetApi.pm @@ -52,13 +52,18 @@ sub new { # # Update an existing pet # -# @param Pet $body Pet object that needs to be added to the store (optional) +# @param Pet $body Pet object that needs to be added to the store (required) # @return void # sub update_pet { my ($self, %args) = @_; + # verify the required parameter 'body' is set + unless (exists $args{'body'}) { + croak("Missing the required parameter 'body' when calling update_pet"); + } + # parse inputs my $_resource_path = '/pet'; @@ -70,7 +75,7 @@ sub update_pet { my $form_params = {}; # 'Accept' and 'Content-Type' header - my $_header_accept = $self->{api_client}->select_header_accept('application/json', 'application/xml'); + my $_header_accept = $self->{api_client}->select_header_accept('application/xml', 'application/json'); if ($_header_accept) { $header_params->{'Accept'} = $_header_accept; } @@ -102,13 +107,18 @@ sub update_pet { # # Add a new pet to the store # -# @param Pet $body Pet object that needs to be added to the store (optional) +# @param Pet $body Pet object that needs to be added to the store (required) # @return void # sub add_pet { my ($self, %args) = @_; + # verify the required parameter 'body' is set + unless (exists $args{'body'}) { + croak("Missing the required parameter 'body' when calling add_pet"); + } + # parse inputs my $_resource_path = '/pet'; @@ -120,7 +130,7 @@ sub add_pet { my $form_params = {}; # 'Accept' and 'Content-Type' header - my $_header_accept = $self->{api_client}->select_header_accept('application/json', 'application/xml'); + my $_header_accept = $self->{api_client}->select_header_accept('application/xml', 'application/json'); if ($_header_accept) { $header_params->{'Accept'} = $_header_accept; } @@ -152,13 +162,18 @@ sub add_pet { # # Finds Pets by status # -# @param ARRAY[string] $status Status values that need to be considered for filter (optional) +# @param ARRAY[string] $status Status values that need to be considered for filter (required) # @return ARRAY[Pet] # sub find_pets_by_status { my ($self, %args) = @_; + # verify the required parameter 'status' is set + unless (exists $args{'status'}) { + croak("Missing the required parameter 'status' when calling find_pets_by_status"); + } + # parse inputs my $_resource_path = '/pet/findByStatus'; @@ -170,7 +185,7 @@ sub find_pets_by_status { my $form_params = {}; # 'Accept' and 'Content-Type' header - my $_header_accept = $self->{api_client}->select_header_accept('application/json', 'application/xml'); + my $_header_accept = $self->{api_client}->select_header_accept('application/xml', 'application/json'); if ($_header_accept) { $header_params->{'Accept'} = $_header_accept; } @@ -205,13 +220,18 @@ sub find_pets_by_status { # # Finds Pets by tags # -# @param ARRAY[string] $tags Tags to filter by (optional) +# @param ARRAY[string] $tags Tags to filter by (required) # @return ARRAY[Pet] # sub find_pets_by_tags { my ($self, %args) = @_; + # verify the required parameter 'tags' is set + unless (exists $args{'tags'}) { + croak("Missing the required parameter 'tags' when calling find_pets_by_tags"); + } + # parse inputs my $_resource_path = '/pet/findByTags'; @@ -223,7 +243,7 @@ sub find_pets_by_tags { my $form_params = {}; # 'Accept' and 'Content-Type' header - my $_header_accept = $self->{api_client}->select_header_accept('application/json', 'application/xml'); + my $_header_accept = $self->{api_client}->select_header_accept('application/xml', 'application/json'); if ($_header_accept) { $header_params->{'Accept'} = $_header_accept; } @@ -258,7 +278,7 @@ sub find_pets_by_tags { # # Find pet by ID # -# @param int $pet_id ID of pet that needs to be fetched (required) +# @param int $pet_id ID of pet to return (required) # @return Pet # sub get_pet_by_id { @@ -281,7 +301,7 @@ sub get_pet_by_id { my $form_params = {}; # 'Accept' and 'Content-Type' header - my $_header_accept = $self->{api_client}->select_header_accept('application/json', 'application/xml'); + my $_header_accept = $self->{api_client}->select_header_accept('application/xml', 'application/json'); if ($_header_accept) { $header_params->{'Accept'} = $_header_accept; } @@ -300,7 +320,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, @@ -318,7 +338,7 @@ sub get_pet_by_id { # # Updates a pet in the store with form data # -# @param string $pet_id ID of pet that needs to be updated (required) +# @param int $pet_id ID of pet that needs to be updated (required) # @param string $name Updated name of the pet (optional) # @param string $status Updated status of the pet (optional) # @return void @@ -343,7 +363,7 @@ sub update_pet_with_form { my $form_params = {}; # 'Accept' and 'Content-Type' header - my $_header_accept = $self->{api_client}->select_header_accept('application/json', 'application/xml'); + my $_header_accept = $self->{api_client}->select_header_accept('application/xml', 'application/json'); if ($_header_accept) { $header_params->{'Accept'} = $_header_accept; } @@ -411,7 +431,7 @@ sub delete_pet { my $form_params = {}; # 'Accept' and 'Content-Type' header - my $_header_accept = $self->{api_client}->select_header_accept('application/json', 'application/xml'); + my $_header_accept = $self->{api_client}->select_header_accept('application/xml', 'application/json'); if ($_header_accept) { $header_params->{'Accept'} = $_header_accept; } @@ -451,7 +471,7 @@ sub delete_pet { # @param int $pet_id ID of pet to update (required) # @param string $additional_metadata Additional data to pass to server (optional) # @param file $file file to upload (optional) -# @return void +# @return ApiResponse # sub upload_file { my ($self, %args) = @_; @@ -473,7 +493,7 @@ sub upload_file { my $form_params = {}; # 'Accept' and 'Content-Type' header - my $_header_accept = $self->{api_client}->select_header_accept('application/json', 'application/xml'); + my $_header_accept = $self->{api_client}->select_header_accept('application/json'); if ($_header_accept) { $header_params->{'Accept'} = $_header_accept; } @@ -506,11 +526,14 @@ sub upload_file { my $auth_settings = ['petstore_auth']; # make the API Call - - $self->{api_client}->call_api($_resource_path, $_method, + my $response = $self->{api_client}->call_api($_resource_path, $_method, $query_params, $form_params, $header_params, $_body_data, $auth_settings); - return; + if (!$response) { + return; + } + my $_response_object = $self->{api_client}->deserialize('ApiResponse', $response); + return $_response_object; } diff --git a/samples/client/petstore/perl/lib/WWW/SwaggerClient/StoreApi.pm b/samples/client/petstore/perl/lib/WWW/SwaggerClient/StoreApi.pm index a2ac9010e779..047c4624587e 100644 --- a/samples/client/petstore/perl/lib/WWW/SwaggerClient/StoreApi.pm +++ b/samples/client/petstore/perl/lib/WWW/SwaggerClient/StoreApi.pm @@ -69,7 +69,7 @@ sub get_inventory { my $form_params = {}; # 'Accept' and 'Content-Type' header - my $_header_accept = $self->{api_client}->select_header_accept('application/json', 'application/xml'); + my $_header_accept = $self->{api_client}->select_header_accept('application/json'); if ($_header_accept) { $header_params->{'Accept'} = $_header_accept; } @@ -101,13 +101,18 @@ sub get_inventory { # # Place an order for a pet # -# @param Order $body order placed for purchasing the pet (optional) +# @param Order $body order placed for purchasing the pet (required) # @return Order # sub place_order { my ($self, %args) = @_; + # verify the required parameter 'body' is set + unless (exists $args{'body'}) { + croak("Missing the required parameter 'body' when calling place_order"); + } + # parse inputs my $_resource_path = '/store/order'; @@ -119,7 +124,7 @@ sub place_order { my $form_params = {}; # 'Accept' and 'Content-Type' header - my $_header_accept = $self->{api_client}->select_header_accept('application/json', 'application/xml'); + my $_header_accept = $self->{api_client}->select_header_accept('application/xml', 'application/json'); if ($_header_accept) { $header_params->{'Accept'} = $_header_accept; } @@ -154,7 +159,7 @@ sub place_order { # # Find purchase order by ID # -# @param string $order_id ID of pet that needs to be fetched (required) +# @param int $order_id ID of pet that needs to be fetched (required) # @return Order # sub get_order_by_id { @@ -177,7 +182,7 @@ sub get_order_by_id { my $form_params = {}; # 'Accept' and 'Content-Type' header - my $_header_accept = $self->{api_client}->select_header_accept('application/json', 'application/xml'); + my $_header_accept = $self->{api_client}->select_header_accept('application/xml', 'application/json'); if ($_header_accept) { $header_params->{'Accept'} = $_header_accept; } @@ -237,7 +242,7 @@ sub delete_order { my $form_params = {}; # 'Accept' and 'Content-Type' header - my $_header_accept = $self->{api_client}->select_header_accept('application/json', 'application/xml'); + my $_header_accept = $self->{api_client}->select_header_accept('application/xml', 'application/json'); if ($_header_accept) { $header_params->{'Accept'} = $_header_accept; } diff --git a/samples/client/petstore/perl/lib/WWW/SwaggerClient/UserApi.pm b/samples/client/petstore/perl/lib/WWW/SwaggerClient/UserApi.pm index e1088a0839e0..f70c20136a41 100644 --- a/samples/client/petstore/perl/lib/WWW/SwaggerClient/UserApi.pm +++ b/samples/client/petstore/perl/lib/WWW/SwaggerClient/UserApi.pm @@ -52,13 +52,18 @@ sub new { # # Create user # -# @param User $body Created user object (optional) +# @param User $body Created user object (required) # @return void # sub create_user { my ($self, %args) = @_; + # verify the required parameter 'body' is set + unless (exists $args{'body'}) { + croak("Missing the required parameter 'body' when calling create_user"); + } + # parse inputs my $_resource_path = '/user'; @@ -70,7 +75,7 @@ sub create_user { my $form_params = {}; # 'Accept' and 'Content-Type' header - my $_header_accept = $self->{api_client}->select_header_accept('application/json', 'application/xml'); + my $_header_accept = $self->{api_client}->select_header_accept('application/xml', 'application/json'); if ($_header_accept) { $header_params->{'Accept'} = $_header_accept; } @@ -102,13 +107,18 @@ sub create_user { # # Creates list of users with given input array # -# @param ARRAY[User] $body List of user object (optional) +# @param ARRAY[User] $body List of user object (required) # @return void # sub create_users_with_array_input { my ($self, %args) = @_; + # verify the required parameter 'body' is set + unless (exists $args{'body'}) { + croak("Missing the required parameter 'body' when calling create_users_with_array_input"); + } + # parse inputs my $_resource_path = '/user/createWithArray'; @@ -120,7 +130,7 @@ sub create_users_with_array_input { my $form_params = {}; # 'Accept' and 'Content-Type' header - my $_header_accept = $self->{api_client}->select_header_accept('application/json', 'application/xml'); + my $_header_accept = $self->{api_client}->select_header_accept('application/xml', 'application/json'); if ($_header_accept) { $header_params->{'Accept'} = $_header_accept; } @@ -152,13 +162,18 @@ sub create_users_with_array_input { # # Creates list of users with given input array # -# @param ARRAY[User] $body List of user object (optional) +# @param ARRAY[User] $body List of user object (required) # @return void # sub create_users_with_list_input { my ($self, %args) = @_; + # verify the required parameter 'body' is set + unless (exists $args{'body'}) { + croak("Missing the required parameter 'body' when calling create_users_with_list_input"); + } + # parse inputs my $_resource_path = '/user/createWithList'; @@ -170,7 +185,7 @@ sub create_users_with_list_input { my $form_params = {}; # 'Accept' and 'Content-Type' header - my $_header_accept = $self->{api_client}->select_header_accept('application/json', 'application/xml'); + my $_header_accept = $self->{api_client}->select_header_accept('application/xml', 'application/json'); if ($_header_accept) { $header_params->{'Accept'} = $_header_accept; } @@ -202,14 +217,24 @@ sub create_users_with_list_input { # # Logs user into the system # -# @param string $username The user name for login (optional) -# @param string $password The password for login in clear text (optional) +# @param string $username The user name for login (required) +# @param string $password The password for login in clear text (required) # @return string # sub login_user { my ($self, %args) = @_; + # verify the required parameter 'username' is set + unless (exists $args{'username'}) { + croak("Missing the required parameter 'username' when calling login_user"); + } + + # verify the required parameter 'password' is set + unless (exists $args{'password'}) { + croak("Missing the required parameter 'password' when calling login_user"); + } + # parse inputs my $_resource_path = '/user/login'; @@ -221,7 +246,7 @@ sub login_user { my $form_params = {}; # 'Accept' and 'Content-Type' header - my $_header_accept = $self->{api_client}->select_header_accept('application/json', 'application/xml'); + my $_header_accept = $self->{api_client}->select_header_accept('application/xml', 'application/json'); if ($_header_accept) { $header_params->{'Accept'} = $_header_accept; } @@ -276,7 +301,7 @@ sub logout_user { my $form_params = {}; # 'Accept' and 'Content-Type' header - my $_header_accept = $self->{api_client}->select_header_accept('application/json', 'application/xml'); + my $_header_accept = $self->{api_client}->select_header_accept('application/xml', 'application/json'); if ($_header_accept) { $header_params->{'Accept'} = $_header_accept; } @@ -328,7 +353,7 @@ sub get_user_by_name { my $form_params = {}; # 'Accept' and 'Content-Type' header - my $_header_accept = $self->{api_client}->select_header_accept('application/json', 'application/xml'); + my $_header_accept = $self->{api_client}->select_header_accept('application/xml', 'application/json'); if ($_header_accept) { $header_params->{'Accept'} = $_header_accept; } @@ -366,7 +391,7 @@ sub get_user_by_name { # Updated user # # @param string $username name that need to be deleted (required) -# @param User $body Updated user object (optional) +# @param User $body Updated user object (required) # @return void # sub update_user { @@ -378,6 +403,11 @@ sub update_user { croak("Missing the required parameter 'username' when calling update_user"); } + # verify the required parameter 'body' is set + unless (exists $args{'body'}) { + croak("Missing the required parameter 'body' when calling update_user"); + } + # parse inputs my $_resource_path = '/user/{username}'; @@ -389,7 +419,7 @@ sub update_user { my $form_params = {}; # 'Accept' and 'Content-Type' header - my $_header_accept = $self->{api_client}->select_header_accept('application/json', 'application/xml'); + my $_header_accept = $self->{api_client}->select_header_accept('application/xml', 'application/json'); if ($_header_accept) { $header_params->{'Accept'} = $_header_accept; } @@ -449,7 +479,7 @@ sub delete_user { my $form_params = {}; # 'Accept' and 'Content-Type' header - my $_header_accept = $self->{api_client}->select_header_accept('application/json', 'application/xml'); + my $_header_accept = $self->{api_client}->select_header_accept('application/xml', 'application/json'); if ($_header_accept) { $header_params->{'Accept'} = $_header_accept; } diff --git a/samples/client/petstore/perl/t/01_pet_api.t b/samples/client/petstore/perl/t/01_pet_api.t index bbd1aae4c1df..6e1bb28af949 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'; @@ -69,8 +69,8 @@ is $update_pet_with_form, undef, 'get the null response from update_pet_wth_form my $get_pet_after_update = $api->get_pet_by_id(pet_id => $pet_id); is $get_pet_after_update->{status}, 'sold', 'get the updated status after update_pet_with_form'; -my $upload_pet = $api->upload_file(pet_id => $pet_id, additional_metadata => 'testabc', file => 'test.pl'); -is $upload_pet, undef, 'get the null response from upload_pet'; +my $upload_file = $api->upload_file(pet_id => $pet_id, additional_metadata => 'testabc', file => 'test.pl'); +isa_ok($upload_file, 'WWW::SwaggerClient::Object::ApiResponse'); # WWW::SwaggerClient::Object::Pet my $delete_pet = $api->delete_pet(pet_id => $pet_id); is $delete_pet, undef, 'get the null response from delete_pet'; From 37b123530f42ae548d13bf06743129cdad68c8b1 Mon Sep 17 00:00:00 2001 From: Dave Baird Date: Tue, 3 Nov 2015 17:58:53 +0100 Subject: [PATCH 2/5] Add ApiFactory class and proper accessors on object classes ApiFactory provides a get_api() method to generate API objects without having to hard-code class names. All API and object classes are loaded automatically. Also, added proper accessors for attributes of object classes. --- .../codegen/languages/PerlClientCodegen.java | 1 + .../main/resources/perl/ApiFactory.mustache | 63 +++++++++++++++++++ .../main/resources/perl/BaseObject.mustache | 2 + .../src/main/resources/perl/object.mustache | 2 + .../perl/lib/WWW/SwaggerClient/ApiFactory.pm | 63 +++++++++++++++++++ .../WWW/SwaggerClient/Object/ApiResponse.pm | 2 + .../WWW/SwaggerClient/Object/BaseObject.pm | 2 + .../lib/WWW/SwaggerClient/Object/Category.pm | 2 + .../lib/WWW/SwaggerClient/Object/Order.pm | 2 + .../perl/lib/WWW/SwaggerClient/Object/Pet.pm | 2 + .../perl/lib/WWW/SwaggerClient/Object/Tag.pm | 2 + .../perl/lib/WWW/SwaggerClient/Object/User.pm | 2 + samples/client/petstore/perl/pom.xml | 13 ++++ .../client/petstore/perl/t/03_api_factory.t | 34 ++++++++++ 14 files changed, 192 insertions(+) create mode 100644 modules/swagger-codegen/src/main/resources/perl/ApiFactory.mustache create mode 100644 samples/client/petstore/perl/lib/WWW/SwaggerClient/ApiFactory.pm create mode 100644 samples/client/petstore/perl/t/03_api_factory.t 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/ApiFactory.mustache b/modules/swagger-codegen/src/main/resources/perl/ApiFactory.mustache new file mode 100644 index 000000000000..2ea2db724023 --- /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 4e9a4699ed3c..58c047f49a4b 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 d4d6961c646d..fc418e79ce4f 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 000000000000..811031b06a3b --- /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 02c36f172620..029aad32b9b4 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 1c17b55976c8..fcdbe3fc14e9 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 2b2c0beceac7..7aa9b6428246 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 14da4a5f3af2..f7016179e3a7 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 eb74ad3f368a..20b9262e49fb 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 1b136d5fbfc3..2c585b1ec800 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 1beb8f0b2017..271f5e91ef75 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 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/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'; From 4264b74e4083288c8971f13c26c83a3ff527dc1a Mon Sep 17 00:00:00 2001 From: Dave Baird Date: Wed, 4 Nov 2015 20:27:23 +0100 Subject: [PATCH 3/5] Minor tidying up Some documentation, fix a warning, added a couple of logger calls. --- .../main/resources/perl/ApiClient.mustache | 16 +++++++- .../main/resources/perl/ApiFactory.mustache | 39 ++++++++++++++++++- .../src/main/resources/perl/api.mustache | 2 +- .../perl/lib/WWW/SwaggerClient/ApiClient.pm | 16 +++++++- .../perl/lib/WWW/SwaggerClient/ApiFactory.pm | 39 ++++++++++++++++++- .../perl/lib/WWW/SwaggerClient/PetApi.pm | 2 +- 6 files changed, 104 insertions(+), 10 deletions(-) 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 index 2ea2db724023..fc309c91b9cf 100644 --- a/modules/swagger-codegen/src/main/resources/perl/ApiFactory.mustache +++ b/modules/swagger-codegen/src/main/resources/perl/ApiFactory.mustache @@ -45,19 +45,54 @@ 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); + return $api_class->new(api_client => $self->api_client); } -sub _api_client { $_[0]->{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/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/samples/client/petstore/perl/lib/WWW/SwaggerClient/ApiClient.pm b/samples/client/petstore/perl/lib/WWW/SwaggerClient/ApiClient.pm index f75bf24e0310..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; @@ -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/samples/client/petstore/perl/lib/WWW/SwaggerClient/ApiFactory.pm b/samples/client/petstore/perl/lib/WWW/SwaggerClient/ApiFactory.pm index 811031b06a3b..701a22603c53 100644 --- a/samples/client/petstore/perl/lib/WWW/SwaggerClient/ApiFactory.pm +++ b/samples/client/petstore/perl/lib/WWW/SwaggerClient/ApiFactory.pm @@ -45,19 +45,54 @@ 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); + return $api_class->new(api_client => $self->api_client); } -sub _api_client { $_[0]->{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/PetApi.pm b/samples/client/petstore/perl/lib/WWW/SwaggerClient/PetApi.pm index c8f0fc98ff30..a7cb9a973df4 100644 --- a/samples/client/petstore/perl/lib/WWW/SwaggerClient/PetApi.pm +++ b/samples/client/petstore/perl/lib/WWW/SwaggerClient/PetApi.pm @@ -515,7 +515,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'}; } From f521680c0f1f3d796468b69fe8b0fe3aada0af7f Mon Sep 17 00:00:00 2001 From: Dave Baird Date: Wed, 4 Nov 2015 21:42:27 +0100 Subject: [PATCH 4/5] Moved object classes' constructor into base class --- .../main/resources/perl/BaseObject.mustache | 26 ++++++++--- .../src/main/resources/perl/object.mustache | 32 +++---------- .../WWW/SwaggerClient/Object/ApiResponse.pm | 35 +++------------ .../WWW/SwaggerClient/Object/BaseObject.pm | 26 ++++++++--- .../lib/WWW/SwaggerClient/Object/Category.pm | 33 +++----------- .../lib/WWW/SwaggerClient/Object/Order.pm | 41 +++-------------- .../perl/lib/WWW/SwaggerClient/Object/Pet.pm | 41 +++-------------- .../perl/lib/WWW/SwaggerClient/Object/Tag.pm | 33 +++----------- .../perl/lib/WWW/SwaggerClient/Object/User.pm | 45 +++---------------- 9 files changed, 77 insertions(+), 235 deletions(-) diff --git a/modules/swagger-codegen/src/main/resources/perl/BaseObject.mustache b/modules/swagger-codegen/src/main/resources/perl/BaseObject.mustache index 58c047f49a4b..f2a58efdb68f 100644 --- a/modules/swagger-codegen/src/main/resources/perl/BaseObject.mustache +++ b/modules/swagger-codegen/src/main/resources/perl/BaseObject.mustache @@ -11,7 +11,7 @@ use Log::Any qw($log); use Date::Parse; use DateTime; -use base "Class::Accessor"; +use base ("Class::Accessor", "Class::Data::Inheritable"); # @@ -20,6 +20,22 @@ use base "Class::Accessor"; #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 { @@ -30,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; @@ -43,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/object.mustache b/modules/swagger-codegen/src/main/resources/perl/object.mustache index fc418e79ce4f..bbddd3c7f32c 100644 --- a/modules/swagger-codegen/src/main/resources/perl/object.mustache +++ b/modules/swagger-codegen/src/main/resources/perl/object.mustache @@ -21,39 +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}} -}; +} ); -__PACKAGE__->mk_accessors(keys %$attribute_map); - -# 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/Object/ApiResponse.pm b/samples/client/petstore/perl/lib/WWW/SwaggerClient/Object/ApiResponse.pm index 029aad32b9b4..9d3de7648aad 100644 --- a/samples/client/petstore/perl/lib/WWW/SwaggerClient/Object/ApiResponse.pm +++ b/samples/client/petstore/perl/lib/WWW/SwaggerClient/Object/ApiResponse.pm @@ -19,43 +19,18 @@ 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( { 'code' => 'int', 'type' => 'string', 'message' => 'string' -}; +} ); -my $attribute_map = { +__PACKAGE__->attribute_map( { 'code' => 'code', 'type' => 'type', 'message' => 'message' -}; +} ); -__PACKAGE__->mk_accessors(keys %$attribute_map); - -# new object -sub new { - my ($class, %args) = @_; - my $self = { - # - 'code' => $args{'code'}, - # - 'type' => $args{'type'}, - # - 'message' => $args{'message'} - }; - - 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/BaseObject.pm b/samples/client/petstore/perl/lib/WWW/SwaggerClient/Object/BaseObject.pm index fcdbe3fc14e9..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,7 +11,7 @@ use Log::Any qw($log); use Date::Parse; use DateTime; -use base "Class::Accessor"; +use base ("Class::Accessor", "Class::Data::Inheritable"); # @@ -20,6 +20,22 @@ use base "Class::Accessor"; #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 { @@ -30,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; @@ -43,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/samples/client/petstore/perl/lib/WWW/SwaggerClient/Object/Category.pm b/samples/client/petstore/perl/lib/WWW/SwaggerClient/Object/Category.pm index 7aa9b6428246..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,39 +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' -}; +} ); -__PACKAGE__->mk_accessors(keys %$attribute_map); - -# 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 f7016179e3a7..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,55 +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' -}; +} ); -__PACKAGE__->mk_accessors(keys %$attribute_map); - -# 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 20b9262e49fb..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,55 +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' -}; +} ); -__PACKAGE__->mk_accessors(keys %$attribute_map); - -# 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 2c585b1ec800..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,39 +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' -}; +} ); -__PACKAGE__->mk_accessors(keys %$attribute_map); - -# 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 271f5e91ef75..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,43 +39,8 @@ my $attribute_map = { 'password' => 'password', 'phone' => 'phone', 'user_status' => 'userStatus' -}; +} ); -__PACKAGE__->mk_accessors(keys %$attribute_map); - -# 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; From baf20ca22652598ccd5eb81ead36e5c769f2e7a2 Mon Sep 17 00:00:00 2001 From: Dave Baird Date: Wed, 4 Nov 2015 22:24:49 +0100 Subject: [PATCH 5/5] Fix errors from using wrong JSON spec I've been using http://petstore.swagger.io/v2/swagger.json instead of modules/swagger-codegen/src/test/resources/2_0/petstore.json as the input spec for building the petstore. This commit reverts the changes introduced from that. --- .../WWW/SwaggerClient/Object/ApiResponse.pm | 36 ----------- .../perl/lib/WWW/SwaggerClient/PetApi.pm | 59 ++++++------------- .../perl/lib/WWW/SwaggerClient/StoreApi.pm | 17 ++---- .../perl/lib/WWW/SwaggerClient/UserApi.pm | 58 +++++------------- samples/client/petstore/perl/t/01_pet_api.t | 4 +- 5 files changed, 40 insertions(+), 134 deletions(-) delete mode 100644 samples/client/petstore/perl/lib/WWW/SwaggerClient/Object/ApiResponse.pm diff --git a/samples/client/petstore/perl/lib/WWW/SwaggerClient/Object/ApiResponse.pm b/samples/client/petstore/perl/lib/WWW/SwaggerClient/Object/ApiResponse.pm deleted file mode 100644 index 9d3de7648aad..000000000000 --- a/samples/client/petstore/perl/lib/WWW/SwaggerClient/Object/ApiResponse.pm +++ /dev/null @@ -1,36 +0,0 @@ -package WWW::SwaggerClient::Object::ApiResponse; - -require 5.6.0; -use strict; -use warnings; -use utf8; -use JSON qw(decode_json); -use Data::Dumper; -use Module::Runtime qw(use_module); -use Log::Any qw($log); -use Date::Parse; -use DateTime; - -use base "WWW::SwaggerClient::Object::BaseObject"; - -# -# -# -#NOTE: This class is auto generated by the swagger code generator program. Do not edit the class manually. -# - -__PACKAGE__->swagger_types( { - 'code' => 'int', - 'type' => 'string', - 'message' => 'string' -} ); - -__PACKAGE__->attribute_map( { - 'code' => 'code', - 'type' => 'type', - 'message' => 'message' -} ); - -__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 a7cb9a973df4..26b9f42e620e 100644 --- a/samples/client/petstore/perl/lib/WWW/SwaggerClient/PetApi.pm +++ b/samples/client/petstore/perl/lib/WWW/SwaggerClient/PetApi.pm @@ -52,18 +52,13 @@ sub new { # # Update an existing pet # -# @param Pet $body Pet object that needs to be added to the store (required) +# @param Pet $body Pet object that needs to be added to the store (optional) # @return void # sub update_pet { my ($self, %args) = @_; - # verify the required parameter 'body' is set - unless (exists $args{'body'}) { - croak("Missing the required parameter 'body' when calling update_pet"); - } - # parse inputs my $_resource_path = '/pet'; @@ -75,7 +70,7 @@ sub update_pet { my $form_params = {}; # 'Accept' and 'Content-Type' header - my $_header_accept = $self->{api_client}->select_header_accept('application/xml', 'application/json'); + my $_header_accept = $self->{api_client}->select_header_accept('application/json', 'application/xml'); if ($_header_accept) { $header_params->{'Accept'} = $_header_accept; } @@ -107,18 +102,13 @@ sub update_pet { # # Add a new pet to the store # -# @param Pet $body Pet object that needs to be added to the store (required) +# @param Pet $body Pet object that needs to be added to the store (optional) # @return void # sub add_pet { my ($self, %args) = @_; - # verify the required parameter 'body' is set - unless (exists $args{'body'}) { - croak("Missing the required parameter 'body' when calling add_pet"); - } - # parse inputs my $_resource_path = '/pet'; @@ -130,7 +120,7 @@ sub add_pet { my $form_params = {}; # 'Accept' and 'Content-Type' header - my $_header_accept = $self->{api_client}->select_header_accept('application/xml', 'application/json'); + my $_header_accept = $self->{api_client}->select_header_accept('application/json', 'application/xml'); if ($_header_accept) { $header_params->{'Accept'} = $_header_accept; } @@ -162,18 +152,13 @@ sub add_pet { # # Finds Pets by status # -# @param ARRAY[string] $status Status values that need to be considered for filter (required) +# @param ARRAY[string] $status Status values that need to be considered for filter (optional) # @return ARRAY[Pet] # sub find_pets_by_status { my ($self, %args) = @_; - # verify the required parameter 'status' is set - unless (exists $args{'status'}) { - croak("Missing the required parameter 'status' when calling find_pets_by_status"); - } - # parse inputs my $_resource_path = '/pet/findByStatus'; @@ -185,7 +170,7 @@ sub find_pets_by_status { my $form_params = {}; # 'Accept' and 'Content-Type' header - my $_header_accept = $self->{api_client}->select_header_accept('application/xml', 'application/json'); + my $_header_accept = $self->{api_client}->select_header_accept('application/json', 'application/xml'); if ($_header_accept) { $header_params->{'Accept'} = $_header_accept; } @@ -220,18 +205,13 @@ sub find_pets_by_status { # # Finds Pets by tags # -# @param ARRAY[string] $tags Tags to filter by (required) +# @param ARRAY[string] $tags Tags to filter by (optional) # @return ARRAY[Pet] # sub find_pets_by_tags { my ($self, %args) = @_; - # verify the required parameter 'tags' is set - unless (exists $args{'tags'}) { - croak("Missing the required parameter 'tags' when calling find_pets_by_tags"); - } - # parse inputs my $_resource_path = '/pet/findByTags'; @@ -243,7 +223,7 @@ sub find_pets_by_tags { my $form_params = {}; # 'Accept' and 'Content-Type' header - my $_header_accept = $self->{api_client}->select_header_accept('application/xml', 'application/json'); + my $_header_accept = $self->{api_client}->select_header_accept('application/json', 'application/xml'); if ($_header_accept) { $header_params->{'Accept'} = $_header_accept; } @@ -278,7 +258,7 @@ sub find_pets_by_tags { # # Find pet by ID # -# @param int $pet_id ID of pet to return (required) +# @param int $pet_id ID of pet that needs to be fetched (required) # @return Pet # sub get_pet_by_id { @@ -301,7 +281,7 @@ sub get_pet_by_id { my $form_params = {}; # 'Accept' and 'Content-Type' header - my $_header_accept = $self->{api_client}->select_header_accept('application/xml', 'application/json'); + my $_header_accept = $self->{api_client}->select_header_accept('application/json', 'application/xml'); if ($_header_accept) { $header_params->{'Accept'} = $_header_accept; } @@ -338,7 +318,7 @@ sub get_pet_by_id { # # Updates a pet in the store with form data # -# @param int $pet_id ID of pet that needs to be updated (required) +# @param string $pet_id ID of pet that needs to be updated (required) # @param string $name Updated name of the pet (optional) # @param string $status Updated status of the pet (optional) # @return void @@ -363,7 +343,7 @@ sub update_pet_with_form { my $form_params = {}; # 'Accept' and 'Content-Type' header - my $_header_accept = $self->{api_client}->select_header_accept('application/xml', 'application/json'); + my $_header_accept = $self->{api_client}->select_header_accept('application/json', 'application/xml'); if ($_header_accept) { $header_params->{'Accept'} = $_header_accept; } @@ -431,7 +411,7 @@ sub delete_pet { my $form_params = {}; # 'Accept' and 'Content-Type' header - my $_header_accept = $self->{api_client}->select_header_accept('application/xml', 'application/json'); + my $_header_accept = $self->{api_client}->select_header_accept('application/json', 'application/xml'); if ($_header_accept) { $header_params->{'Accept'} = $_header_accept; } @@ -471,7 +451,7 @@ sub delete_pet { # @param int $pet_id ID of pet to update (required) # @param string $additional_metadata Additional data to pass to server (optional) # @param file $file file to upload (optional) -# @return ApiResponse +# @return void # sub upload_file { my ($self, %args) = @_; @@ -493,7 +473,7 @@ sub upload_file { my $form_params = {}; # 'Accept' and 'Content-Type' header - my $_header_accept = $self->{api_client}->select_header_accept('application/json'); + my $_header_accept = $self->{api_client}->select_header_accept('application/json', 'application/xml'); if ($_header_accept) { $header_params->{'Accept'} = $_header_accept; } @@ -526,14 +506,11 @@ sub upload_file { my $auth_settings = ['petstore_auth']; # make the API Call - my $response = $self->{api_client}->call_api($_resource_path, $_method, + + $self->{api_client}->call_api($_resource_path, $_method, $query_params, $form_params, $header_params, $_body_data, $auth_settings); - if (!$response) { - return; - } - my $_response_object = $self->{api_client}->deserialize('ApiResponse', $response); - return $_response_object; + return; } diff --git a/samples/client/petstore/perl/lib/WWW/SwaggerClient/StoreApi.pm b/samples/client/petstore/perl/lib/WWW/SwaggerClient/StoreApi.pm index 047c4624587e..a2ac9010e779 100644 --- a/samples/client/petstore/perl/lib/WWW/SwaggerClient/StoreApi.pm +++ b/samples/client/petstore/perl/lib/WWW/SwaggerClient/StoreApi.pm @@ -69,7 +69,7 @@ sub get_inventory { my $form_params = {}; # 'Accept' and 'Content-Type' header - my $_header_accept = $self->{api_client}->select_header_accept('application/json'); + my $_header_accept = $self->{api_client}->select_header_accept('application/json', 'application/xml'); if ($_header_accept) { $header_params->{'Accept'} = $_header_accept; } @@ -101,18 +101,13 @@ sub get_inventory { # # Place an order for a pet # -# @param Order $body order placed for purchasing the pet (required) +# @param Order $body order placed for purchasing the pet (optional) # @return Order # sub place_order { my ($self, %args) = @_; - # verify the required parameter 'body' is set - unless (exists $args{'body'}) { - croak("Missing the required parameter 'body' when calling place_order"); - } - # parse inputs my $_resource_path = '/store/order'; @@ -124,7 +119,7 @@ sub place_order { my $form_params = {}; # 'Accept' and 'Content-Type' header - my $_header_accept = $self->{api_client}->select_header_accept('application/xml', 'application/json'); + my $_header_accept = $self->{api_client}->select_header_accept('application/json', 'application/xml'); if ($_header_accept) { $header_params->{'Accept'} = $_header_accept; } @@ -159,7 +154,7 @@ sub place_order { # # Find purchase order by ID # -# @param int $order_id ID of pet that needs to be fetched (required) +# @param string $order_id ID of pet that needs to be fetched (required) # @return Order # sub get_order_by_id { @@ -182,7 +177,7 @@ sub get_order_by_id { my $form_params = {}; # 'Accept' and 'Content-Type' header - my $_header_accept = $self->{api_client}->select_header_accept('application/xml', 'application/json'); + my $_header_accept = $self->{api_client}->select_header_accept('application/json', 'application/xml'); if ($_header_accept) { $header_params->{'Accept'} = $_header_accept; } @@ -242,7 +237,7 @@ sub delete_order { my $form_params = {}; # 'Accept' and 'Content-Type' header - my $_header_accept = $self->{api_client}->select_header_accept('application/xml', 'application/json'); + my $_header_accept = $self->{api_client}->select_header_accept('application/json', 'application/xml'); if ($_header_accept) { $header_params->{'Accept'} = $_header_accept; } diff --git a/samples/client/petstore/perl/lib/WWW/SwaggerClient/UserApi.pm b/samples/client/petstore/perl/lib/WWW/SwaggerClient/UserApi.pm index f70c20136a41..e1088a0839e0 100644 --- a/samples/client/petstore/perl/lib/WWW/SwaggerClient/UserApi.pm +++ b/samples/client/petstore/perl/lib/WWW/SwaggerClient/UserApi.pm @@ -52,18 +52,13 @@ sub new { # # Create user # -# @param User $body Created user object (required) +# @param User $body Created user object (optional) # @return void # sub create_user { my ($self, %args) = @_; - # verify the required parameter 'body' is set - unless (exists $args{'body'}) { - croak("Missing the required parameter 'body' when calling create_user"); - } - # parse inputs my $_resource_path = '/user'; @@ -75,7 +70,7 @@ sub create_user { my $form_params = {}; # 'Accept' and 'Content-Type' header - my $_header_accept = $self->{api_client}->select_header_accept('application/xml', 'application/json'); + my $_header_accept = $self->{api_client}->select_header_accept('application/json', 'application/xml'); if ($_header_accept) { $header_params->{'Accept'} = $_header_accept; } @@ -107,18 +102,13 @@ sub create_user { # # Creates list of users with given input array # -# @param ARRAY[User] $body List of user object (required) +# @param ARRAY[User] $body List of user object (optional) # @return void # sub create_users_with_array_input { my ($self, %args) = @_; - # verify the required parameter 'body' is set - unless (exists $args{'body'}) { - croak("Missing the required parameter 'body' when calling create_users_with_array_input"); - } - # parse inputs my $_resource_path = '/user/createWithArray'; @@ -130,7 +120,7 @@ sub create_users_with_array_input { my $form_params = {}; # 'Accept' and 'Content-Type' header - my $_header_accept = $self->{api_client}->select_header_accept('application/xml', 'application/json'); + my $_header_accept = $self->{api_client}->select_header_accept('application/json', 'application/xml'); if ($_header_accept) { $header_params->{'Accept'} = $_header_accept; } @@ -162,18 +152,13 @@ sub create_users_with_array_input { # # Creates list of users with given input array # -# @param ARRAY[User] $body List of user object (required) +# @param ARRAY[User] $body List of user object (optional) # @return void # sub create_users_with_list_input { my ($self, %args) = @_; - # verify the required parameter 'body' is set - unless (exists $args{'body'}) { - croak("Missing the required parameter 'body' when calling create_users_with_list_input"); - } - # parse inputs my $_resource_path = '/user/createWithList'; @@ -185,7 +170,7 @@ sub create_users_with_list_input { my $form_params = {}; # 'Accept' and 'Content-Type' header - my $_header_accept = $self->{api_client}->select_header_accept('application/xml', 'application/json'); + my $_header_accept = $self->{api_client}->select_header_accept('application/json', 'application/xml'); if ($_header_accept) { $header_params->{'Accept'} = $_header_accept; } @@ -217,24 +202,14 @@ sub create_users_with_list_input { # # Logs user into the system # -# @param string $username The user name for login (required) -# @param string $password The password for login in clear text (required) +# @param string $username The user name for login (optional) +# @param string $password The password for login in clear text (optional) # @return string # sub login_user { my ($self, %args) = @_; - # verify the required parameter 'username' is set - unless (exists $args{'username'}) { - croak("Missing the required parameter 'username' when calling login_user"); - } - - # verify the required parameter 'password' is set - unless (exists $args{'password'}) { - croak("Missing the required parameter 'password' when calling login_user"); - } - # parse inputs my $_resource_path = '/user/login'; @@ -246,7 +221,7 @@ sub login_user { my $form_params = {}; # 'Accept' and 'Content-Type' header - my $_header_accept = $self->{api_client}->select_header_accept('application/xml', 'application/json'); + my $_header_accept = $self->{api_client}->select_header_accept('application/json', 'application/xml'); if ($_header_accept) { $header_params->{'Accept'} = $_header_accept; } @@ -301,7 +276,7 @@ sub logout_user { my $form_params = {}; # 'Accept' and 'Content-Type' header - my $_header_accept = $self->{api_client}->select_header_accept('application/xml', 'application/json'); + my $_header_accept = $self->{api_client}->select_header_accept('application/json', 'application/xml'); if ($_header_accept) { $header_params->{'Accept'} = $_header_accept; } @@ -353,7 +328,7 @@ sub get_user_by_name { my $form_params = {}; # 'Accept' and 'Content-Type' header - my $_header_accept = $self->{api_client}->select_header_accept('application/xml', 'application/json'); + my $_header_accept = $self->{api_client}->select_header_accept('application/json', 'application/xml'); if ($_header_accept) { $header_params->{'Accept'} = $_header_accept; } @@ -391,7 +366,7 @@ sub get_user_by_name { # Updated user # # @param string $username name that need to be deleted (required) -# @param User $body Updated user object (required) +# @param User $body Updated user object (optional) # @return void # sub update_user { @@ -403,11 +378,6 @@ sub update_user { croak("Missing the required parameter 'username' when calling update_user"); } - # verify the required parameter 'body' is set - unless (exists $args{'body'}) { - croak("Missing the required parameter 'body' when calling update_user"); - } - # parse inputs my $_resource_path = '/user/{username}'; @@ -419,7 +389,7 @@ sub update_user { my $form_params = {}; # 'Accept' and 'Content-Type' header - my $_header_accept = $self->{api_client}->select_header_accept('application/xml', 'application/json'); + my $_header_accept = $self->{api_client}->select_header_accept('application/json', 'application/xml'); if ($_header_accept) { $header_params->{'Accept'} = $_header_accept; } @@ -479,7 +449,7 @@ sub delete_user { my $form_params = {}; # 'Accept' and 'Content-Type' header - my $_header_accept = $self->{api_client}->select_header_accept('application/xml', 'application/json'); + my $_header_accept = $self->{api_client}->select_header_accept('application/json', 'application/xml'); if ($_header_accept) { $header_params->{'Accept'} = $_header_accept; } diff --git a/samples/client/petstore/perl/t/01_pet_api.t b/samples/client/petstore/perl/t/01_pet_api.t index 6e1bb28af949..5460c3282b26 100644 --- a/samples/client/petstore/perl/t/01_pet_api.t +++ b/samples/client/petstore/perl/t/01_pet_api.t @@ -69,8 +69,8 @@ is $update_pet_with_form, undef, 'get the null response from update_pet_wth_form my $get_pet_after_update = $api->get_pet_by_id(pet_id => $pet_id); is $get_pet_after_update->{status}, 'sold', 'get the updated status after update_pet_with_form'; -my $upload_file = $api->upload_file(pet_id => $pet_id, additional_metadata => 'testabc', file => 'test.pl'); -isa_ok($upload_file, 'WWW::SwaggerClient::Object::ApiResponse'); # WWW::SwaggerClient::Object::Pet +my $upload_pet = $api->upload_file(pet_id => $pet_id, additional_metadata => 'testabc', file => 'test.pl'); +is $upload_pet, undef, 'get the null response from upload_pet'; my $delete_pet = $api->delete_pet(pet_id => $pet_id); is $delete_pet, undef, 'get the null response from delete_pet';