From 303dbe7730694ba0f74584e06bc7b00d2860bacd Mon Sep 17 00:00:00 2001 From: wing328 Date: Wed, 10 Jun 2015 10:53:21 +0800 Subject: [PATCH] fix array,map for perl, add test case --- .../main/resources/perl/ApiClient.mustache | 50 +++++++---- .../perl/lib/WWW/SwaggerClient/ApiClient.pm | 48 +++++++--- .../perl/lib/WWW/SwaggerClient/PetApi.pm | 2 +- samples/client/petstore/perl/pom.xml | 15 +++- samples/client/petstore/perl/t/02_store_api.t | 88 +++++++++++++++++++ samples/client/petstore/perl/test.pl | 27 ++++++ .../php-coveralls/build/config/apigen.neon | 5 ++ .../php-coveralls/build/config/phpcs.xml | 31 +++++++ .../php-coveralls/build/config/phpmd.xml | 45 ++++++++++ .../satooshi/php-coveralls/travis/empty | 0 10 files changed, 280 insertions(+), 31 deletions(-) create mode 100644 samples/client/petstore/perl/t/02_store_api.t create mode 100644 samples/client/petstore/php/SwaggerClient/vendor/satooshi/php-coveralls/build/config/apigen.neon create mode 100644 samples/client/petstore/php/SwaggerClient/vendor/satooshi/php-coveralls/build/config/phpcs.xml create mode 100644 samples/client/petstore/php/SwaggerClient/vendor/satooshi/php-coveralls/build/config/phpmd.xml create mode 100644 samples/client/petstore/php/SwaggerClient/vendor/satooshi/php-coveralls/travis/empty diff --git a/modules/swagger-codegen/src/main/resources/perl/ApiClient.mustache b/modules/swagger-codegen/src/main/resources/perl/ApiClient.mustache index 2bcdd6da690..f57642ff293 100644 --- a/modules/swagger-codegen/src/main/resources/perl/ApiClient.mustache +++ b/modules/swagger-codegen/src/main/resources/perl/ApiClient.mustache @@ -198,33 +198,53 @@ sub deserialize { my ($self, $class, $data) = @_; $log->debugf("deserializing %s for %s", $data, $class); - my $_result; if (not defined $data) { return undef; - } elsif ( lc(substr($class, 0, 4)) eq 'map[') { #hash - $_result = \(json_decode $data); - } elsif ( lc(substr($class, 0, 6)) eq 'array[' ) { # array of data + } elsif ( (substr($class, 0, 5)) eq 'HASH[') { #hash + if ($class =~ /^HASH\[(.*),(.*)\]$/) { + my ($key_type, $type) = ($1, $2); + my %hash; + my $decoded_data = decode_json $data; + foreach my $key (keys %$decoded_data) { + if (ref $decoded_data->{$key} eq 'HASH') { + $hash{$key} = $self->deserialize($type, encode_json $decoded_data->{$key}); + } else { + $hash{$key} = $self->deserialize($type, $decoded_data->{$key}); + } + } + return \%hash; + } else { + #TODO log error + } + + } elsif ( (substr($class, 0, 6)) eq 'ARRAY[' ) { # array of data return $data if $data eq '[]'; # return if empty array my $_sub_class = substr($class, 6, -1); - my @_json_data = json_decode $data; + my $_json_data = decode_json $data; my @_values = (); - foreach my $_value (@_json_data) { - push @_values, $self->deserialize($_sub_class, $_value); + foreach my $_value (@$_json_data) { + if (ref $_value eq 'ARRAY') { + push @_values, $self->deserialize($_sub_class, encode_json $_value); + } else { + push @_values, $self->deserialize($_sub_class, $_value); + } } - $_result = \@_values; + return \@_values; } elsif ($class eq 'DateTime') { - $_result = DateTime->from_epoch(epoch => str2time($data)); - } elsif (grep /^$class$/, ('string', 'int', 'float', 'bool', 'object')) { #TODO revise the primitive type - $_result= $data; + return DateTime->from_epoch(epoch => str2time($data)); + } elsif (grep /^$class$/, ('string', 'int', 'float', 'bool', 'object')) { + return $data; } else { # model - my $_instance = use_module("WWW::{{invokerPackage}}::Object::$class")->new; - $_result = $_instance->from_hash(decode_json $data); + my $_instance = use_module("WWW::SwaggerClient::Object::$class")->new; + if (ref $data eq "HASH") { + return $_instance->from_hash($data); + } else { # string, need to json decode first + return $_instance->from_hash(decode_json $data); + } } - return $_result; - } # return 'Accept' based on an array of accept provided diff --git a/samples/client/petstore/perl/lib/WWW/SwaggerClient/ApiClient.pm b/samples/client/petstore/perl/lib/WWW/SwaggerClient/ApiClient.pm index da29b3e246a..3a69adbad4d 100644 --- a/samples/client/petstore/perl/lib/WWW/SwaggerClient/ApiClient.pm +++ b/samples/client/petstore/perl/lib/WWW/SwaggerClient/ApiClient.pm @@ -198,33 +198,53 @@ sub deserialize { my ($self, $class, $data) = @_; $log->debugf("deserializing %s for %s", $data, $class); - my $_result; if (not defined $data) { return undef; - } elsif ( lc(substr($class, 0, 4)) eq 'map[') { #hash - $_result = \(json_decode $data); - } elsif ( lc(substr($class, 0, 6)) eq 'array[' ) { # array of data + } elsif ( (substr($class, 0, 5)) eq 'HASH[') { #hash + if ($class =~ /^HASH\[(.*),(.*)\]$/) { + my ($key_type, $type) = ($1, $2); + my %hash; + my $decoded_data = decode_json $data; + foreach my $key (keys %$decoded_data) { + if (ref $decoded_data->{$key} eq 'HASH') { + $hash{$key} = $self->deserialize($type, encode_json $decoded_data->{$key}); + } else { + $hash{$key} = $self->deserialize($type, $decoded_data->{$key}); + } + } + return \%hash; + } else { + #TODO log error + } + + } elsif ( (substr($class, 0, 6)) eq 'ARRAY[' ) { # array of data return $data if $data eq '[]'; # return if empty array my $_sub_class = substr($class, 6, -1); - my @_json_data = json_decode $data; + my $_json_data = decode_json $data; my @_values = (); - foreach my $_value (@_json_data) { - push @_values, $self->deserialize($_sub_class, $_value); + foreach my $_value (@$_json_data) { + if (ref $_value eq 'ARRAY') { + push @_values, $self->deserialize($_sub_class, encode_json $_value); + } else { + push @_values, $self->deserialize($_sub_class, $_value); + } } - $_result = \@_values; + return \@_values; } elsif ($class eq 'DateTime') { - $_result = DateTime->from_epoch(epoch => str2time($data)); - } elsif (grep /^$class$/, ('string', 'int', 'float', 'bool', 'object')) { #TODO revise the primitive type - $_result= $data; + return DateTime->from_epoch(epoch => str2time($data)); + } elsif (grep /^$class$/, ('string', 'int', 'float', 'bool', 'object')) { + return $data; } else { # model my $_instance = use_module("WWW::SwaggerClient::Object::$class")->new; - $_result = $_instance->from_hash(decode_json $data); + if (ref $data eq "HASH") { + return $_instance->from_hash($data); + } else { # string, need to json decode first + return $_instance->from_hash(decode_json $data); + } } - return $_result; - } # return 'Accept' based on an array of accept provided diff --git a/samples/client/petstore/perl/lib/WWW/SwaggerClient/PetApi.pm b/samples/client/petstore/perl/lib/WWW/SwaggerClient/PetApi.pm index dd5ff29a803..0a926625b74 100644 --- a/samples/client/petstore/perl/lib/WWW/SwaggerClient/PetApi.pm +++ b/samples/client/petstore/perl/lib/WWW/SwaggerClient/PetApi.pm @@ -317,7 +317,7 @@ sub new { # authentication setting, if any - my $auth_settings = ['petstore_auth', 'api_key']; + my $auth_settings = ['api_key', 'petstore_auth']; # make the API Call my $response = $self->{api_client}->call_api($_resource_path, $_method, diff --git a/samples/client/petstore/perl/pom.xml b/samples/client/petstore/perl/pom.xml index f92eaacb134..00d192b1e54 100644 --- a/samples/client/petstore/perl/pom.xml +++ b/samples/client/petstore/perl/pom.xml @@ -27,7 +27,7 @@ 1.2.1 - Test::More + Test::More for Pet integration-test exec @@ -39,6 +39,19 @@ + + Test::More for Store + integration-test + + exec + + + perl + + t/02_store_api.t + + + diff --git a/samples/client/petstore/perl/t/02_store_api.t b/samples/client/petstore/perl/t/02_store_api.t new file mode 100644 index 00000000000..f4687daad41 --- /dev/null +++ b/samples/client/petstore/perl/t/02_store_api.t @@ -0,0 +1,88 @@ +use Test::More tests => 22; +use Test::Exception; + +use lib 'lib'; +use strict; +use warnings; + +use JSON; + +use_ok('WWW::SwaggerClient::StoreApi'); +use_ok('WWW::SwaggerClient::ApiClient'); +use_ok('WWW::SwaggerClient::Object::Pet'); +use_ok('WWW::SwaggerClient::Object::Tag'); +use_ok('WWW::SwaggerClient::Object::Category'); + +my $api_client = WWW::SwaggerClient::ApiClient->new(); +my $store_api = WWW::SwaggerClient::StoreApi->new('api_client' => $api_client); + +is $store_api->{api_client}->{base_url}, 'http://petstore.swagger.io/v2', 'get the default base URL from api client'; + +my $get_inventory_response = $store_api->get_inventory(); + +like ($get_inventory_response->{pending}, qr/^\d+$/, "pending is numeric"); +like ($get_inventory_response->{sold}, qr/^\d+$/, "sold is numeric"); + +my $pet_json = <deserialize("HASH[string,Pet]", $pet_json)->{pet}, "WWW::SwaggerClient::Object::Pet", "get Pet object from hash"; +is $api_client->deserialize("HASH[string,Pet]", $pet_json)->{pet}->{name}, "doggie", "get the name of the Pet object"; +is $api_client->deserialize("HASH[string,Pet]", $pet_json)->{pet}->{category}->{name}, "string", "get the category name of the Pet object"; +is ref $api_client->deserialize("HASH[string,Pet]", $pet_json)->{pet}->{category}, "WWW::SwaggerClient::Object::Category", "get the Category the Pet object"; +is ref $api_client->deserialize("HASH[string,Pet]", $pet_json)->{pet}->{tags}[0], "WWW::SwaggerClient::Object::Tag", "get the Tag of the Pet object"; +is $api_client->deserialize("HASH[string,Pet]", $pet_json)->{pet}->{tags}[0]->{name}, "tag string", "get the Tag name of the Pet object"; + +my $array_json = <deserialize("ARRAY[Pet]", $array_json)->[0], "WWW::SwaggerClient::Object::Pet", "get Pet object from hash"; +is $api_client->deserialize("ARRAY[Pet]", $array_json)->[0]->{name}, "doggie", "get the name of the Pet object"; +is $api_client->deserialize("ARRAY[Pet]", $array_json)->[0]->{category}->{name}, "string", "get the category name of the Pet object"; +is ref $api_client->deserialize("ARRAY[Pet]", $array_json)->[0]->{category}, "WWW::SwaggerClient::Object::Category", "get the Category the Pet object"; +is ref $api_client->deserialize("ARRAY[Pet]", $array_json)->[0]->{tags}->[0], "WWW::SwaggerClient::Object::Tag", "get the Tag[0] the Pet object"; +is $api_client->deserialize("ARRAY[Pet]", $array_json)->[0]->{tags}->[0]->{name}, "tag string", "get the tag name the Pet object"; + + diff --git a/samples/client/petstore/perl/test.pl b/samples/client/petstore/perl/test.pl index d4f3fbb6f4b..b12105c9054 100755 --- a/samples/client/petstore/perl/test.pl +++ b/samples/client/petstore/perl/test.pl @@ -5,6 +5,7 @@ use lib 'lib'; use strict; use warnings; use WWW::SwaggerClient::PetApi; +use WWW::SwaggerClient::StoreApi; use WWW::SwaggerClient::ApiClient; use WWW::SwaggerClient::Configuration; use WWW::SwaggerClient::Object::Pet; @@ -45,4 +46,30 @@ print "\nget_pet_by_id:".Dumper $api->get_pet_by_id(pet_id => $pet_id); print "\nupdate_pet_with_form:".Dumper $api->update_pet_with_form(pet_id => $pet_id, name => 'test_name', status => 'test status'); print "\ndelete_pet:".Dumper $api->delete_pet(pet_id => $pet_id); +my $store_api = WWW::SwaggerClient::StoreApi->new(); +print "\nget_inventory:".Dumper $store_api->get_inventory(); +my $pet_json = <deserialize:".Dumper($api->{api_client}->deserialize("HASH[string,Pet]", $pet_json)); diff --git a/samples/client/petstore/php/SwaggerClient/vendor/satooshi/php-coveralls/build/config/apigen.neon b/samples/client/petstore/php/SwaggerClient/vendor/satooshi/php-coveralls/build/config/apigen.neon new file mode 100644 index 00000000000..c067c2c290f --- /dev/null +++ b/samples/client/petstore/php/SwaggerClient/vendor/satooshi/php-coveralls/build/config/apigen.neon @@ -0,0 +1,5 @@ +main: Contrib +title: php-coveralls +internal: yes +todo: yes +wipeout: yes diff --git a/samples/client/petstore/php/SwaggerClient/vendor/satooshi/php-coveralls/build/config/phpcs.xml b/samples/client/petstore/php/SwaggerClient/vendor/satooshi/php-coveralls/build/config/phpcs.xml new file mode 100644 index 00000000000..82a58e1b324 --- /dev/null +++ b/samples/client/petstore/php/SwaggerClient/vendor/satooshi/php-coveralls/build/config/phpcs.xml @@ -0,0 +1,31 @@ + + + The coding standard for standard PHP application + */img/* + */images/* + */less/* + */css/* + */js/* + *.html + *.twig + *.yml + *.xml + *.txt + *.less + *.css + *.js + *.jpg + *.jpeg + *.png + *.gif + + + + + + + + + + + diff --git a/samples/client/petstore/php/SwaggerClient/vendor/satooshi/php-coveralls/build/config/phpmd.xml b/samples/client/petstore/php/SwaggerClient/vendor/satooshi/php-coveralls/build/config/phpmd.xml new file mode 100644 index 00000000000..27d3193e749 --- /dev/null +++ b/samples/client/petstore/php/SwaggerClient/vendor/satooshi/php-coveralls/build/config/phpmd.xml @@ -0,0 +1,45 @@ + + + + My custom rule set that checks my code... + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/samples/client/petstore/php/SwaggerClient/vendor/satooshi/php-coveralls/travis/empty b/samples/client/petstore/php/SwaggerClient/vendor/satooshi/php-coveralls/travis/empty new file mode 100644 index 00000000000..e69de29bb2d