From 7756296c25266e15e3546726166c34c8032e13e6 Mon Sep 17 00:00:00 2001 From: Sai Giridhar P Date: Wed, 29 May 2019 17:08:42 +0530 Subject: [PATCH] [Python] ModuleNotFoundError when packagename contains dots (#2992) * feat(python): Support package names with dots * feat(python): Fixing tests * feat(python): Adding comment * fix(python): Fixing indentation * fix(python): Fixing indentation --- .../languages/PythonClientCodegen.java | 24 ++++++++++++------- ..._init__test.mustache => __init__.mustache} | 0 2 files changed, 16 insertions(+), 8 deletions(-) rename modules/openapi-generator/src/main/resources/python/{__init__test.mustache => __init__.mustache} (100%) 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 5160417e0ec..0df25cfd159 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 @@ -41,8 +41,8 @@ public class PythonClientCodegen extends DefaultCodegen implements CodegenConfig public static final String PACKAGE_URL = "packageUrl"; public static final String DEFAULT_LIBRARY = "urllib3"; - protected String packageName; // e.g. petstore_api - protected String packageVersion; + protected String packageName = "openapi_client"; + protected String packageVersion = "1.0.0"; protected String projectName; // for setup.py, e.g. petstore-api protected String packageUrl; protected String apiDocPath = "docs/"; @@ -177,8 +177,6 @@ public class PythonClientCodegen extends DefaultCodegen implements CodegenConfig if (additionalProperties.containsKey(CodegenConstants.PACKAGE_NAME)) { setPackageName((String) additionalProperties.get(CodegenConstants.PACKAGE_NAME)); - } else { - setPackageName("openapi_client"); } if (additionalProperties.containsKey(CodegenConstants.PROJECT_NAME)) { @@ -191,9 +189,7 @@ public class PythonClientCodegen extends DefaultCodegen implements CodegenConfig if (additionalProperties.containsKey(CodegenConstants.PACKAGE_VERSION)) { setPackageVersion((String) additionalProperties.get(CodegenConstants.PACKAGE_VERSION)); - } else { - setPackageVersion("1.0.0"); - } + } Boolean generateSourceCodeOnly = false; if (additionalProperties.containsKey(CodegenConstants.SOURCECODEONLY_GENERATION)) { @@ -241,10 +237,22 @@ public class PythonClientCodegen extends DefaultCodegen implements CodegenConfig 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 the package name consists of dots(openapi.client), then we need to create the directory structure like openapi/client with __init__ files. + String[] packageNameSplits = packageName.split("\\."); + String currentPackagePath = ""; + for (int i = 0; i < packageNameSplits.length-1; i++) { + if (i > 0) { + currentPackagePath = currentPackagePath + File.separatorChar; + } + currentPackagePath = currentPackagePath + packageNameSplits[i]; + supportingFiles.add(new SupportingFile("__init__.mustache", currentPackagePath, "__init__.py")); + } + supportingFiles.add(new SupportingFile("exceptions.mustache", packagePath(), "exceptions.py")); if (Boolean.FALSE.equals(excludeTests)) { - supportingFiles.add(new SupportingFile("__init__test.mustache", testFolder, "__init__.py")); + supportingFiles.add(new SupportingFile("__init__.mustache", testFolder, "__init__.py")); } supportingFiles.add(new SupportingFile("api_client.mustache", packagePath(), "api_client.py")); diff --git a/modules/openapi-generator/src/main/resources/python/__init__test.mustache b/modules/openapi-generator/src/main/resources/python/__init__.mustache similarity index 100% rename from modules/openapi-generator/src/main/resources/python/__init__test.mustache rename to modules/openapi-generator/src/main/resources/python/__init__.mustache