From 2ed02ee1877be9220dc27f236295ecd48ed6cecd Mon Sep 17 00:00:00 2001 From: wing328 Date: Wed, 24 Feb 2016 21:57:29 +0800 Subject: [PATCH] better handling of reserved keyword in perl --- .../io/swagger/codegen/DefaultCodegen.java | 4 +-- .../codegen/languages/PerlClientCodegen.java | 14 +++++++--- .../src/test/resources/2_0/petstore.json | 4 +-- samples/client/petstore/perl/README.md | 2 +- .../perl/lib/WWW/SwaggerClient/ApiClient.pm | 28 +++++++++++++++++++ .../perl/lib/WWW/SwaggerClient/PetApi.pm | 26 ++++++++--------- .../perl/lib/WWW/SwaggerClient/Role.pm | 4 +-- .../perl/lib/WWW/SwaggerClient/StoreApi.pm | 4 +-- samples/client/petstore/perl/t/PetApiTest.t | 6 ++-- samples/client/petstore/perl/tests/04_role.t | 2 +- 10 files changed, 64 insertions(+), 30 deletions(-) diff --git a/modules/swagger-codegen/src/main/java/io/swagger/codegen/DefaultCodegen.java b/modules/swagger-codegen/src/main/java/io/swagger/codegen/DefaultCodegen.java index 0ad30ed8459..a2328e8cb73 100644 --- a/modules/swagger-codegen/src/main/java/io/swagger/codegen/DefaultCodegen.java +++ b/modules/swagger-codegen/src/main/java/io/swagger/codegen/DefaultCodegen.java @@ -1798,8 +1798,8 @@ public class DefaultCodegen { builder.append(part); } } - operationId = builder.toString(); - LOGGER.info("generated operationId " + operationId + "\tfor Path: " + httpMethod + " " + path); + operationId = sanitizeName(builder.toString()); + LOGGER.warn("Empty operationId found for path: " + httpMethod + " " + path + ". Renamed to auto-generated operationId: " + operationId); } return operationId; } 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 edb54828468..56ead27f395 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 @@ -24,6 +24,8 @@ public class PerlClientCodegen extends DefaultCodegen implements CodegenConfig { protected String modulePathPart = moduleName.replaceAll("::", Matcher.quoteReplacement(File.separator)); protected String moduleVersion = "1.0.0"; + protected static int emptyFunctionNameCounter = 0; + public PerlClientCodegen() { super(); modelPackage = File.separatorChar + "Object"; @@ -46,7 +48,8 @@ public class PerlClientCodegen extends DefaultCodegen implements CodegenConfig { "cmp", "ge", "package", "until", "continue", "gt", "q", "while", "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 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)) { - 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 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); diff --git a/modules/swagger-codegen/src/test/resources/2_0/petstore.json b/modules/swagger-codegen/src/test/resources/2_0/petstore.json index 6df7d079a10..e067c7b7a63 100644 --- a/modules/swagger-codegen/src/test/resources/2_0/petstore.json +++ b/modules/swagger-codegen/src/test/resources/2_0/petstore.json @@ -256,14 +256,14 @@ ], "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", - "operationId": "getPetByIdWithByteArray", + "operationId": "", "produces": [ "application/json", "application/xml" ], "parameters": [ { - "name": "petId", + "name": "package", "in": "path", "description": "ID of pet that needs to be fetched", "required": true, diff --git a/samples/client/petstore/perl/README.md b/samples/client/petstore/perl/README.md index d0e4d50476e..9f71e181829 100644 --- a/samples/client/petstore/perl/README.md +++ b/samples/client/petstore/perl/README.md @@ -8,7 +8,7 @@ WWW::SwaggerClient::Role - a Moose role for the Swagger Petstore 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 - Codegen version: diff --git a/samples/client/petstore/perl/lib/WWW/SwaggerClient/ApiClient.pm b/samples/client/petstore/perl/lib/WWW/SwaggerClient/ApiClient.pm index fb3ffe6a2e2..3ec07335894 100644 --- a/samples/client/petstore/perl/lib/WWW/SwaggerClient/ApiClient.pm +++ b/samples/client/petstore/perl/lib/WWW/SwaggerClient/ApiClient.pm @@ -319,6 +319,13 @@ sub update_params_for_auth { if (!defined($auth)) { # 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') { 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; } } + 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') { if ($WWW::SwaggerClient::Configuration::access_token) { diff --git a/samples/client/petstore/perl/lib/WWW/SwaggerClient/PetApi.pm b/samples/client/petstore/perl/lib/WWW/SwaggerClient/PetApi.pm index 47dd6b1acc9..3049d26ad23 100644 --- a/samples/client/petstore/perl/lib/WWW/SwaggerClient/PetApi.pm +++ b/samples/client/petstore/perl/lib/WWW/SwaggerClient/PetApi.pm @@ -378,7 +378,7 @@ sub get_pet_by_id { # authentication setting, if any - my $auth_settings = [qw(api_key )]; + my $auth_settings = [qw(api_key petstore_auth )]; # make the API Call 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' # -# @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 = { - 'pet_id' => { + 'package' => { data_type => 'int', description => 'ID of pet that needs to be fetched', 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 'Find pet by ID'', params => $params, returns => 'string', @@ -684,13 +684,13 @@ sub upload_file { } # @return string # -sub get_pet_by_id_with_byte_array { +sub pet_pet_idtesting_byte_arraytrue_get { my ($self, %args) = @_; - # verify the required parameter 'pet_id' is set - unless (exists $args{'pet_id'}) { - croak("Missing the required parameter 'pet_id' when calling get_pet_by_id_with_byte_array"); + # verify the required parameter 'package' is set + unless (exists $args{'package'}) { + 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 - if ( exists $args{'pet_id'}) { - my $_base_variable = "{" . "petId" . "}"; - my $_base_value = $self->{api_client}->to_path_value($args{'pet_id'}); + if ( exists $args{'package'}) { + my $_base_variable = "{" . "package" . "}"; + my $_base_value = $self->{api_client}->to_path_value($args{'package'}); $_resource_path =~ s/$_base_variable/$_base_value/g; } @@ -723,7 +723,7 @@ sub get_pet_by_id_with_byte_array { # authentication setting, if any - my $auth_settings = [qw(api_key )]; + my $auth_settings = [qw(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/lib/WWW/SwaggerClient/Role.pm b/samples/client/petstore/perl/lib/WWW/SwaggerClient/Role.pm index 98484b46c6e..3a4f9614a6d 100644 --- a/samples/client/petstore/perl/lib/WWW/SwaggerClient/Role.pm +++ b/samples/client/petstore/perl/lib/WWW/SwaggerClient/Role.pm @@ -37,7 +37,7 @@ has version_info => ( is => 'ro', default => sub { { app_name => 'Swagger Petstore', 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', } }, 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 -=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 diff --git a/samples/client/petstore/perl/lib/WWW/SwaggerClient/StoreApi.pm b/samples/client/petstore/perl/lib/WWW/SwaggerClient/StoreApi.pm index e28f8a6b260..a2c37435ee9 100644 --- a/samples/client/petstore/perl/lib/WWW/SwaggerClient/StoreApi.pm +++ b/samples/client/petstore/perl/lib/WWW/SwaggerClient/StoreApi.pm @@ -164,7 +164,7 @@ sub place_order { } # authentication setting, if any - my $auth_settings = [qw()]; + my $auth_settings = [qw(test_api_client_id test_api_client_secret )]; # make the API Call my $response = $self->{api_client}->call_api($_resource_path, $_method, @@ -239,7 +239,7 @@ sub get_order_by_id { # authentication setting, if any - my $auth_settings = [qw()]; + my $auth_settings = [qw(test_api_key_header test_api_key_query )]; # make the API Call my $response = $self->{api_client}->call_api($_resource_path, $_method, diff --git a/samples/client/petstore/perl/t/PetApiTest.t b/samples/client/petstore/perl/t/PetApiTest.t index 5bc2a6fbab8..790359a704c 100644 --- a/samples/client/petstore/perl/t/PetApiTest.t +++ b/samples/client/petstore/perl/t/PetApiTest.t @@ -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 $result = $api->get_pet_by_id_with_byte_array(pet_id => $pet_id); + my $package = undef; # replace NULL with a proper value + my $result = $api->pet_pet_idtesting_byte_arraytrue_get(package => $package); } # diff --git a/samples/client/petstore/perl/tests/04_role.t b/samples/client/petstore/perl/tests/04_role.t index a5f47ec53c5..42abf5b4f41 100644 --- a/samples/client/petstore/perl/tests/04_role.t +++ b/samples/client/petstore/perl/tests/04_role.t @@ -50,7 +50,7 @@ is $get_pet->tags->[0]->id, '11', 'stored and retrieved: got the proper tag id'; # API method docs 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"); 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';