better handling of reserved keyword in perl

This commit is contained in:
wing328 2016-02-24 21:57:29 +08:00
parent 0520e68e29
commit 2ed02ee187
10 changed files with 64 additions and 30 deletions

View File

@ -1798,8 +1798,8 @@ public class DefaultCodegen {
builder.append(part); builder.append(part);
} }
} }
operationId = builder.toString(); operationId = sanitizeName(builder.toString());
LOGGER.info("generated operationId " + operationId + "\tfor Path: " + httpMethod + " " + path); LOGGER.warn("Empty operationId found for path: " + httpMethod + " " + path + ". Renamed to auto-generated operationId: " + operationId);
} }
return operationId; return operationId;
} }

View File

@ -24,6 +24,8 @@ public class PerlClientCodegen extends DefaultCodegen implements CodegenConfig {
protected String modulePathPart = moduleName.replaceAll("::", Matcher.quoteReplacement(File.separator)); protected String modulePathPart = moduleName.replaceAll("::", Matcher.quoteReplacement(File.separator));
protected String moduleVersion = "1.0.0"; protected String moduleVersion = "1.0.0";
protected static int emptyFunctionNameCounter = 0;
public PerlClientCodegen() { public PerlClientCodegen() {
super(); super();
modelPackage = File.separatorChar + "Object"; modelPackage = File.separatorChar + "Object";
@ -46,7 +48,8 @@ public class PerlClientCodegen extends DefaultCodegen implements CodegenConfig {
"cmp", "ge", "package", "until", "cmp", "ge", "package", "until",
"continue", "gt", "q", "while", "continue", "gt", "q", "while",
"CORE", "if", "qq", "xor", "CORE", "if", "qq", "xor",
"do", "le", "qr", "y" "do", "le", "qr", "y",
"return"
) )
); );
@ -261,14 +264,17 @@ public class PerlClientCodegen extends DefaultCodegen implements CodegenConfig {
@Override @Override
public String toOperationId(String operationId) { public String toOperationId(String operationId) {
// throw exception if method name is empty //rename to empty_function_name_1 (e.g.) if method name is empty
if (StringUtils.isEmpty(operationId)) { if (StringUtils.isEmpty(operationId)) {
throw new RuntimeException("Empty method name (operationId) not allowed"); operationId = underscore("empty_function_name_" + emptyFunctionNameCounter++);
LOGGER.warn("Empty method name (operationId) found. Renamed to " + operationId);
return operationId;
} }
// method name cannot use reserved keyword, e.g. return // method name cannot use reserved keyword, e.g. return
if (reservedWords.contains(operationId)) { if (reservedWords.contains(operationId)) {
throw new RuntimeException(operationId + " (reserved word) cannot be used as method name"); LOGGER.warn(operationId + " (reserved word) cannot be used as method name. Renamed to " + underscore("call_" + operationId));
return underscore("call_" + operationId);
} }
return underscore(operationId); return underscore(operationId);

View File

@ -256,14 +256,14 @@
], ],
"summary": "Fake endpoint to test byte array return by 'Find pet by ID'", "summary": "Fake endpoint to test byte array return by 'Find pet by ID'",
"description": "Returns a pet when ID < 10. ID > 10 or nonintegers will simulate API error conditions", "description": "Returns a pet when ID < 10. ID > 10 or nonintegers will simulate API error conditions",
"operationId": "getPetByIdWithByteArray", "operationId": "",
"produces": [ "produces": [
"application/json", "application/json",
"application/xml" "application/xml"
], ],
"parameters": [ "parameters": [
{ {
"name": "petId", "name": "package",
"in": "path", "in": "path",
"description": "ID of pet that needs to be fetched", "description": "ID of pet that needs to be fetched",
"required": true, "required": true,

View File

@ -8,7 +8,7 @@ WWW::SwaggerClient::Role - a Moose role for the Swagger Petstore
Automatically generated by the Perl Swagger Codegen project: Automatically generated by the Perl Swagger Codegen project:
- Build date: 2016-02-11T08:22:52.982+08:00 - Build date: 2016-02-24T21:56:00.847+08:00
- Build package: class io.swagger.codegen.languages.PerlClientCodegen - Build package: class io.swagger.codegen.languages.PerlClientCodegen
- Codegen version: - Codegen version:

View File

@ -319,6 +319,13 @@ sub update_params_for_auth {
if (!defined($auth)) { if (!defined($auth)) {
# TODO show warning about auth setting not defined # TODO show warning about auth setting not defined
} }
elsif ($auth eq 'test_api_key_header') {
my $api_key = $self->get_api_key_with_prefix('test_api_key_header');
if ($api_key) {
$header_params->{'test_api_key_header'} = $api_key;
}
}
elsif ($auth eq 'api_key') { elsif ($auth eq 'api_key') {
my $api_key = $self->get_api_key_with_prefix('api_key'); my $api_key = $self->get_api_key_with_prefix('api_key');
@ -326,6 +333,27 @@ sub update_params_for_auth {
$header_params->{'api_key'} = $api_key; $header_params->{'api_key'} = $api_key;
} }
} }
elsif ($auth eq 'test_api_client_secret') {
my $api_key = $self->get_api_key_with_prefix('x-test_api_client_secret');
if ($api_key) {
$header_params->{'x-test_api_client_secret'} = $api_key;
}
}
elsif ($auth eq 'test_api_client_id') {
my $api_key = $self->get_api_key_with_prefix('x-test_api_client_id');
if ($api_key) {
$header_params->{'x-test_api_client_id'} = $api_key;
}
}
elsif ($auth eq 'test_api_key_query') {
my $api_key = $self->get_api_key_with_prefix('test_api_key_query');
if ($api_key) {
$query_params->{'test_api_key_query'} = $api_key;
}
}
elsif ($auth eq 'petstore_auth') { elsif ($auth eq 'petstore_auth') {
if ($WWW::SwaggerClient::Configuration::access_token) { if ($WWW::SwaggerClient::Configuration::access_token) {

View File

@ -378,7 +378,7 @@ sub get_pet_by_id {
# authentication setting, if any # authentication setting, if any
my $auth_settings = [qw(api_key )]; my $auth_settings = [qw(api_key petstore_auth )];
# make the API Call # make the API Call
my $response = $self->{api_client}->call_api($_resource_path, $_method, my $response = $self->{api_client}->call_api($_resource_path, $_method,
@ -663,20 +663,20 @@ sub upload_file {
} }
# #
# get_pet_by_id_with_byte_array # pet_pet_idtesting_byte_arraytrue_get
# #
# Fake endpoint to test byte array return by 'Find pet by ID' # Fake endpoint to test byte array return by 'Find pet by ID'
# #
# @param int $pet_id ID of pet that needs to be fetched (required) # @param int $package ID of pet that needs to be fetched (required)
{ {
my $params = { my $params = {
'pet_id' => { 'package' => {
data_type => 'int', data_type => 'int',
description => 'ID of pet that needs to be fetched', description => 'ID of pet that needs to be fetched',
required => '1', required => '1',
}, },
}; };
__PACKAGE__->method_documentation->{ get_pet_by_id_with_byte_array } = { __PACKAGE__->method_documentation->{ pet_pet_idtesting_byte_arraytrue_get } = {
summary => 'Fake endpoint to test byte array return by &#39;Find pet by ID&#39;', summary => 'Fake endpoint to test byte array return by &#39;Find pet by ID&#39;',
params => $params, params => $params,
returns => 'string', returns => 'string',
@ -684,13 +684,13 @@ sub upload_file {
} }
# @return string # @return string
# #
sub get_pet_by_id_with_byte_array { sub pet_pet_idtesting_byte_arraytrue_get {
my ($self, %args) = @_; my ($self, %args) = @_;
# verify the required parameter 'pet_id' is set # verify the required parameter 'package' is set
unless (exists $args{'pet_id'}) { unless (exists $args{'package'}) {
croak("Missing the required parameter 'pet_id' when calling get_pet_by_id_with_byte_array"); croak("Missing the required parameter 'package' when calling pet_pet_idtesting_byte_arraytrue_get");
} }
@ -713,9 +713,9 @@ sub get_pet_by_id_with_byte_array {
# path params # path params
if ( exists $args{'pet_id'}) { if ( exists $args{'package'}) {
my $_base_variable = "{" . "petId" . "}"; my $_base_variable = "{" . "package" . "}";
my $_base_value = $self->{api_client}->to_path_value($args{'pet_id'}); my $_base_value = $self->{api_client}->to_path_value($args{'package'});
$_resource_path =~ s/$_base_variable/$_base_value/g; $_resource_path =~ s/$_base_variable/$_base_value/g;
} }
@ -723,7 +723,7 @@ sub get_pet_by_id_with_byte_array {
# authentication setting, if any # authentication setting, if any
my $auth_settings = [qw(api_key )]; my $auth_settings = [qw(api_key petstore_auth )];
# make the API Call # make the API Call
my $response = $self->{api_client}->call_api($_resource_path, $_method, my $response = $self->{api_client}->call_api($_resource_path, $_method,

View File

@ -37,7 +37,7 @@ has version_info => ( is => 'ro',
default => sub { { default => sub { {
app_name => 'Swagger Petstore', app_name => 'Swagger Petstore',
app_version => '1.0.0', app_version => '1.0.0',
generated_date => '2016-02-11T08:22:52.982+08:00', generated_date => '2016-02-24T21:56:00.847+08:00',
generator_class => 'class io.swagger.codegen.languages.PerlClientCodegen', generator_class => 'class io.swagger.codegen.languages.PerlClientCodegen',
} }, } },
documentation => 'Information about the application version and the codegen codebase version' documentation => 'Information about the application version and the codegen codebase version'
@ -103,7 +103,7 @@ Automatically generated by the Perl Swagger Codegen project:
=over 4 =over 4
=item Build date: 2016-02-11T08:22:52.982+08:00 =item Build date: 2016-02-24T21:56:00.847+08:00
=item Build package: class io.swagger.codegen.languages.PerlClientCodegen =item Build package: class io.swagger.codegen.languages.PerlClientCodegen

View File

@ -164,7 +164,7 @@ sub place_order {
} }
# authentication setting, if any # authentication setting, if any
my $auth_settings = [qw()]; my $auth_settings = [qw(test_api_client_id test_api_client_secret )];
# make the API Call # make the API Call
my $response = $self->{api_client}->call_api($_resource_path, $_method, my $response = $self->{api_client}->call_api($_resource_path, $_method,
@ -239,7 +239,7 @@ sub get_order_by_id {
# authentication setting, if any # authentication setting, if any
my $auth_settings = [qw()]; my $auth_settings = [qw(test_api_key_header test_api_key_query )];
# make the API Call # make the API Call
my $response = $self->{api_client}->call_api($_resource_path, $_method, my $response = $self->{api_client}->call_api($_resource_path, $_method,

View File

@ -99,11 +99,11 @@ isa_ok($api, 'WWW::SwaggerClient::PetApi');
} }
# #
# get_pet_by_id_with_byte_array test # pet_pet_idtesting_byte_arraytrue_get test
# #
{ {
my $pet_id = undef; # replace NULL with a proper value my $package = undef; # replace NULL with a proper value
my $result = $api->get_pet_by_id_with_byte_array(pet_id => $pet_id); my $result = $api->pet_pet_idtesting_byte_arraytrue_get(package => $package);
} }
# #

View File

@ -50,7 +50,7 @@ is $get_pet->tags->[0]->id, '11', 'stored and retrieved: got the proper tag id';
# API method docs # API method docs
is_deeply( [sort keys %{$api->pet_api->method_documentation}], is_deeply( [sort keys %{$api->pet_api->method_documentation}],
[ 'add_pet', 'add_pet_using_byte_array', 'delete_pet', 'find_pets_by_status', 'find_pets_by_tags', 'get_pet_by_id', 'get_pet_by_id_with_byte_array', 'update_pet', 'update_pet_with_form', 'upload_file'], [ 'add_pet', 'add_pet_using_byte_array', 'delete_pet', 'find_pets_by_status', 'find_pets_by_tags', 'get_pet_by_id', 'pet_pet_idtesting_byte_arraytrue_get', 'update_pet', 'update_pet_with_form', 'upload_file'],
"Pet API method_documentation has the correct keys"); "Pet API method_documentation has the correct keys");
is $api->pet_api->method_documentation->{get_pet_by_id}->{params}->{pet_id}->{description}, is $api->pet_api->method_documentation->{get_pet_by_id}->{params}->{pet_id}->{description},
'ID of pet that needs to be fetched', 'get_pet_by_id parameter pet_id description is correct'; 'ID of pet that needs to be fetched', 'get_pet_by_id parameter pet_id description is correct';