Skip to content

Commit 7756296

Browse files
saigiridhar21wing328
authored andcommitted
[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
1 parent 44e4dc3 commit 7756296

File tree

2 files changed

+16
-8
lines changed

2 files changed

+16
-8
lines changed

modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/PythonClientCodegen.java

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,8 @@ public class PythonClientCodegen extends DefaultCodegen implements CodegenConfig
4141
public static final String PACKAGE_URL = "packageUrl";
4242
public static final String DEFAULT_LIBRARY = "urllib3";
4343

44-
protected String packageName; // e.g. petstore_api
45-
protected String packageVersion;
44+
protected String packageName = "openapi_client";
45+
protected String packageVersion = "1.0.0";
4646
protected String projectName; // for setup.py, e.g. petstore-api
4747
protected String packageUrl;
4848
protected String apiDocPath = "docs/";
@@ -177,8 +177,6 @@ public void processOpts() {
177177

178178
if (additionalProperties.containsKey(CodegenConstants.PACKAGE_NAME)) {
179179
setPackageName((String) additionalProperties.get(CodegenConstants.PACKAGE_NAME));
180-
} else {
181-
setPackageName("openapi_client");
182180
}
183181

184182
if (additionalProperties.containsKey(CodegenConstants.PROJECT_NAME)) {
@@ -191,9 +189,7 @@ public void processOpts() {
191189

192190
if (additionalProperties.containsKey(CodegenConstants.PACKAGE_VERSION)) {
193191
setPackageVersion((String) additionalProperties.get(CodegenConstants.PACKAGE_VERSION));
194-
} else {
195-
setPackageVersion("1.0.0");
196-
}
192+
}
197193

198194
Boolean generateSourceCodeOnly = false;
199195
if (additionalProperties.containsKey(CodegenConstants.SOURCECODEONLY_GENERATION)) {
@@ -241,10 +237,22 @@ public void processOpts() {
241237
supportingFiles.add(new SupportingFile("__init__package.mustache", packagePath(), "__init__.py"));
242238
supportingFiles.add(new SupportingFile("__init__model.mustache", packagePath() + File.separatorChar + modelPackage, "__init__.py"));
243239
supportingFiles.add(new SupportingFile("__init__api.mustache", packagePath() + File.separatorChar + apiPackage, "__init__.py"));
240+
241+
// If the package name consists of dots(openapi.client), then we need to create the directory structure like openapi/client with __init__ files.
242+
String[] packageNameSplits = packageName.split("\\.");
243+
String currentPackagePath = "";
244+
for (int i = 0; i < packageNameSplits.length-1; i++) {
245+
if (i > 0) {
246+
currentPackagePath = currentPackagePath + File.separatorChar;
247+
}
248+
currentPackagePath = currentPackagePath + packageNameSplits[i];
249+
supportingFiles.add(new SupportingFile("__init__.mustache", currentPackagePath, "__init__.py"));
250+
}
251+
244252
supportingFiles.add(new SupportingFile("exceptions.mustache", packagePath(), "exceptions.py"));
245253

246254
if (Boolean.FALSE.equals(excludeTests)) {
247-
supportingFiles.add(new SupportingFile("__init__test.mustache", testFolder, "__init__.py"));
255+
supportingFiles.add(new SupportingFile("__init__.mustache", testFolder, "__init__.py"));
248256
}
249257

250258
supportingFiles.add(new SupportingFile("api_client.mustache", packagePath(), "api_client.py"));

0 commit comments

Comments
 (0)