diff --git a/modules/swagger-codegen/src/main/resources/perl/APIClient.mustache b/modules/swagger-codegen/src/main/resources/perl/APIClient.mustache index b0c3c9b5d39..19079a07922 100644 --- a/modules/swagger-codegen/src/main/resources/perl/APIClient.mustache +++ b/modules/swagger-codegen/src/main/resources/perl/APIClient.mustache @@ -253,4 +253,39 @@ sub deserialize } +# return 'Accept' based on an array of accept provided +# @param [Array] header_accept_array Array fo 'Accept' +# @return String Accept (e.g. application/json) +sub select_header_accept +{ + my ($self, @header) = @_; + + if (@header == 0 || (@header == 1 && $header[0] eq '')) { + return undef; + } elsif (grep(/^application\/json$/i, @header)) { + return 'application/json'; + } else { + return join(',', @header); + } + +} + +# return the content type based on an array of content-type provided +# @param [Array] content_type_array Array fo content-type +# @return String Content-Type (e.g. application/json) +sub select_header_content_type +{ + my ($self, @header) = @_; + + if (@header == 0 || (@header == 1 && $header[0] eq '')) { + return 'application/json'; # default to application/json + } elsif (grep(/^application\/json$/i, @header)) { + return 'application/json'; + } else { + return join(',', @header); + } + +} + + 1; diff --git a/modules/swagger-codegen/src/main/resources/perl/api.mustache b/modules/swagger-codegen/src/main/resources/perl/api.mustache index 7b7d9d98a4e..131eb3a3934 100644 --- a/modules/swagger-codegen/src/main/resources/perl/api.mustache +++ b/modules/swagger-codegen/src/main/resources/perl/api.mustache @@ -85,12 +85,12 @@ sub new { my $header_params = {}; my $form_params = {}; - my $_header_accept = '{{#produces}}{{mediaType}}{{#hasMore}}, {{/hasMore}}{{/produces}}'; - if ($_header_accept ne '') { + # 'Accept' and 'Content-Type' header + my $_header_accept = $self->{api_client}->select_header_accept({{#produces}}'{{mediaType}}'{{#hasMore}}, {{/hasMore}}{{/produces}}); + if ($_header_accept) { $header_params->{'Accept'} = $_header_accept; } - my @_header_content_type = ({{#consumes}}'{{mediaType}}'{{#hasMore}},{{/hasMore}}{{/consumes}}); - $header_params->{'Content-Type'} = scalar(@_header_content_type) > 0 ? $_header_content_type[0] : 'application/json'; + $header_params->{'Content-Type'} = $self->{api_client}->select_header_content_type({{#consumes}}'{{mediaType}}'{{#hasMore}}, {{/hasMore}}{{/consumes}}); {{#queryParams}} # query params if ( exists $args{'{{paramName}}'}) { diff --git a/samples/client/petstore/perl/lib/WWW/SwaggerClient/APIClient.pm b/samples/client/petstore/perl/lib/WWW/SwaggerClient/APIClient.pm index 6d0ecc44dc6..0f0fea793e3 100644 --- a/samples/client/petstore/perl/lib/WWW/SwaggerClient/APIClient.pm +++ b/samples/client/petstore/perl/lib/WWW/SwaggerClient/APIClient.pm @@ -253,4 +253,39 @@ sub deserialize } +# return 'Accept' based on an array of accept provided +# @param [Array] header_accept_array Array fo 'Accept' +# @return String Accept (e.g. application/json) +sub select_header_accept +{ + my ($self, @header) = @_; + + if (@header == 0 || (@header == 1 && $header[0] eq '')) { + return undef; + } elsif (grep(/^application\/json$/i, @header)) { + return 'application/json'; + } else { + return join(',', @header); + } + +} + +# return the content type based on an array of content-type provided +# @param [Array] content_type_array Array fo content-type +# @return String Content-Type (e.g. application/json) +sub select_header_content_type +{ + my ($self, @header) = @_; + + if (@header == 0 || (@header == 1 && $header[0] eq '')) { + return 'application/json'; # default to application/json + } elsif (grep(/^application\/json$/i, @header)) { + return 'application/json'; + } else { + return join(',', @header); + } + +} + + 1; diff --git a/samples/client/petstore/perl/lib/WWW/SwaggerClient/PetApi.pm b/samples/client/petstore/perl/lib/WWW/SwaggerClient/PetApi.pm index 930bd52102b..57237a96974 100644 --- a/samples/client/petstore/perl/lib/WWW/SwaggerClient/PetApi.pm +++ b/samples/client/petstore/perl/lib/WWW/SwaggerClient/PetApi.pm @@ -86,12 +86,12 @@ sub new { my $header_params = {}; my $form_params = {}; - my $_header_accept = 'application/json, application/xml'; - if ($_header_accept ne '') { + # 'Accept' and 'Content-Type' header + my $_header_accept = $self->{api_client}->select_header_accept('application/json', 'application/xml'); + if ($_header_accept) { $header_params->{'Accept'} = $_header_accept; } - my @_header_content_type = ('application/json','application/xml',); - $header_params->{'Content-Type'} = scalar(@_header_content_type) > 0 ? $_header_content_type[0] : 'application/json'; + $header_params->{'Content-Type'} = $self->{api_client}->select_header_content_type('application/json', 'application/xml', ); @@ -137,12 +137,12 @@ sub new { my $header_params = {}; my $form_params = {}; - my $_header_accept = 'application/json, application/xml'; - if ($_header_accept ne '') { + # 'Accept' and 'Content-Type' header + my $_header_accept = $self->{api_client}->select_header_accept('application/json', 'application/xml'); + if ($_header_accept) { $header_params->{'Accept'} = $_header_accept; } - my @_header_content_type = ('application/json','application/xml',); - $header_params->{'Content-Type'} = scalar(@_header_content_type) > 0 ? $_header_content_type[0] : 'application/json'; + $header_params->{'Content-Type'} = $self->{api_client}->select_header_content_type('application/json', 'application/xml', ); @@ -188,12 +188,12 @@ sub new { my $header_params = {}; my $form_params = {}; - my $_header_accept = 'application/json, application/xml'; - if ($_header_accept ne '') { + # 'Accept' and 'Content-Type' header + my $_header_accept = $self->{api_client}->select_header_accept('application/json', 'application/xml'); + if ($_header_accept) { $header_params->{'Accept'} = $_header_accept; } - my @_header_content_type = (); - $header_params->{'Content-Type'} = scalar(@_header_content_type) > 0 ? $_header_content_type[0] : 'application/json'; + $header_params->{'Content-Type'} = $self->{api_client}->select_header_content_type(); # query params if ( exists $args{'status'}) { @@ -242,12 +242,12 @@ sub new { my $header_params = {}; my $form_params = {}; - my $_header_accept = 'application/json, application/xml'; - if ($_header_accept ne '') { + # 'Accept' and 'Content-Type' header + my $_header_accept = $self->{api_client}->select_header_accept('application/json', 'application/xml'); + if ($_header_accept) { $header_params->{'Accept'} = $_header_accept; } - my @_header_content_type = (); - $header_params->{'Content-Type'} = scalar(@_header_content_type) > 0 ? $_header_content_type[0] : 'application/json'; + $header_params->{'Content-Type'} = $self->{api_client}->select_header_content_type(); # query params if ( exists $args{'tags'}) { @@ -301,12 +301,12 @@ sub new { my $header_params = {}; my $form_params = {}; - my $_header_accept = 'application/json, application/xml'; - if ($_header_accept ne '') { + # 'Accept' and 'Content-Type' header + my $_header_accept = $self->{api_client}->select_header_accept('application/json', 'application/xml'); + if ($_header_accept) { $header_params->{'Accept'} = $_header_accept; } - my @_header_content_type = (); - $header_params->{'Content-Type'} = scalar(@_header_content_type) > 0 ? $_header_content_type[0] : 'application/json'; + $header_params->{'Content-Type'} = $self->{api_client}->select_header_content_type(); @@ -364,12 +364,12 @@ sub new { my $header_params = {}; my $form_params = {}; - my $_header_accept = 'application/json, application/xml'; - if ($_header_accept ne '') { + # 'Accept' and 'Content-Type' header + my $_header_accept = $self->{api_client}->select_header_accept('application/json', 'application/xml'); + if ($_header_accept) { $header_params->{'Accept'} = $_header_accept; } - my @_header_content_type = ('application/x-www-form-urlencoded',); - $header_params->{'Content-Type'} = scalar(@_header_content_type) > 0 ? $_header_content_type[0] : 'application/json'; + $header_params->{'Content-Type'} = $self->{api_client}->select_header_content_type('application/x-www-form-urlencoded', ); @@ -433,12 +433,12 @@ sub new { my $header_params = {}; my $form_params = {}; - my $_header_accept = 'application/json, application/xml'; - if ($_header_accept ne '') { + # 'Accept' and 'Content-Type' header + my $_header_accept = $self->{api_client}->select_header_accept('application/json', 'application/xml'); + if ($_header_accept) { $header_params->{'Accept'} = $_header_accept; } - my @_header_content_type = (); - $header_params->{'Content-Type'} = scalar(@_header_content_type) > 0 ? $_header_content_type[0] : 'application/json'; + $header_params->{'Content-Type'} = $self->{api_client}->select_header_content_type(); # header params @@ -496,12 +496,12 @@ sub new { my $header_params = {}; my $form_params = {}; - my $_header_accept = 'application/json, application/xml'; - if ($_header_accept ne '') { + # 'Accept' and 'Content-Type' header + my $_header_accept = $self->{api_client}->select_header_accept('application/json', 'application/xml'); + if ($_header_accept) { $header_params->{'Accept'} = $_header_accept; } - my @_header_content_type = ('multipart/form-data',); - $header_params->{'Content-Type'} = scalar(@_header_content_type) > 0 ? $_header_content_type[0] : 'application/json'; + $header_params->{'Content-Type'} = $self->{api_client}->select_header_content_type('multipart/form-data', ); diff --git a/samples/client/petstore/perl/lib/WWW/SwaggerClient/StoreApi.pm b/samples/client/petstore/perl/lib/WWW/SwaggerClient/StoreApi.pm index bb361d05a43..d4261c6bbec 100644 --- a/samples/client/petstore/perl/lib/WWW/SwaggerClient/StoreApi.pm +++ b/samples/client/petstore/perl/lib/WWW/SwaggerClient/StoreApi.pm @@ -81,12 +81,12 @@ sub new { my $header_params = {}; my $form_params = {}; - my $_header_accept = 'application/json, application/xml'; - if ($_header_accept ne '') { + # 'Accept' and 'Content-Type' header + my $_header_accept = $self->{api_client}->select_header_accept('application/json', 'application/xml'); + if ($_header_accept) { $header_params->{'Accept'} = $_header_accept; } - my @_header_content_type = (); - $header_params->{'Content-Type'} = scalar(@_header_content_type) > 0 ? $_header_content_type[0] : 'application/json'; + $header_params->{'Content-Type'} = $self->{api_client}->select_header_content_type(); @@ -132,12 +132,12 @@ sub new { my $header_params = {}; my $form_params = {}; - my $_header_accept = 'application/json, application/xml'; - if ($_header_accept ne '') { + # 'Accept' and 'Content-Type' header + my $_header_accept = $self->{api_client}->select_header_accept('application/json', 'application/xml'); + if ($_header_accept) { $header_params->{'Accept'} = $_header_accept; } - my @_header_content_type = (); - $header_params->{'Content-Type'} = scalar(@_header_content_type) > 0 ? $_header_content_type[0] : 'application/json'; + $header_params->{'Content-Type'} = $self->{api_client}->select_header_content_type(); @@ -191,12 +191,12 @@ sub new { my $header_params = {}; my $form_params = {}; - my $_header_accept = 'application/json, application/xml'; - if ($_header_accept ne '') { + # 'Accept' and 'Content-Type' header + my $_header_accept = $self->{api_client}->select_header_accept('application/json', 'application/xml'); + if ($_header_accept) { $header_params->{'Accept'} = $_header_accept; } - my @_header_content_type = (); - $header_params->{'Content-Type'} = scalar(@_header_content_type) > 0 ? $_header_content_type[0] : 'application/json'; + $header_params->{'Content-Type'} = $self->{api_client}->select_header_content_type(); @@ -252,12 +252,12 @@ sub new { my $header_params = {}; my $form_params = {}; - my $_header_accept = 'application/json, application/xml'; - if ($_header_accept ne '') { + # 'Accept' and 'Content-Type' header + my $_header_accept = $self->{api_client}->select_header_accept('application/json', 'application/xml'); + if ($_header_accept) { $header_params->{'Accept'} = $_header_accept; } - my @_header_content_type = (); - $header_params->{'Content-Type'} = scalar(@_header_content_type) > 0 ? $_header_content_type[0] : 'application/json'; + $header_params->{'Content-Type'} = $self->{api_client}->select_header_content_type(); diff --git a/samples/client/petstore/perl/lib/WWW/SwaggerClient/UserApi.pm b/samples/client/petstore/perl/lib/WWW/SwaggerClient/UserApi.pm index d3869b62555..2e5889ba39e 100644 --- a/samples/client/petstore/perl/lib/WWW/SwaggerClient/UserApi.pm +++ b/samples/client/petstore/perl/lib/WWW/SwaggerClient/UserApi.pm @@ -86,12 +86,12 @@ sub new { my $header_params = {}; my $form_params = {}; - my $_header_accept = 'application/json, application/xml'; - if ($_header_accept ne '') { + # 'Accept' and 'Content-Type' header + my $_header_accept = $self->{api_client}->select_header_accept('application/json', 'application/xml'); + if ($_header_accept) { $header_params->{'Accept'} = $_header_accept; } - my @_header_content_type = (); - $header_params->{'Content-Type'} = scalar(@_header_content_type) > 0 ? $_header_content_type[0] : 'application/json'; + $header_params->{'Content-Type'} = $self->{api_client}->select_header_content_type(); @@ -137,12 +137,12 @@ sub new { my $header_params = {}; my $form_params = {}; - my $_header_accept = 'application/json, application/xml'; - if ($_header_accept ne '') { + # 'Accept' and 'Content-Type' header + my $_header_accept = $self->{api_client}->select_header_accept('application/json', 'application/xml'); + if ($_header_accept) { $header_params->{'Accept'} = $_header_accept; } - my @_header_content_type = (); - $header_params->{'Content-Type'} = scalar(@_header_content_type) > 0 ? $_header_content_type[0] : 'application/json'; + $header_params->{'Content-Type'} = $self->{api_client}->select_header_content_type(); @@ -188,12 +188,12 @@ sub new { my $header_params = {}; my $form_params = {}; - my $_header_accept = 'application/json, application/xml'; - if ($_header_accept ne '') { + # 'Accept' and 'Content-Type' header + my $_header_accept = $self->{api_client}->select_header_accept('application/json', 'application/xml'); + if ($_header_accept) { $header_params->{'Accept'} = $_header_accept; } - my @_header_content_type = (); - $header_params->{'Content-Type'} = scalar(@_header_content_type) > 0 ? $_header_content_type[0] : 'application/json'; + $header_params->{'Content-Type'} = $self->{api_client}->select_header_content_type(); @@ -240,12 +240,12 @@ sub new { my $header_params = {}; my $form_params = {}; - my $_header_accept = 'application/json, application/xml'; - if ($_header_accept ne '') { + # 'Accept' and 'Content-Type' header + my $_header_accept = $self->{api_client}->select_header_accept('application/json', 'application/xml'); + if ($_header_accept) { $header_params->{'Accept'} = $_header_accept; } - my @_header_content_type = (); - $header_params->{'Content-Type'} = scalar(@_header_content_type) > 0 ? $_header_content_type[0] : 'application/json'; + $header_params->{'Content-Type'} = $self->{api_client}->select_header_content_type(); # query params if ( exists $args{'username'}) { @@ -296,12 +296,12 @@ sub new { my $header_params = {}; my $form_params = {}; - my $_header_accept = 'application/json, application/xml'; - if ($_header_accept ne '') { + # 'Accept' and 'Content-Type' header + my $_header_accept = $self->{api_client}->select_header_accept('application/json', 'application/xml'); + if ($_header_accept) { $header_params->{'Accept'} = $_header_accept; } - my @_header_content_type = (); - $header_params->{'Content-Type'} = scalar(@_header_content_type) > 0 ? $_header_content_type[0] : 'application/json'; + $header_params->{'Content-Type'} = $self->{api_client}->select_header_content_type(); @@ -349,12 +349,12 @@ sub new { my $header_params = {}; my $form_params = {}; - my $_header_accept = 'application/json, application/xml'; - if ($_header_accept ne '') { + # 'Accept' and 'Content-Type' header + my $_header_accept = $self->{api_client}->select_header_accept('application/json', 'application/xml'); + if ($_header_accept) { $header_params->{'Accept'} = $_header_accept; } - my @_header_content_type = (); - $header_params->{'Content-Type'} = scalar(@_header_content_type) > 0 ? $_header_content_type[0] : 'application/json'; + $header_params->{'Content-Type'} = $self->{api_client}->select_header_content_type(); @@ -411,12 +411,12 @@ sub new { my $header_params = {}; my $form_params = {}; - my $_header_accept = 'application/json, application/xml'; - if ($_header_accept ne '') { + # 'Accept' and 'Content-Type' header + my $_header_accept = $self->{api_client}->select_header_accept('application/json', 'application/xml'); + if ($_header_accept) { $header_params->{'Accept'} = $_header_accept; } - my @_header_content_type = (); - $header_params->{'Content-Type'} = scalar(@_header_content_type) > 0 ? $_header_content_type[0] : 'application/json'; + $header_params->{'Content-Type'} = $self->{api_client}->select_header_content_type(); @@ -472,12 +472,12 @@ sub new { my $header_params = {}; my $form_params = {}; - my $_header_accept = 'application/json, application/xml'; - if ($_header_accept ne '') { + # 'Accept' and 'Content-Type' header + my $_header_accept = $self->{api_client}->select_header_accept('application/json', 'application/xml'); + if ($_header_accept) { $header_params->{'Accept'} = $_header_accept; } - my @_header_content_type = (); - $header_params->{'Content-Type'} = scalar(@_header_content_type) > 0 ? $_header_content_type[0] : 'application/json'; + $header_params->{'Content-Type'} = $self->{api_client}->select_header_content_type(); diff --git a/samples/client/petstore/perl/t/01_pet_api.t b/samples/client/petstore/perl/t/01_pet_api.t index 02a10544c57..7855254e657 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 => 25; +use Test::More tests => 31; use Test::Exception; use lib 'lib'; @@ -12,6 +12,16 @@ use_ok('WWW::SwaggerClient::Object::Tag'); use_ok('WWW::SwaggerClient::Object::Category'); my $api = WWW::SwaggerClient::PetApi->new(); +# test select_header_content_type +is $api->{api_client}->select_header_content_type('application/xml', 'Application/JSON'), 'application/json', 'get the proper content type application/json but not application/xml'; +is $api->{api_client}->select_header_content_type('application/xml'), 'application/xml', 'get the proper content type application/json'; +is $api->{api_client}->select_header_content_type(''), 'application/json', 'get the proper content type application/json (default)'; + +# test select_header_accept +is $api->{api_client}->select_header_accept('application/xml', 'Application/JSON'), 'application/json', 'get the proper accept application/json but not application/xml'; +is $api->{api_client}->select_header_content_type('application/xml'), 'application/xml', 'get the proper accept application/json'; +is $api->{api_client}->select_header_accept(''), undef, 'get the proper accept "undef" (default)'; + my $pet_id = 10008; my $category = WWW::SwaggerClient::Object::Category->new('id' => '22', 'name' => 'perl');