From 7dca394ecae5e87111cef4e90588bed7f00a984a Mon Sep 17 00:00:00 2001 From: Andrew B Date: Mon, 13 Jul 2015 14:46:56 -0700 Subject: [PATCH 1/3] Support for customizing Composer package and namespaces --- .../codegen/languages/PhpClientCodegen.java | 72 ++++++++++++++----- .../src/main/resources/php/composer.mustache | 2 +- .../SwaggerClient-php/lib/Model/Category.php | 2 +- .../php/SwaggerClient-php/lib/Model/Order.php | 2 +- .../php/SwaggerClient-php/lib/Model/Pet.php | 2 +- .../php/SwaggerClient-php/lib/Model/Tag.php | 2 +- .../php/SwaggerClient-php/lib/Model/User.php | 2 +- samples/client/petstore/php/test.php | 4 +- 8 files changed, 62 insertions(+), 26 deletions(-) diff --git a/modules/swagger-codegen/src/main/java/io/swagger/codegen/languages/PhpClientCodegen.java b/modules/swagger-codegen/src/main/java/io/swagger/codegen/languages/PhpClientCodegen.java index 4034b966cb6..65aeaf4791a 100644 --- a/modules/swagger-codegen/src/main/java/io/swagger/codegen/languages/PhpClientCodegen.java +++ b/modules/swagger-codegen/src/main/java/io/swagger/codegen/languages/PhpClientCodegen.java @@ -17,8 +17,8 @@ import java.util.HashSet; public class PhpClientCodegen extends DefaultCodegen implements CodegenConfig { protected String invokerPackage = "Swagger\\Client"; - protected String groupId = "swagger"; - protected String artifactId = "swagger-client"; + protected String composerVendorName = "swagger"; + protected String composerProjectName = "swagger-client"; protected String packagePath = "SwaggerClient-php"; protected String artifactVersion = "1.0.0"; protected String srcBasePath = "lib"; @@ -38,15 +38,6 @@ public class PhpClientCodegen extends DefaultCodegen implements CodegenConfig { "__halt_compiler", "abstract", "and", "array", "as", "break", "callable", "case", "catch", "class", "clone", "const", "continue", "declare", "default", "die", "do", "echo", "else", "elseif", "empty", "enddeclare", "endfor", "endforeach", "endif", "endswitch", "endwhile", "eval", "exit", "extends", "final", "for", "foreach", "function", "global", "goto", "if", "implements", "include", "include_once", "instanceof", "insteadof", "interface", "isset", "list", "namespace", "new", "or", "print", "private", "protected", "public", "require", "require_once", "return", "static", "switch", "throw", "trait", "try", "unset", "use", "var", "while", "xor") ); - additionalProperties.put("invokerPackage", invokerPackage); - additionalProperties.put("modelPackage", modelPackage); - additionalProperties.put("apiPackage", apiPackage); - additionalProperties.put("srcBasePath", srcBasePath); - additionalProperties.put("escapedInvokerPackage", invokerPackage.replace("\\", "\\\\")); - additionalProperties.put("groupId", groupId); - additionalProperties.put("artifactId", artifactId); - additionalProperties.put("artifactVersion", artifactVersion); - // ref: http://php.net/manual/en/language.types.intro.php languageSpecificPrimitives = new HashSet( Arrays.asList( @@ -86,8 +77,12 @@ public class PhpClientCodegen extends DefaultCodegen implements CodegenConfig { typeMapping.put("object", "object"); typeMapping.put("DateTime", "\\DateTime"); - cliOptions.add(new CliOption("packagePath", "main package name for classes")); - cliOptions.add(new CliOption("srcBasePath", "directory directory under packagePath to serve as source root")); + cliOptions.add(new CliOption("invokerPackage", "The main namespace to use for all classes.")); + cliOptions.add(new CliOption("packagePath", "The main package name for classes.")); + cliOptions.add(new CliOption("srcBasePath", "The directory under packagePath to serve as source root.")); + cliOptions.add(new CliOption("composerVendorName", "The vendor name used in the composer package name. The template uses {{composerVendorName}}/{{composerProjectName}} for the composer package name.")); + cliOptions.add(new CliOption("composerProjectName", "The project name used in the composer package name. The template uses {{composerVendorName}}/{{composerProjectName}} for the composer package name.")); + cliOptions.add(new CliOption("artifactVersion", "The composer package version used to generate the composer.json file.")); } public String getPackagePath() { @@ -141,23 +136,52 @@ public class PhpClientCodegen extends DefaultCodegen implements CodegenConfig { if (additionalProperties.containsKey("packagePath")) { this.setPackagePath((String) additionalProperties.get("packagePath")); + } else { + additionalProperties.put("packagePath", packagePath); } if (additionalProperties.containsKey("srcBasePath")) { this.setSrcBasePath((String) additionalProperties.get("srcBasePath")); + } else { + additionalProperties.put("srcBasePath", srcBasePath); } - + + if (additionalProperties.containsKey("invokerPackage")) { + this.setInvokerPackage((String) additionalProperties.get("invokerPackage")); + } else { + additionalProperties.put("invokerPackage", invokerPackage); + } + if (additionalProperties.containsKey("modelPackage")) { this.setModelPackage((String) additionalProperties.get("modelPackage")); + } else { + additionalProperties.put("modelPackage", this.invokerPackage + modelPackage); } if (additionalProperties.containsKey("apiPackage")) { this.setApiPackage((String) additionalProperties.get("apiPackage")); + } else { + additionalProperties.put("apiPackage", apiPackage); } - - additionalProperties.put("srcBasePath", srcBasePath); - additionalProperties.put("modelPackage", modelPackage); - additionalProperties.put("apiPackage", apiPackage); + + if (additionalProperties.containsKey("composerProjectName")) { + this.setComposerProjectName((String) additionalProperties.get("composerProjectName")); + } else { + additionalProperties.put("composerProjectName", composerProjectName); + } + + if (additionalProperties.containsKey("composerVendorName")) { + this.setComposerVendorName((String) additionalProperties.get("composerVendorName")); + } else { + additionalProperties.put("composerVendorName", composerVendorName); + } + + if (additionalProperties.containsKey("artifactVersion")) { + this.setArtifactVersion((String) additionalProperties.get("artifactVersion")); + } else { + additionalProperties.put("artifactVersion", artifactVersion); + } + additionalProperties.put("escapedInvokerPackage", invokerPackage.replace("\\", "\\\\")); supportingFiles.add(new SupportingFile("configuration.mustache", toPackagePath(invokerPackage, srcBasePath), "Configuration.php")); @@ -235,6 +259,10 @@ public class PhpClientCodegen extends DefaultCodegen implements CodegenConfig { public void setInvokerPackage(String invokerPackage) { this.invokerPackage = invokerPackage; } + + public void setArtifactVersion(String artifactVersion) { + this.artifactVersion = artifactVersion; + } public void setPackagePath(String packagePath) { this.packagePath = packagePath; @@ -243,6 +271,14 @@ public class PhpClientCodegen extends DefaultCodegen implements CodegenConfig { public void setSrcBasePath(String srcBasePath) { this.srcBasePath = srcBasePath; } + + private void setComposerVendorName(String composerVendorName) { + this.composerVendorName = composerVendorName; + } + + public void setComposerProjectName(String composerProjectName) { + this.composerProjectName = composerProjectName; + } @Override public String toVarName(String name) { diff --git a/modules/swagger-codegen/src/main/resources/php/composer.mustache b/modules/swagger-codegen/src/main/resources/php/composer.mustache index 52425899ce7..994b6f1ccc7 100644 --- a/modules/swagger-codegen/src/main/resources/php/composer.mustache +++ b/modules/swagger-codegen/src/main/resources/php/composer.mustache @@ -1,5 +1,5 @@ { - "name": "{{groupId}}/{{artifactId}}",{{#artifactVersion}} + "name": "{{composerVendorName}}/{{composerProjectName}}",{{#artifactVersion}} "version": "{{artifactVersion}}",{{/artifactVersion}} "description": "{{description}}", "keywords": [ diff --git a/samples/client/petstore/php/SwaggerClient-php/lib/Model/Category.php b/samples/client/petstore/php/SwaggerClient-php/lib/Model/Category.php index 444c3c1ed7e..b045eb303d5 100644 --- a/samples/client/petstore/php/SwaggerClient-php/lib/Model/Category.php +++ b/samples/client/petstore/php/SwaggerClient-php/lib/Model/Category.php @@ -31,7 +31,7 @@ * Do not edit the class manually. */ -namespace Swagger\Client\Model; +namespace Swagger\ClientSwagger\Client\Model; use \ArrayAccess; /** diff --git a/samples/client/petstore/php/SwaggerClient-php/lib/Model/Order.php b/samples/client/petstore/php/SwaggerClient-php/lib/Model/Order.php index b803bd7325e..4f2ab1b98cb 100644 --- a/samples/client/petstore/php/SwaggerClient-php/lib/Model/Order.php +++ b/samples/client/petstore/php/SwaggerClient-php/lib/Model/Order.php @@ -31,7 +31,7 @@ * Do not edit the class manually. */ -namespace Swagger\Client\Model; +namespace Swagger\ClientSwagger\Client\Model; use \ArrayAccess; /** diff --git a/samples/client/petstore/php/SwaggerClient-php/lib/Model/Pet.php b/samples/client/petstore/php/SwaggerClient-php/lib/Model/Pet.php index 6871ea9d9be..4e286a84234 100644 --- a/samples/client/petstore/php/SwaggerClient-php/lib/Model/Pet.php +++ b/samples/client/petstore/php/SwaggerClient-php/lib/Model/Pet.php @@ -31,7 +31,7 @@ * Do not edit the class manually. */ -namespace Swagger\Client\Model; +namespace Swagger\ClientSwagger\Client\Model; use \ArrayAccess; /** diff --git a/samples/client/petstore/php/SwaggerClient-php/lib/Model/Tag.php b/samples/client/petstore/php/SwaggerClient-php/lib/Model/Tag.php index bcecb32fa7e..06f25feb2e8 100644 --- a/samples/client/petstore/php/SwaggerClient-php/lib/Model/Tag.php +++ b/samples/client/petstore/php/SwaggerClient-php/lib/Model/Tag.php @@ -31,7 +31,7 @@ * Do not edit the class manually. */ -namespace Swagger\Client\Model; +namespace Swagger\ClientSwagger\Client\Model; use \ArrayAccess; /** diff --git a/samples/client/petstore/php/SwaggerClient-php/lib/Model/User.php b/samples/client/petstore/php/SwaggerClient-php/lib/Model/User.php index 1650635d8ce..c253ebfd0d6 100644 --- a/samples/client/petstore/php/SwaggerClient-php/lib/Model/User.php +++ b/samples/client/petstore/php/SwaggerClient-php/lib/Model/User.php @@ -31,7 +31,7 @@ * Do not edit the class manually. */ -namespace Swagger\Client\Model; +namespace Swagger\ClientSwagger\Client\Model; use \ArrayAccess; /** diff --git a/samples/client/petstore/php/test.php b/samples/client/petstore/php/test.php index 79cc4ca4ad7..099e2975a4a 100644 --- a/samples/client/petstore/php/test.php +++ b/samples/client/petstore/php/test.php @@ -21,7 +21,7 @@ try { //$api_client = new Swagger\Client\ApiClient('http://petstore.swagger.io/v2'); //$api_client->getConfig()->addDefaultHeader("test1", "value1"); //$pet_api = new Swagger\Client\PetAPI($api_client); - $pet_api = new Swagger\Client\Api\PetAPI(); + $pet_api = new Swagger\Client\Api\PetApi(); $pet_api->getApiClient()->getConfig()->setTempFolderPath('/var/tmp/php/'); // test default header //$pet_api->getApiClient()->addDefaultHeader("TEST_API_KEY", "09182sdkanafndsl903"); @@ -47,7 +47,7 @@ try { $new_pet->setTags(array($tag)); $new_pet->setCategory($category); - $pet_api = new Swagger\Client\Api\PetAPI(); + $pet_api = new Swagger\Client\Api\PetApi(); // add a new pet (model) $add_response = $pet_api->addPet($new_pet); From b70a0bf0c62543baf131400180d79f826b61ebc0 Mon Sep 17 00:00:00 2001 From: Andrew B Date: Mon, 13 Jul 2015 14:56:33 -0700 Subject: [PATCH 2/3] Fixing an issue with errant namespace in the sample petstore --- .../java/io/swagger/codegen/languages/PhpClientCodegen.java | 2 +- .../petstore/php/SwaggerClient-php/lib/Model/Category.php | 2 +- .../client/petstore/php/SwaggerClient-php/lib/Model/Order.php | 2 +- samples/client/petstore/php/SwaggerClient-php/lib/Model/Pet.php | 2 +- samples/client/petstore/php/SwaggerClient-php/lib/Model/Tag.php | 2 +- .../client/petstore/php/SwaggerClient-php/lib/Model/User.php | 2 +- 6 files changed, 6 insertions(+), 6 deletions(-) diff --git a/modules/swagger-codegen/src/main/java/io/swagger/codegen/languages/PhpClientCodegen.java b/modules/swagger-codegen/src/main/java/io/swagger/codegen/languages/PhpClientCodegen.java index 65aeaf4791a..531e9a27b2d 100644 --- a/modules/swagger-codegen/src/main/java/io/swagger/codegen/languages/PhpClientCodegen.java +++ b/modules/swagger-codegen/src/main/java/io/swagger/codegen/languages/PhpClientCodegen.java @@ -155,7 +155,7 @@ public class PhpClientCodegen extends DefaultCodegen implements CodegenConfig { if (additionalProperties.containsKey("modelPackage")) { this.setModelPackage((String) additionalProperties.get("modelPackage")); } else { - additionalProperties.put("modelPackage", this.invokerPackage + modelPackage); + additionalProperties.put("modelPackage", modelPackage); } if (additionalProperties.containsKey("apiPackage")) { diff --git a/samples/client/petstore/php/SwaggerClient-php/lib/Model/Category.php b/samples/client/petstore/php/SwaggerClient-php/lib/Model/Category.php index b045eb303d5..444c3c1ed7e 100644 --- a/samples/client/petstore/php/SwaggerClient-php/lib/Model/Category.php +++ b/samples/client/petstore/php/SwaggerClient-php/lib/Model/Category.php @@ -31,7 +31,7 @@ * Do not edit the class manually. */ -namespace Swagger\ClientSwagger\Client\Model; +namespace Swagger\Client\Model; use \ArrayAccess; /** diff --git a/samples/client/petstore/php/SwaggerClient-php/lib/Model/Order.php b/samples/client/petstore/php/SwaggerClient-php/lib/Model/Order.php index 4f2ab1b98cb..b803bd7325e 100644 --- a/samples/client/petstore/php/SwaggerClient-php/lib/Model/Order.php +++ b/samples/client/petstore/php/SwaggerClient-php/lib/Model/Order.php @@ -31,7 +31,7 @@ * Do not edit the class manually. */ -namespace Swagger\ClientSwagger\Client\Model; +namespace Swagger\Client\Model; use \ArrayAccess; /** diff --git a/samples/client/petstore/php/SwaggerClient-php/lib/Model/Pet.php b/samples/client/petstore/php/SwaggerClient-php/lib/Model/Pet.php index 4e286a84234..6871ea9d9be 100644 --- a/samples/client/petstore/php/SwaggerClient-php/lib/Model/Pet.php +++ b/samples/client/petstore/php/SwaggerClient-php/lib/Model/Pet.php @@ -31,7 +31,7 @@ * Do not edit the class manually. */ -namespace Swagger\ClientSwagger\Client\Model; +namespace Swagger\Client\Model; use \ArrayAccess; /** diff --git a/samples/client/petstore/php/SwaggerClient-php/lib/Model/Tag.php b/samples/client/petstore/php/SwaggerClient-php/lib/Model/Tag.php index 06f25feb2e8..bcecb32fa7e 100644 --- a/samples/client/petstore/php/SwaggerClient-php/lib/Model/Tag.php +++ b/samples/client/petstore/php/SwaggerClient-php/lib/Model/Tag.php @@ -31,7 +31,7 @@ * Do not edit the class manually. */ -namespace Swagger\ClientSwagger\Client\Model; +namespace Swagger\Client\Model; use \ArrayAccess; /** diff --git a/samples/client/petstore/php/SwaggerClient-php/lib/Model/User.php b/samples/client/petstore/php/SwaggerClient-php/lib/Model/User.php index c253ebfd0d6..1650635d8ce 100644 --- a/samples/client/petstore/php/SwaggerClient-php/lib/Model/User.php +++ b/samples/client/petstore/php/SwaggerClient-php/lib/Model/User.php @@ -31,7 +31,7 @@ * Do not edit the class manually. */ -namespace Swagger\ClientSwagger\Client\Model; +namespace Swagger\Client\Model; use \ArrayAccess; /** From f4226474e2ecfd3db360419b47174049662ef744 Mon Sep 17 00:00:00 2001 From: Andrew B Date: Mon, 13 Jul 2015 15:02:14 -0700 Subject: [PATCH 3/3] Updating command-line parameter description for artifactVersion --- .../java/io/swagger/codegen/languages/PhpClientCodegen.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/modules/swagger-codegen/src/main/java/io/swagger/codegen/languages/PhpClientCodegen.java b/modules/swagger-codegen/src/main/java/io/swagger/codegen/languages/PhpClientCodegen.java index 531e9a27b2d..188e5c76644 100644 --- a/modules/swagger-codegen/src/main/java/io/swagger/codegen/languages/PhpClientCodegen.java +++ b/modules/swagger-codegen/src/main/java/io/swagger/codegen/languages/PhpClientCodegen.java @@ -82,7 +82,7 @@ public class PhpClientCodegen extends DefaultCodegen implements CodegenConfig { cliOptions.add(new CliOption("srcBasePath", "The directory under packagePath to serve as source root.")); cliOptions.add(new CliOption("composerVendorName", "The vendor name used in the composer package name. The template uses {{composerVendorName}}/{{composerProjectName}} for the composer package name.")); cliOptions.add(new CliOption("composerProjectName", "The project name used in the composer package name. The template uses {{composerVendorName}}/{{composerProjectName}} for the composer package name.")); - cliOptions.add(new CliOption("artifactVersion", "The composer package version used to generate the composer.json file.")); + cliOptions.add(new CliOption("artifactVersion", "The version to use in the composer package version field.")); } public String getPackagePath() {