Skip to content

Commit

Permalink
Adding usePythonSrcRootInImports logic to AbstractPythonConnexionServ…
Browse files Browse the repository at this point in the history
…erCodegen. (OpenAPITools#9558)
  • Loading branch information
tray2100 committed May 23, 2021
1 parent ce1e86c commit a09ca63
Show file tree
Hide file tree
Showing 3 changed files with 117 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -600,6 +600,7 @@ public String patternCorrection(String pattern) {

public void setPackageName(String packageName) {
this.packageName = packageName;
additionalProperties.put(CodegenConstants.PACKAGE_NAME, this.packageName);
}

public void setProjectName(String projectName) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ public abstract class AbstractPythonConnexionServerCodegen extends AbstractPytho
// nose is a python testing framework, we use pytest if USE_NOSE is unset
public static final String USE_NOSE = "useNose";
public static final String PYTHON_SRC_ROOT = "pythonSrcRoot";
public static final String USE_PYTHON_SRC_ROOT_IN_IMPORTS = "usePythonSrcRootInImports";
static final String MEDIA_TYPE = "mediaType";

protected int serverPort = 8080;
Expand All @@ -62,6 +63,7 @@ public abstract class AbstractPythonConnexionServerCodegen extends AbstractPytho
protected boolean featureCORS = Boolean.FALSE;
protected boolean useNose = Boolean.FALSE;
protected String pythonSrcRoot;
protected boolean usePythonSrcRootInImports = Boolean.FALSE;

public AbstractPythonConnexionServerCodegen(String templateDirectory, boolean fixBodyNameValue) {
super();
Expand Down Expand Up @@ -132,6 +134,9 @@ public AbstractPythonConnexionServerCodegen(String templateDirectory, boolean fi
defaultValue(Boolean.FALSE.toString()));
cliOptions.add(new CliOption(PYTHON_SRC_ROOT, "put python sources in this subdirectory of output folder (defaults to \"\" for). Use this for src/ layout.").
defaultValue(""));
cliOptions.add(new CliOption(USE_PYTHON_SRC_ROOT_IN_IMPORTS, "put python sources in this subdirectory of output folder (defaults " +
"to \"\" for). Use this for src/ layout.").
defaultValue(""));
}

protected void addSupportingFiles() {
Expand All @@ -147,7 +152,6 @@ public void processOpts() {
setPackageName((String) additionalProperties.get(CodegenConstants.PACKAGE_NAME));
} else {
setPackageName("openapi_server");
additionalProperties.put(CodegenConstants.PACKAGE_NAME, this.packageName);
}
if (additionalProperties.containsKey(CodegenConstants.PACKAGE_VERSION)) {
setPackageVersion((String) additionalProperties.get(CodegenConstants.PACKAGE_VERSION));
Expand Down Expand Up @@ -177,9 +181,16 @@ public void processOpts() {
if (additionalProperties.containsKey(USE_NOSE)) {
setUseNose((String) additionalProperties.get(USE_NOSE));
}
if (additionalProperties.containsKey(USE_PYTHON_SRC_ROOT_IN_IMPORTS)) {
setUsePythonSrcRootInImports((String) additionalProperties.get(USE_PYTHON_SRC_ROOT_IN_IMPORTS));
}
if (additionalProperties.containsKey(PYTHON_SRC_ROOT)) {
setPythonSrcRoot((String) additionalProperties.get(PYTHON_SRC_ROOT));
additionalProperties.put(PYTHON_SRC_ROOT, pythonSrcRoot);
String pythonSrcRoot = (String) additionalProperties.get(PYTHON_SRC_ROOT);
if (usePythonSrcRootInImports) {
setPackageName(pythonSrcRoot + "." + packageName);
pythonSrcRoot = "";
}
setPythonSrcRoot(pythonSrcRoot);
} else {
setPythonSrcRoot("");
}
Expand Down Expand Up @@ -220,6 +231,10 @@ public void setPythonSrcRoot(String val) {
}
}

public void setUsePythonSrcRootInImports(String val) {
this.usePythonSrcRootInImports = Boolean.parseBoolean(val);
}


public String pythonSrcOutputFolder() {
return outputFolder + File.separator + pythonSrcRoot;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
package org.openapitools.codegen.python;

import static org.openapitools.codegen.languages.AbstractPythonConnexionServerCodegen.PYTHON_SRC_ROOT;
import static org.openapitools.codegen.languages.AbstractPythonConnexionServerCodegen.USE_PYTHON_SRC_ROOT_IN_IMPORTS;

import java.util.Collections;
import java.util.Map;

import com.google.common.collect.ImmutableMap;
import org.openapitools.codegen.CodegenConstants;
import org.openapitools.codegen.languages.AbstractPythonConnexionServerCodegen;
import org.testng.Assert;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;

public class AbstractPythonConnexionServerCodegenTest {

/**
* Passing "description" as first param to succinctly identify the significance of test parameters.
*/
@Test(dataProvider = "data")
public void test(String description, Map<String, Object> additionalProperties, String modelName,
ExpectedValues expectedValues) {
AbstractPythonConnexionServerCodegen codegen = new MockAbstractPythonConnexionServerCodegen("", false);
codegen.additionalProperties().putAll(additionalProperties);
codegen.processOpts();
Assert.assertEquals(codegen.apiPackage(), expectedValues.expectedApiPackage);
Assert.assertEquals(codegen.modelFileFolder(), expectedValues.expectedModelFileFolder);
Assert.assertEquals(codegen.apiFileFolder(), expectedValues.expectedApiFileFolder);
Assert.assertEquals(codegen.toModelImport(modelName), expectedValues.expectedImport);
}

@DataProvider
public Object[][] data() {
return new Object[][]{
new Object[]{
"Default setup",
Collections.emptyMap(),
"TestModel",
new ExpectedValues("from openapi_server.models.test_model import TestModel",
"openapi_server.controllers",
"generated-code/connexion/openapi_server/models",
"generated-code/connexion/openapi_server/controllers")
},
new Object[]{
"Default setup with Python src root",
ImmutableMap.of(PYTHON_SRC_ROOT, "test_root"),
"TestModel",
new ExpectedValues("from openapi_server.models.test_model import TestModel",
"openapi_server.controllers",
"generated-code/connexion/test_root/openapi_server/models",
"generated-code/connexion/test_root/openapi_server/controllers")
},
new Object[]{
"Python src in import",
ImmutableMap.of(PYTHON_SRC_ROOT, "test_root", USE_PYTHON_SRC_ROOT_IN_IMPORTS, "true"),
"TestModel",
new ExpectedValues("from test_root.openapi_server.models.test_model import TestModel",
"test_root.openapi_server.controllers",
"generated-code/connexion/test_root/openapi_server/models",
"generated-code/connexion/test_root/openapi_server/controllers")
},
new Object[]{
"Python src in import with specified package",
ImmutableMap.of(PYTHON_SRC_ROOT, "test_root",
USE_PYTHON_SRC_ROOT_IN_IMPORTS, "true",
CodegenConstants.PACKAGE_NAME, "test_package"),
"TestModel",
new ExpectedValues("from test_root.test_package.models.test_model import TestModel",
"test_root.test_package.controllers",
"generated-code/connexion/test_root/test_package/models",
"generated-code/connexion/test_root/test_package/controllers")
}
};
}

private static class MockAbstractPythonConnexionServerCodegen extends AbstractPythonConnexionServerCodegen {
public MockAbstractPythonConnexionServerCodegen(String templateDirectory, boolean fixBodyNameValue) {
super(templateDirectory, fixBodyNameValue);
}
}

private static class ExpectedValues {
public final String expectedImport;
public final String expectedApiPackage;
public final String expectedModelFileFolder;
public final String expectedApiFileFolder;

public ExpectedValues(String expectedImport, String expectedApiPackage, String expectedModelFileFolder,
String expectedApiFileFolder) {
this.expectedImport = expectedImport;
this.expectedApiPackage = expectedApiPackage;
this.expectedModelFileFolder = expectedModelFileFolder;
this.expectedApiFileFolder = expectedApiFileFolder;
}
}

}

0 comments on commit a09ca63

Please sign in to comment.