forked from loafle/openapi-generator-original
Merge pull request #756 from wing328/perl_improve_accept_header
[Perl] better logic for accept and content-type, added test cases
This commit is contained in:
@@ -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;
|
||||
|
||||
@@ -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}}'}) {
|
||||
|
||||
Reference in New Issue
Block a user