From fd0847864a6734fb627983b2bc806296ed0197c3 Mon Sep 17 00:00:00 2001 From: Sven Panne Date: Tue, 12 Feb 2019 05:20:34 +0100 Subject: [PATCH] Fixed handling of dotted module names in python generator. (#2016) Previously, if you used a packageName of the form "foo.bar.baz", half of the generated files of the python generator went into a subdirectory "foo/bar/baz" (correct), the other half went into a subdirectory "foo.bar.baz" (incorrect). --- .../languages/PythonClientCodegen.java | 28 +++++++++++-------- .../python/PythonClientOptionsTest.java | 19 ++++++++++--- 2 files changed, 31 insertions(+), 16 deletions(-) diff --git a/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/PythonClientCodegen.java b/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/PythonClientCodegen.java index b250f64ae7c..24803f48190 100644 --- a/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/PythonClientCodegen.java +++ b/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/PythonClientCodegen.java @@ -203,10 +203,10 @@ public class PythonClientCodegen extends DefaultCodegen implements CodegenConfig if (generateSourceCodeOnly) { // tests in /test - testFolder = packageName + File.separatorChar + testFolder; + testFolder = packagePath() + File.separatorChar + testFolder; // api/model docs in /docs - apiDocPath = packageName + File.separatorChar + apiDocPath; - modelDocPath = packageName + File.separatorChar + modelDocPath; + apiDocPath = packagePath() + File.separatorChar + apiDocPath; + modelDocPath = packagePath() + File.separatorChar + modelDocPath; } // make api and model doc path available in mustache template additionalProperties.put("apiDocPath", apiDocPath); @@ -219,7 +219,7 @@ public class PythonClientCodegen extends DefaultCodegen implements CodegenConfig String readmePath = "README.md"; String readmeTemplate = "README.mustache"; if (generateSourceCodeOnly) { - readmePath = packageName + "_" + readmePath; + readmePath = packagePath() + "_" + readmePath; readmeTemplate = "README_onlypackage.mustache"; } supportingFiles.add(new SupportingFile(readmeTemplate, "", readmePath)); @@ -234,25 +234,25 @@ public class PythonClientCodegen extends DefaultCodegen implements CodegenConfig supportingFiles.add(new SupportingFile("travis.mustache", "", ".travis.yml")); supportingFiles.add(new SupportingFile("setup.mustache", "", "setup.py")); } - supportingFiles.add(new SupportingFile("configuration.mustache", packageName, "configuration.py")); - supportingFiles.add(new SupportingFile("__init__package.mustache", packageName, "__init__.py")); - supportingFiles.add(new SupportingFile("__init__model.mustache", packageName + File.separatorChar + modelPackage, "__init__.py")); - supportingFiles.add(new SupportingFile("__init__api.mustache", packageName + File.separatorChar + apiPackage, "__init__.py")); + supportingFiles.add(new SupportingFile("configuration.mustache", packagePath(), "configuration.py")); + supportingFiles.add(new SupportingFile("__init__package.mustache", packagePath(), "__init__.py")); + supportingFiles.add(new SupportingFile("__init__model.mustache", packagePath() + File.separatorChar + modelPackage, "__init__.py")); + supportingFiles.add(new SupportingFile("__init__api.mustache", packagePath() + File.separatorChar + apiPackage, "__init__.py")); if (Boolean.FALSE.equals(excludeTests)) { supportingFiles.add(new SupportingFile("__init__test.mustache", testFolder, "__init__.py")); } - supportingFiles.add(new SupportingFile("api_client.mustache", packageName, "api_client.py")); + supportingFiles.add(new SupportingFile("api_client.mustache", packagePath(), "api_client.py")); if ("asyncio".equals(getLibrary())) { - supportingFiles.add(new SupportingFile("asyncio/rest.mustache", packageName, "rest.py")); + supportingFiles.add(new SupportingFile("asyncio/rest.mustache", packagePath(), "rest.py")); additionalProperties.put("asyncio", "true"); } else if ("tornado".equals(getLibrary())) { - supportingFiles.add(new SupportingFile("tornado/rest.mustache", packageName, "rest.py")); + supportingFiles.add(new SupportingFile("tornado/rest.mustache", packagePath(), "rest.py")); additionalProperties.put("tornado", "true"); } else { - supportingFiles.add(new SupportingFile("rest.mustache", packageName, "rest.py")); + supportingFiles.add(new SupportingFile("rest.mustache", packagePath(), "rest.py")); } modelPackage = packageName + "." + modelPackage; @@ -582,6 +582,10 @@ public class PythonClientCodegen extends DefaultCodegen implements CodegenConfig this.packageUrl = packageUrl; } + public String packagePath() { + return packageName.replace('.', File.separatorChar); + } + /** * Generate Python package name from String `packageName` *

diff --git a/modules/openapi-generator/src/test/java/org/openapitools/codegen/python/PythonClientOptionsTest.java b/modules/openapi-generator/src/test/java/org/openapitools/codegen/python/PythonClientOptionsTest.java index 03f3e80a66f..47d59ba78c4 100644 --- a/modules/openapi-generator/src/test/java/org/openapitools/codegen/python/PythonClientOptionsTest.java +++ b/modules/openapi-generator/src/test/java/org/openapitools/codegen/python/PythonClientOptionsTest.java @@ -24,6 +24,8 @@ import org.openapitools.codegen.CodegenConfig; import org.openapitools.codegen.languages.PythonClientCodegen; import org.openapitools.codegen.options.PythonClientOptionsProvider; +import java.io.File; + public class PythonClientOptionsTest extends AbstractOptionsTest { @Tested @@ -43,11 +45,20 @@ public class PythonClientOptionsTest extends AbstractOptionsTest { protected void setExpectations() { new Expectations(clientCodegen) {{ clientCodegen.setPackageName(PythonClientOptionsProvider.PACKAGE_NAME_VALUE); - clientCodegen.setProjectName(PythonClientOptionsProvider.PROJECT_NAME_VALUE); - clientCodegen.setPackageVersion(PythonClientOptionsProvider.PACKAGE_VERSION_VALUE); - clientCodegen.setPackageUrl(PythonClientOptionsProvider.PACKAGE_URL_VALUE); - // clientCodegen.setLibrary(PythonClientCodegen.DEFAULT_LIBRARY); times = 1; + + clientCodegen.setProjectName(PythonClientOptionsProvider.PROJECT_NAME_VALUE); + times = 1; + + clientCodegen.setPackageVersion(PythonClientOptionsProvider.PACKAGE_VERSION_VALUE); + times = 1; + + clientCodegen.setPackageUrl(PythonClientOptionsProvider.PACKAGE_URL_VALUE); + times = 1; + + clientCodegen.packagePath(); + result = PythonClientOptionsProvider.PACKAGE_NAME_VALUE.replace('.', File.separatorChar); + minTimes = 1; }}; } }