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).
This commit is contained in:
Sven Panne 2019-02-12 05:20:34 +01:00 committed by William Cheng
parent 4e85993490
commit fd0847864a
2 changed files with 31 additions and 16 deletions

View File

@ -203,10 +203,10 @@ public class PythonClientCodegen extends DefaultCodegen implements CodegenConfig
if (generateSourceCodeOnly) {
// tests in <package>/test
testFolder = packageName + File.separatorChar + testFolder;
testFolder = packagePath() + File.separatorChar + testFolder;
// api/model docs in <package>/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`
* <p>

View File

@ -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;
}};
}
}