Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/generators/python-aiohttp.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ These options may be applied as additional-properties (cli) or configOptions (pl
|sortModelPropertiesByRequiredFlag|Sort model properties to place required parameters before optional parameters.| |true|
|sortParamsByRequiredFlag|Sort method arguments to place required parameters before optional parameters.| |true|
|supportPython2|support python2. This option has been deprecated and will be removed in the 5.x release.| |false|
|testsUsePythonSrcRoot|generates test under the pythonSrcRoot folder.| |false|
|useNose|use the nose test framework| |false|
|usePythonSrcRootInImports|include pythonSrcRoot in import namespaces.| |false|

Expand Down
1 change: 1 addition & 0 deletions docs/generators/python-blueplanet.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ These options may be applied as additional-properties (cli) or configOptions (pl
|sortModelPropertiesByRequiredFlag|Sort model properties to place required parameters before optional parameters.| |true|
|sortParamsByRequiredFlag|Sort method arguments to place required parameters before optional parameters.| |true|
|supportPython2|support python2. This option has been deprecated and will be removed in the 5.x release.| |false|
|testsUsePythonSrcRoot|generates test under the pythonSrcRoot folder.| |false|
|useNose|use the nose test framework| |false|
|usePythonSrcRootInImports|include pythonSrcRoot in import namespaces.| |false|

Expand Down
1 change: 1 addition & 0 deletions docs/generators/python-flask.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ These options may be applied as additional-properties (cli) or configOptions (pl
|sortModelPropertiesByRequiredFlag|Sort model properties to place required parameters before optional parameters.| |true|
|sortParamsByRequiredFlag|Sort method arguments to place required parameters before optional parameters.| |true|
|supportPython2|support python2. This option has been deprecated and will be removed in the 5.x release.| |false|
|testsUsePythonSrcRoot|generates test under the pythonSrcRoot folder.| |false|
|useNose|use the nose test framework| |false|
|usePythonSrcRootInImports|include pythonSrcRoot in import namespaces.| |false|

Expand Down
7 changes: 6 additions & 1 deletion modules/openapi-generator/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -418,7 +418,12 @@
<artifactId>caffeine</artifactId>
<version>2.8.1</version>
</dependency>

<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
<version>3.19.0</version>
<scope>test</scope>
</dependency>
</dependencies>
<repositories>
<repository>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ public abstract class AbstractPythonConnexionServerCodegen extends AbstractPytho
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";
public static final String MOVE_TESTS_UNDER_PYTHON_SRC_ROOT = "testsUsePythonSrcRoot";
static final String MEDIA_TYPE = "mediaType";

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

public AbstractPythonConnexionServerCodegen(String templateDirectory, boolean fixBodyNameValue) {
super();
Expand Down Expand Up @@ -136,6 +138,8 @@ public AbstractPythonConnexionServerCodegen(String templateDirectory, boolean fi
defaultValue(""));
cliOptions.add(new CliOption(USE_PYTHON_SRC_ROOT_IN_IMPORTS, "include pythonSrcRoot in import namespaces.").
defaultValue("false"));
cliOptions.add(new CliOption(MOVE_TESTS_UNDER_PYTHON_SRC_ROOT, "generates test under the pythonSrcRoot folder.")
.defaultValue("false"));
}

protected void addSupportingFiles() {
Expand Down Expand Up @@ -183,11 +187,17 @@ public void processOpts() {
if (additionalProperties.containsKey(USE_PYTHON_SRC_ROOT_IN_IMPORTS)) {
setUsePythonSrcRootInImports((String) additionalProperties.get(USE_PYTHON_SRC_ROOT_IN_IMPORTS));
}
if (additionalProperties.containsKey(MOVE_TESTS_UNDER_PYTHON_SRC_ROOT)) {
setMoveTestsUnderPythonSrcRoot((String) additionalProperties.get(MOVE_TESTS_UNDER_PYTHON_SRC_ROOT));
}
if (additionalProperties.containsKey(PYTHON_SRC_ROOT)) {
String pythonSrcRoot = (String) additionalProperties.get(PYTHON_SRC_ROOT);
if (moveTestsUnderPythonSrcRoot) {
testPackage = pythonSrcRoot + "." + testPackage;
}
if (usePythonSrcRootInImports) {
// if we prepend the package name if the pythonSrcRoot we get the desired effect.
// but we also need to set pythonSrcRoot itself to "" to ensure all the paths are
// if we prepend the package name with the pythonSrcRoot we get the desired effect.
// But, we also need to set pythonSrcRoot itself to "" to ensure all the paths are
// what we expect.
setPackageName(pythonSrcRoot + "." + packageName);
pythonSrcRoot = "";
Expand All @@ -196,6 +206,7 @@ public void processOpts() {
} else {
setPythonSrcRoot("");
}

supportingFiles.add(new SupportingFile("__main__.mustache", packagePath(), "__main__.py"));
supportingFiles.add(new SupportingFile("util.mustache", packagePath(), "util.py"));
supportingFiles.add(new SupportingFile("typing_utils.mustache", packagePath(), "typing_utils.py"));
Expand Down Expand Up @@ -238,6 +249,9 @@ public void setUsePythonSrcRootInImports(String val) {
this.usePythonSrcRootInImports = Boolean.parseBoolean(val);
}

public void setMoveTestsUnderPythonSrcRoot(String val) {
this.moveTestsUnderPythonSrcRoot = Boolean.parseBoolean(val);
}

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

import static org.assertj.core.api.Assertions.assertThat;
import static org.openapitools.codegen.languages.AbstractPythonConnexionServerCodegen.MOVE_TESTS_UNDER_PYTHON_SRC_ROOT;
import static org.openapitools.codegen.languages.AbstractPythonConnexionServerCodegen.PYTHON_SRC_ROOT;
import static org.openapitools.codegen.languages.AbstractPythonConnexionServerCodegen.USE_PYTHON_SRC_ROOT_IN_IMPORTS;

Expand All @@ -9,11 +11,9 @@
import java.util.Objects;

import com.google.common.collect.ImmutableMap;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
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;

Expand All @@ -29,11 +29,12 @@ public void test(String description, Map<String, Object> additionalProperties, S
codegen.additionalProperties().putAll(additionalProperties);
codegen.processOpts();
String pythonSrcRoot = Objects.toString(codegen.additionalProperties().get(PYTHON_SRC_ROOT), null);
Assert.assertEquals(pythonSrcRoot, expectedValues.pythonSrcRoot);
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);
assertThat(pythonSrcRoot).isEqualTo(expectedValues.pythonSrcRoot);
assertThat(codegen.apiPackage()).isEqualTo(expectedValues.expectedApiPackage);
assertThat(codegen.modelFileFolder()).isEqualTo(expectedValues.expectedModelFileFolder);
assertThat(codegen.apiFileFolder()).isEqualTo(expectedValues.expectedApiFileFolder);
assertThat(codegen.apiTestFileFolder()).isEqualTo(expectedValues.expectedApiTestFileFolder);
assertThat(codegen.toModelImport(modelName)).isEqualTo(expectedValues.expectedImport);
Comment thread
spacether marked this conversation as resolved.
Outdated
}

@DataProvider
Expand All @@ -47,6 +48,7 @@ public Object[][] data() {
"openapi_server.controllers",
platformAgnosticPath("generated-code", "connexion", "openapi_server", "models"),
platformAgnosticPath("generated-code", "connexion", "openapi_server", "controllers"),
platformAgnosticPath("generated-code", "connexion", "test"),
null)
},
new Object[]{
Expand All @@ -57,6 +59,7 @@ public Object[][] data() {
"openapi_server.controllers",
platformAgnosticPath("generated-code", "connexion", "test_root", "openapi_server", "models"),
platformAgnosticPath("generated-code", "connexion", "test_root", "openapi_server", "controllers"),
platformAgnosticPath("generated-code", "connexion", "test"),
"test_root")
},
new Object[]{
Expand All @@ -67,6 +70,19 @@ public Object[][] data() {
"test_root.openapi_server.controllers",
platformAgnosticPath("generated-code", "connexion", "test_root", "openapi_server", "models"),
platformAgnosticPath("generated-code", "connexion", "test_root", "openapi_server", "controllers"),
platformAgnosticPath("generated-code", "connexion", "test"),
null)
},
new Object[]{
"Python src in import and tests under python src root",
ImmutableMap.of(PYTHON_SRC_ROOT, "test_root", USE_PYTHON_SRC_ROOT_IN_IMPORTS, "true",
MOVE_TESTS_UNDER_PYTHON_SRC_ROOT, "true"),
"TestModel",
new ExpectedValues("from test_root.openapi_server.models.test_model import TestModel",
"test_root.openapi_server.controllers",
platformAgnosticPath("generated-code", "connexion", "test_root", "openapi_server", "models"),
platformAgnosticPath("generated-code", "connexion", "test_root", "openapi_server", "controllers"),
platformAgnosticPath("generated-code", "connexion", "test_root", "test"),
null)
},
new Object[]{
Expand All @@ -79,15 +95,26 @@ public Object[][] data() {
"test_root.test_package.controllers",
platformAgnosticPath("generated-code", "connexion", "test_root", "test_package", "models"),
platformAgnosticPath("generated-code", "connexion", "test_root", "test_package", "controllers"),
platformAgnosticPath("generated-code", "connexion", "test"),
null)
},
new Object[]{
"Python src in import with specified package and tests under python src root",
ImmutableMap.of(PYTHON_SRC_ROOT, "test_root",
USE_PYTHON_SRC_ROOT_IN_IMPORTS, "true",
CodegenConstants.PACKAGE_NAME, "test_package",
MOVE_TESTS_UNDER_PYTHON_SRC_ROOT, "true"),
"TestModel",
new ExpectedValues("from test_root.test_package.models.test_model import TestModel",
"test_root.test_package.controllers",
platformAgnosticPath("generated-code", "connexion", "test_root", "test_package", "models"),
platformAgnosticPath("generated-code", "connexion", "test_root", "test_package", "controllers"),
platformAgnosticPath("generated-code", "connexion", "test_root", "test"),
null)
}
};
}

private static String platformAgnosticPath(String... nodes) {
return StringUtils.join(nodes, File.separatorChar);
}

private static class MockAbstractPythonConnexionServerCodegen extends AbstractPythonConnexionServerCodegen {
public MockAbstractPythonConnexionServerCodegen(String templateDirectory, boolean fixBodyNameValue) {
super(templateDirectory, fixBodyNameValue);
Expand All @@ -99,16 +126,22 @@ private static class ExpectedValues {
public final String expectedApiPackage;
public final String expectedModelFileFolder;
public final String expectedApiFileFolder;
public final String expectedApiTestFileFolder;
public final String pythonSrcRoot;

public ExpectedValues(String expectedImport, String expectedApiPackage, String expectedModelFileFolder,
String expectedApiFileFolder, String pythonSrcRoot) {
String expectedApiFileFolder, String expectedApiTestFileFolder, String pythonSrcRoot) {
this.expectedImport = expectedImport;
this.expectedApiPackage = expectedApiPackage;
this.expectedModelFileFolder = expectedModelFileFolder;
this.expectedApiFileFolder = expectedApiFileFolder;
this.expectedApiTestFileFolder = expectedApiTestFileFolder;
this.pythonSrcRoot = pythonSrcRoot != null ? pythonSrcRoot + File.separatorChar : null;
}
}

private static String platformAgnosticPath(String... nodes) {
return StringUtils.join(nodes, File.separatorChar);
}

}