Skip to content

[PHP][Python][Perl] add prefix/suffix support to model name #2261

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Feb 28, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -79,3 +79,7 @@ samples/client/petstore/swift/SwaggerClientTests/Pods/Pods.xcodeproj/xcshareddat
samples/client/petstore/csharp/SwaggerClientTest/bin/Debug/
samples/client/petstore/csharp/SwaggerClientTest/obj/Debug/
**/.gradle/

samples/client/petstore/python/dev-requirements.txt.log
samples/client/petstore/python/swagger_client.egg-info/SOURCES.txt
samples/client/petstore/python/.coverage
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ public String getSwaggerType(Property p) {
if (type == null) {
return null;
}
return type;
return toModelName(type);
}

@Override
Expand Down Expand Up @@ -220,9 +220,13 @@ public String toParamName(String name) {
public String toModelName(String name) {
// model name cannot use reserved keyword
if (isReservedWord(name)) {
escapeReservedWord(name); // e.g. return => _return
LOGGER.warn(name + " (reserved word) cannot be used as model name. Renamed to " + camelize("object_" + name));
name = "object_" + name;
}

// add prefix/suffic to model name
name = modelNamePrefix + name + modelNameSuffix;

// camelize the model name
// phone_number => PhoneNumber
return camelize(name);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -370,6 +370,11 @@ public String toModelName(String name) {
name = "object_" + name; // e.g. return => ObjectReturn (after camelize)
}

// add prefix and/or suffic only if name does not start wth \ (e.g. \DateTime)
if (!name.matches("^\\\\.*")) {
name = modelNamePrefix + name + modelNameSuffix;
}

// camelize the model name
// phone_number => PhoneNumber
return camelize(name);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ public String toParamName(String name) {

@Override
public String toModelName(String name) {
name = sanitizeName(name); // FIXME: a parameter should not be assigned. Also declare the methods parameters as 'final'.
name = sanitizeName(modelNamePrefix + name + modelNameSuffix); // FIXME: a parameter should not be assigned. Also declare the methods parameters as 'final'.

// remove dollar sign
name = name.replaceAll("$", "");
Expand All @@ -236,6 +236,14 @@ public String toModelFilename(String name) {
name = "object_" + name; // e.g. return => ObjectReturn (after camelize)
}

if (!StringUtils.isEmpty(modelNamePrefix)) {
name = modelNamePrefix + "_" + name;
}

if (!StringUtils.isEmpty(modelNameSuffix)) {
name = name + "_" + modelNameSuffix;
}

// underscore the model file name
// PhoneNumber => phone_number
return underscore(dropDots(name));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@ public void simpleModelTest() {
Assert.assertEquals(cm.classname, "Sample");
Assert.assertEquals(cm.description, "a sample model");
Assert.assertEquals(cm.vars.size(), 3);
Assert.assertEquals(cm.imports.size(), 1);
// {{imports}} is not used in template
//Assert.assertEquals(cm.imports.size(), 1);

final CodegenProperty property1 = cm.vars.get(0);
Assert.assertEquals(property1.baseName, "id");
Expand Down Expand Up @@ -198,7 +199,8 @@ public void complexMapProperty() {
Assert.assertEquals(cm.classname, "Sample");
Assert.assertEquals(cm.description, "a sample model");
Assert.assertEquals(cm.vars.size(), 1);
Assert.assertEquals(Sets.intersection(cm.imports, Sets.newHashSet("Children")).size(), 1);
// {{imports}} is not used in template
//Assert.assertEquals(Sets.intersection(cm.imports, Sets.newHashSet("Children")).size(), 1);

final CodegenProperty property1 = cm.vars.get(0);
Assert.assertEquals(property1.baseName, "children");
Expand Down Expand Up @@ -239,8 +241,9 @@ public void mapModelTest() {
Assert.assertEquals(cm.classname, "Sample");
Assert.assertEquals(cm.description, "a map model");
Assert.assertEquals(cm.vars.size(), 0);
Assert.assertEquals(cm.imports.size(), 2);
Assert.assertEquals(Sets.intersection(cm.imports, Sets.newHashSet("Children")).size(), 1);
// {{imports}} is not used in template
//Assert.assertEquals(cm.imports.size(), 2);
//Assert.assertEquals(Sets.intersection(cm.imports, Sets.newHashSet("Children")).size(), 1);
}

@DataProvider(name = "modelNames")
Expand Down
2 changes: 1 addition & 1 deletion samples/client/petstore/perl/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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-24T21:56:00.847+08:00
- Build date: 2016-02-27T21:43:00.005+08:00
- Build package: class io.swagger.codegen.languages.PerlClientCodegen
- Codegen version:

Expand Down
20 changes: 10 additions & 10 deletions samples/client/petstore/perl/lib/WWW/SwaggerClient/PetApi.pm
Original file line number Diff line number Diff line change
Expand Up @@ -574,7 +574,7 @@ sub delete_pet {
#
# @param int $pet_id ID of pet to update (required)
# @param string $additional_metadata Additional data to pass to server (optional)
# @param file $file file to upload (optional)
# @param File $file file to upload (optional)
{
my $params = {
'pet_id' => {
Expand All @@ -588,7 +588,7 @@ sub delete_pet {
required => '0',
},
'file' => {
data_type => 'file',
data_type => 'File',
description => 'file to upload',
required => '0',
},
Expand Down Expand Up @@ -667,10 +667,10 @@ sub upload_file {
#
# Fake endpoint to test byte array return by 'Find pet by ID'
#
# @param int $package ID of pet that needs to be fetched (required)
# @param int $pet_id ID of pet that needs to be fetched (required)
{
my $params = {
'package' => {
'pet_id' => {
data_type => 'int',
description => 'ID of pet that needs to be fetched',
required => '1',
Expand All @@ -688,9 +688,9 @@ sub pet_pet_idtesting_byte_arraytrue_get {
my ($self, %args) = @_;


# 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");
# verify the required parameter 'pet_id' is set
unless (exists $args{'pet_id'}) {
croak("Missing the required parameter 'pet_id' when calling pet_pet_idtesting_byte_arraytrue_get");
}


Expand All @@ -713,9 +713,9 @@ sub pet_pet_idtesting_byte_arraytrue_get {


# path params
if ( exists $args{'package'}) {
my $_base_variable = "{" . "package" . "}";
my $_base_value = $self->{api_client}->to_path_value($args{'package'});
if ( exists $args{'pet_id'}) {
my $_base_variable = "{" . "petId" . "}";
my $_base_value = $self->{api_client}->to_path_value($args{'pet_id'});
$_resource_path =~ s/$_base_variable/$_base_value/g;
}

Expand Down
4 changes: 2 additions & 2 deletions samples/client/petstore/perl/lib/WWW/SwaggerClient/Role.pm
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ has version_info => ( is => 'ro',
default => sub { {
app_name => 'Swagger Petstore',
app_version => '1.0.0',
generated_date => '2016-02-24T21:56:00.847+08:00',
generated_date => '2016-02-27T21:43:00.005+08:00',
generator_class => 'class io.swagger.codegen.languages.PerlClientCodegen',
} },
documentation => 'Information about the application version and the codegen codebase version'
Expand Down Expand Up @@ -103,7 +103,7 @@ Automatically generated by the Perl Swagger Codegen project:

=over 4

=item Build date: 2016-02-24T21:56:00.847+08:00
=item Build date: 2016-02-27T21:43:00.005+08:00

=item Build package: class io.swagger.codegen.languages.PerlClientCodegen

Expand Down
4 changes: 2 additions & 2 deletions samples/client/petstore/perl/t/PetApiTest.t
Original file line number Diff line number Diff line change
Expand Up @@ -102,8 +102,8 @@ isa_ok($api, 'WWW::SwaggerClient::PetApi');
# pet_pet_idtesting_byte_arraytrue_get test
#
{
my $package = undef; # replace NULL with a proper value
my $result = $api->pet_pet_idtesting_byte_arraytrue_get(package => $package);
my $pet_id = undef; # replace NULL with a proper value
my $result = $api->pet_pet_idtesting_byte_arraytrue_get(pet_id => $pet_id);
}

#
Expand Down
1 change: 0 additions & 1 deletion samples/client/petstore/python/.coverage

This file was deleted.

4 changes: 0 additions & 4 deletions samples/client/petstore/python/dev-requirements.txt

This file was deleted.

22 changes: 0 additions & 22 deletions samples/client/petstore/python/dev-requirements.txt.log

This file was deleted.

29 changes: 0 additions & 29 deletions samples/client/petstore/python/swagger_client.egg-info/SOURCES.txt

This file was deleted.