From f1ba3d5c6481ed6a47ddbe75d1f86a2ac20ece97 Mon Sep 17 00:00:00 2001 From: Andy Date: Sat, 12 Jun 2021 21:57:40 -0400 Subject: [PATCH 1/4] remove _api suffix for generated files and import --- .../codegen/languages/AbstractPythonCodegen.java | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/AbstractPythonCodegen.java b/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/AbstractPythonCodegen.java index eced1ac0e782..b5f89130bd81 100644 --- a/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/AbstractPythonCodegen.java +++ b/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/AbstractPythonCodegen.java @@ -676,7 +676,8 @@ public String toApiFilename(String name) { name = name.replaceAll("-", "_"); // e.g. PhoneNumberApi.py => phone_number_api.py - return underscore(name + "_" + apiNameSuffix); +// return underscore(name + "_" + apiNameSuffix); + return underscore(name); } @Override @@ -692,9 +693,10 @@ public String toApiName(String name) { @Override public String toApiVarName(String name) { if (name.length() == 0) { - return "default_api"; + return "default"; } - return underscore(name + "_" + apiNameSuffix); +// return underscore(name + "_" + apiNameSuffix); + return underscore(name); } protected static String dropDots(String str) { From 0475dab11626693b6e03a311cab55d28ef0ab5df Mon Sep 17 00:00:00 2001 From: Andy Date: Tue, 22 Jun 2021 10:25:59 -0400 Subject: [PATCH 2/4] add titlecase lambda in go mustache --- .../org/openapitools/codegen/languages/GoClientCodegen.java | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/GoClientCodegen.java b/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/GoClientCodegen.java index 11861b579249..0865d58e71cf 100644 --- a/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/GoClientCodegen.java +++ b/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/GoClientCodegen.java @@ -29,6 +29,8 @@ import org.openapitools.codegen.utils.ProcessUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import org.openapitools.codegen.templating.mustache.TitlecaseLambda; + import java.io.File; import java.util.*; @@ -184,6 +186,8 @@ public void processOpts() { additionalProperties.put("apiDocPath", apiDocPath); additionalProperties.put("modelDocPath", modelDocPath); + additionalProperties.put("titlecase", new TitlecaseLambda()); + modelPackage = packageName; apiPackage = packageName; From 1121784ddd0116c173cbef822a2a2a5dc7c98ce1 Mon Sep 17 00:00:00 2001 From: Andy Date: Thu, 8 Jul 2021 23:58:01 -0400 Subject: [PATCH 3/4] add submoduls config args to python client generator --- .../codegen/config/GeneratorSettings.java | 6 ++++++ .../openapitools/codegen/CodegenConstants.java | 4 ++++ .../codegen/config/CodegenConfigurator.java | 9 +++++++++ .../codegen/languages/AbstractPythonCodegen.java | 6 ++++++ .../codegen/languages/GoClientCodegen.java | 9 +++++---- .../codegen/languages/PythonClientCodegen.java | 11 ++++++++++- .../languages/PythonLegacyClientCodegen.java | 15 ++++++++++++--- .../codegen/DefaultGeneratorTest.java | 1 + .../options/PythonClientOptionsProvider.java | 2 ++ .../PythonLegacyClientOptionsProvider.java | 2 ++ .../codegen/python/PythonClientOptionsTest.java | 1 + 11 files changed, 58 insertions(+), 8 deletions(-) diff --git a/modules/openapi-generator-core/src/main/java/org/openapitools/codegen/config/GeneratorSettings.java b/modules/openapi-generator-core/src/main/java/org/openapitools/codegen/config/GeneratorSettings.java index e10440920077..3281add6bdcc 100644 --- a/modules/openapi-generator-core/src/main/java/org/openapitools/codegen/config/GeneratorSettings.java +++ b/modules/openapi-generator-core/src/main/java/org/openapitools/codegen/config/GeneratorSettings.java @@ -500,6 +500,7 @@ public static final class Builder { private String modelPackage; private String invokerPackage; private String packageName; + private String submoduleName; private String apiNameSuffix; private String modelNamePrefix; private String modelNameSuffix; @@ -593,6 +594,11 @@ public Builder withPackageName(String packageName) { return this; } + public Builder withSubmoduleName(String submoduleName) { + this.submoduleName = submoduleName; + return this; + } + /** * Sets the {@code apiNameSuffix} and returns a reference to this Builder so that the methods can be chained together. * diff --git a/modules/openapi-generator/src/main/java/org/openapitools/codegen/CodegenConstants.java b/modules/openapi-generator/src/main/java/org/openapitools/codegen/CodegenConstants.java index 81348f4a774e..9f37d71292a0 100644 --- a/modules/openapi-generator/src/main/java/org/openapitools/codegen/CodegenConstants.java +++ b/modules/openapi-generator/src/main/java/org/openapitools/codegen/CodegenConstants.java @@ -385,4 +385,8 @@ public static enum ENUM_PROPERTY_NAMING_TYPE {camelCase, PascalCase, snake_case, "If true (default), keep the old (incorrect) behaviour that 'additionalProperties' is set to false by default."; public static final String USE_ONEOF_DISCRIMINATOR_LOOKUP = "useOneOfDiscriminatorLookup"; public static final String USE_ONEOF_DISCRIMINATOR_LOOKUP_DESC = "Use the discriminator's mapping in oneOf to speed up the model lookup. IMPORTANT: Validation (e.g. one and only one match in oneOf's schemas) will be skipped."; + + public static final String SUBMODULE_NAME = "submoduleName"; + public static final String SUBMODULE_NAME_DESC = "specify the name of submodule in python generated client"; + } diff --git a/modules/openapi-generator/src/main/java/org/openapitools/codegen/config/CodegenConfigurator.java b/modules/openapi-generator/src/main/java/org/openapitools/codegen/config/CodegenConfigurator.java index cea51acb67fe..e8d6c79676ae 100644 --- a/modules/openapi-generator/src/main/java/org/openapitools/codegen/config/CodegenConfigurator.java +++ b/modules/openapi-generator/src/main/java/org/openapitools/codegen/config/CodegenConfigurator.java @@ -416,6 +416,15 @@ public CodegenConfigurator setPackageName(String packageName) { return this; } + public CodegenConfigurator setSubmoduleName(String submoduleName) { + if (StringUtils.isNotEmpty(submoduleName)) { + addAdditionalProperty(CodegenConstants.SUBMODULE_NAME, submoduleName); + } + generatorSettingsBuilder.withSubmoduleName(submoduleName); + return this; + } + + public CodegenConfigurator setReleaseNote(String releaseNote) { if (StringUtils.isNotEmpty(releaseNote)) { addAdditionalProperty(CodegenConstants.RELEASE_NOTE, releaseNote); diff --git a/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/AbstractPythonCodegen.java b/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/AbstractPythonCodegen.java index b5f89130bd81..0bcd64b5a586 100644 --- a/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/AbstractPythonCodegen.java +++ b/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/AbstractPythonCodegen.java @@ -41,6 +41,7 @@ public abstract class AbstractPythonCodegen extends DefaultCodegen implements Co private final Logger LOGGER = LoggerFactory.getLogger(AbstractPythonCodegen.class); protected String packageName = "openapi_client"; + protected String submoduleName = "cohesity_sdk"; protected String packageVersion = "1.0.0"; protected String projectName; // for setup.py, e.g. petstore-api @@ -603,6 +604,11 @@ public void setPackageName(String packageName) { additionalProperties.put(CodegenConstants.PACKAGE_NAME, this.packageName); } + public void setSubmoduleName(String submoduleName) { + this.submoduleName = submoduleName; + additionalProperties.put(CodegenConstants.SUBMODULE_NAME, this.submoduleName); + } + public void setProjectName(String projectName) { this.projectName = projectName; } diff --git a/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/GoClientCodegen.java b/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/GoClientCodegen.java index 0865d58e71cf..183fc11e30c8 100644 --- a/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/GoClientCodegen.java +++ b/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/GoClientCodegen.java @@ -41,7 +41,9 @@ public class GoClientCodegen extends AbstractGoCodegen { private final Logger LOGGER = LoggerFactory.getLogger(GoClientCodegen.class); protected String packageVersion = "1.0.0"; + protected String modelPath = "models/"; protected String apiDocPath = "docs/"; + protected String utilsPath = "utils/"; protected String modelDocPath = "docs/"; public static final String WITH_XML = "withXml"; public static final String STRUCT_PREFIX = "structPrefix"; @@ -256,7 +258,7 @@ public void processOpts() { supportingFiles.add(new SupportingFile("go.mod.mustache", "", "go.mod")); supportingFiles.add(new SupportingFile("go.sum", "", "go.sum")); supportingFiles.add(new SupportingFile(".travis.yml", "", ".travis.yml")); - supportingFiles.add(new SupportingFile("utils.mustache", "", "utils.go")); + supportingFiles.add(new SupportingFile("utils.mustache", "", utilsPath + File.separatorChar + "utils.go")); } public void setUseOneOfDiscriminatorLookup(boolean useOneOfDiscriminatorLookup) { @@ -289,9 +291,8 @@ public String apiFileFolder() { } @Override - public String modelFileFolder() { - return outputFolder + File.separator; - } +// public String modelFileFolder() { return outputFolder + File.separator; } + public String modelFileFolder() { return (outputFolder + "/" + modelPath).replace('/', File.separatorChar); } @Override public String apiDocFileFolder() { 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 5612b1d7b0ee..cb380c30a709 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 @@ -347,10 +347,19 @@ public String toDefaultValue(Schema p) { return defaultValue; } + private String revisedModelPackage() { + String packageNameGeneral = modelPackage(); + // format import contains special characters, not sure why it handle it like that, it is in the test case + if (packageNameGeneral.equals("models")) + return packageNameGeneral; + else + return packageName + "." + submoduleName + "." + "model"; + } + @Override public String toModelImport(String name) { // name looks like Cat - return "from " + modelPackage() + "." + toModelFilename(name) + " import " + toModelName(name); + return "from " + revisedModelPackage() + "." + toModelFilename(name) + " import " + toModelName(name); } @Override diff --git a/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/PythonLegacyClientCodegen.java b/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/PythonLegacyClientCodegen.java index 138b1c543566..beabcdea0163 100644 --- a/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/PythonLegacyClientCodegen.java +++ b/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/PythonLegacyClientCodegen.java @@ -137,6 +137,8 @@ public PythonLegacyClientCodegen() { cliOptions.add(CliOption.newBoolean(USE_NOSE, "use the nose test framework"). defaultValue(Boolean.FALSE.toString())); cliOptions.add(new CliOption(RECURSION_LIMIT, "Set the recursion limit. If not set, use the system default value.")); + cliOptions.add(new CliOption(CodegenConstants.SUBMODULE_NAME, CodegenConstants.SUBMODULE_NAME_DESC) + .defaultValue("cohesity_sdk")); supportedLibraries.put("urllib3", "urllib3-based client"); supportedLibraries.put("asyncio", "Asyncio-based client (python 3.5+)"); @@ -162,6 +164,10 @@ public void processOpts() { setPackageName((String) additionalProperties.get(CodegenConstants.PACKAGE_NAME)); } + if (additionalProperties.containsKey(CodegenConstants.SUBMODULE_NAME)) { + setSubmoduleName((String) additionalProperties.get(CodegenConstants.SUBMODULE_NAME)); + } + if (additionalProperties.containsKey(CodegenConstants.PROJECT_NAME)) { setProjectName((String) additionalProperties.get(CodegenConstants.PROJECT_NAME)); } else { @@ -177,6 +183,7 @@ public void processOpts() { additionalProperties.put(CodegenConstants.PROJECT_NAME, projectName); additionalProperties.put(CodegenConstants.PACKAGE_NAME, packageName); additionalProperties.put(CodegenConstants.PACKAGE_VERSION, packageVersion); + additionalProperties.put(CodegenConstants.SUBMODULE_NAME, submoduleName); if (additionalProperties.containsKey(CodegenConstants.EXCLUDE_TESTS)) { excludeTests = Boolean.valueOf(additionalProperties.get(CodegenConstants.EXCLUDE_TESTS).toString()); @@ -415,9 +422,11 @@ public void setPackageUrl(String packageUrl) { this.packageUrl = packageUrl; } - public String packagePath() { - return packageName.replace('.', File.separatorChar); - } + public String packagePath() { return packageName.replace('.', File.separatorChar); } +// public String packagePath() { +// return submoduleName.replace('.', File.separatorChar); +//} + /** * Generate Python package name from String `packageName` diff --git a/modules/openapi-generator/src/test/java/org/openapitools/codegen/DefaultGeneratorTest.java b/modules/openapi-generator/src/test/java/org/openapitools/codegen/DefaultGeneratorTest.java index 81869d78f91f..f111fd90bc6b 100644 --- a/modules/openapi-generator/src/test/java/org/openapitools/codegen/DefaultGeneratorTest.java +++ b/modules/openapi-generator/src/test/java/org/openapitools/codegen/DefaultGeneratorTest.java @@ -680,6 +680,7 @@ public void testProcessUserDefinedTemplatesWithConfig() throws IOException { .setGeneratorName("python") .setInputSpec("src/test/resources/3_0/petstore.yaml") .setPackageName("io.something") + .setSubmoduleName("io.something") .setTemplateDir(templates.toAbsolutePath().toString()) .addAdditionalProperty("files", "src/test/resources/sampleConfig.json:\n\t folder: supportingjson "+ "\n\t destinationFilename: supportingconfig.json \n\t templateType: SupportingFiles") diff --git a/modules/openapi-generator/src/test/java/org/openapitools/codegen/options/PythonClientOptionsProvider.java b/modules/openapi-generator/src/test/java/org/openapitools/codegen/options/PythonClientOptionsProvider.java index fc7d74074dd8..6dcd536345a0 100644 --- a/modules/openapi-generator/src/test/java/org/openapitools/codegen/options/PythonClientOptionsProvider.java +++ b/modules/openapi-generator/src/test/java/org/openapitools/codegen/options/PythonClientOptionsProvider.java @@ -26,6 +26,7 @@ public class PythonClientOptionsProvider implements OptionsProvider { public static final String PACKAGE_NAME_VALUE = "swagger_client_python"; public static final String PROJECT_NAME_VALUE = "swagger-client-python"; + public static final String SUBMODULE_NAME_VALUE = "cohesity_sdk"; public static final String PACKAGE_VERSION_VALUE = "1.0.0-SNAPSHOT"; public static final String PACKAGE_URL_VALUE = ""; public static final String USE_NOSE_VALUE = "false"; @@ -43,6 +44,7 @@ public Map createOptions() { ImmutableMap.Builder builder = new ImmutableMap.Builder(); return builder.put(PythonClientCodegen.PACKAGE_URL, PACKAGE_URL_VALUE) .put(CodegenConstants.PACKAGE_NAME, PACKAGE_NAME_VALUE) + .put(CodegenConstants.SUBMODULE_NAME, SUBMODULE_NAME_VALUE) .put(CodegenConstants.PROJECT_NAME, PROJECT_NAME_VALUE) .put(CodegenConstants.PACKAGE_VERSION, PACKAGE_VERSION_VALUE) .put(CodegenConstants.HIDE_GENERATION_TIMESTAMP, "true") diff --git a/modules/openapi-generator/src/test/java/org/openapitools/codegen/options/PythonLegacyClientOptionsProvider.java b/modules/openapi-generator/src/test/java/org/openapitools/codegen/options/PythonLegacyClientOptionsProvider.java index 90e76caea16f..b272aa3de9ba 100644 --- a/modules/openapi-generator/src/test/java/org/openapitools/codegen/options/PythonLegacyClientOptionsProvider.java +++ b/modules/openapi-generator/src/test/java/org/openapitools/codegen/options/PythonLegacyClientOptionsProvider.java @@ -25,6 +25,7 @@ public class PythonLegacyClientOptionsProvider implements OptionsProvider { public static final String PACKAGE_NAME_VALUE = "swagger_client_python"; + public static final String SUBMODULE_NAME_VALUE = "swagger_client_python"; public static final String PROJECT_NAME_VALUE = "swagger-client-python"; public static final String PACKAGE_VERSION_VALUE = "1.0.0-SNAPSHOT"; public static final String PACKAGE_URL_VALUE = ""; @@ -41,6 +42,7 @@ public Map createOptions() { ImmutableMap.Builder builder = new ImmutableMap.Builder(); return builder.put(PythonLegacyClientCodegen.PACKAGE_URL, PACKAGE_URL_VALUE) .put(CodegenConstants.PACKAGE_NAME, PACKAGE_NAME_VALUE) + .put(CodegenConstants.SUBMODULE_NAME, SUBMODULE_NAME_VALUE) .put(CodegenConstants.PROJECT_NAME, PROJECT_NAME_VALUE) .put(CodegenConstants.PACKAGE_VERSION, PACKAGE_VERSION_VALUE) .put(CodegenConstants.SORT_PARAMS_BY_REQUIRED_FLAG, "true") diff --git a/modules/openapi-generator/src/test/java/org/openapitools/codegen/python/PythonClientOptionsTest.java b/modules/openapi-generator/src/test/java/org/openapitools/codegen/python/PythonClientOptionsTest.java index e423c6e85842..a8d14c193e92 100644 --- a/modules/openapi-generator/src/test/java/org/openapitools/codegen/python/PythonClientOptionsTest.java +++ b/modules/openapi-generator/src/test/java/org/openapitools/codegen/python/PythonClientOptionsTest.java @@ -50,5 +50,6 @@ protected void verifyOptions() { verify(clientCodegen).setPackageVersion(PythonClientOptionsProvider.PACKAGE_VERSION_VALUE); verify(clientCodegen).setPackageUrl(PythonClientOptionsProvider.PACKAGE_URL_VALUE); verify(clientCodegen).setUseNose(PythonClientOptionsProvider.USE_NOSE_VALUE); + verify(clientCodegen).setSubmoduleName(PythonClientOptionsProvider.SUBMODULE_NAME_VALUE); } } From 9d741233bc37c943bb3fd24e9800517d1090c248 Mon Sep 17 00:00:00 2001 From: Andy Date: Fri, 9 Jul 2021 12:40:13 -0400 Subject: [PATCH 4/4] move file to corresponding folder under submodule --- .../languages/PythonClientCodegen.java | 8 ++--- .../languages/PythonLegacyClientCodegen.java | 31 ++++++++++++------- .../python/__init__submodule.mustache | 0 .../codegen/DefaultGeneratorTest.java | 6 ++-- .../codegen/python/PythonClientTest.java | 8 ++--- 5 files changed, 31 insertions(+), 22 deletions(-) create mode 100644 modules/openapi-generator/src/main/resources/python/__init__submodule.mustache 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 cb380c30a709..e04cbaf32de7 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 @@ -134,11 +134,11 @@ public void processOpts() { super.processOpts(); modelPackage = packageName + "." + "model"; - supportingFiles.add(new SupportingFile("model_utils.mustache", packagePath(), "model_utils.py")); + supportingFiles.add(new SupportingFile("model_utils.mustache", packagePath() + File.separatorChar + submoduleName, "model_utils.py")); // add the models and apis folders - supportingFiles.add(new SupportingFile("__init__models.mustache", packagePath() + File.separatorChar + "models", "__init__.py")); + // supportingFiles.add(new SupportingFile("__init__models.mustache", packagePath() + File.separatorChar + "models", "__init__.py")); SupportingFile originalInitModel = supportingFiles.stream() .filter(sf -> sf.getTemplateFile().equals("__init__model.mustache")) .reduce((a, b) -> { @@ -146,8 +146,8 @@ public void processOpts() { }) .get(); supportingFiles.remove(originalInitModel); - supportingFiles.add(new SupportingFile("__init__model.mustache", packagePath() + File.separatorChar + "model", "__init__.py")); - supportingFiles.add(new SupportingFile("__init__apis.mustache", packagePath() + File.separatorChar + "apis", "__init__.py")); + supportingFiles.add(new SupportingFile("__init__model.mustache", packagePath() + File.separatorChar + submoduleName + File.separatorChar + "model", "__init__.py")); + // supportingFiles.add(new SupportingFile("__init__apis.mustache", packagePath() + File.separatorChar + "apis", "__init__.py")); // Generate the 'signing.py' module, but only if the 'HTTP signature' security scheme is specified in the OAS. Map securitySchemeMap = openAPI != null ? (openAPI.getComponents() != null ? openAPI.getComponents().getSecuritySchemes() : null) : null; diff --git a/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/PythonLegacyClientCodegen.java b/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/PythonLegacyClientCodegen.java index beabcdea0163..0da7e66df5cc 100644 --- a/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/PythonLegacyClientCodegen.java +++ b/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/PythonLegacyClientCodegen.java @@ -222,7 +222,7 @@ public void processOpts() { } } - String readmePath = "README.md"; + String readmePath = packagePath() + File.separatorChar + submoduleName + File.separatorChar + "README.md"; String readmeTemplate = "README.mustache"; if (generateSourceCodeOnly) { readmePath = packagePath() + "_" + readmePath; @@ -242,10 +242,11 @@ public void processOpts() { supportingFiles.add(new SupportingFile("gitlab-ci.mustache", "", ".gitlab-ci.yml")); supportingFiles.add(new SupportingFile("setup.mustache", "", "setup.py")); } - supportingFiles.add(new SupportingFile("configuration.mustache", packagePath(), "configuration.py")); + supportingFiles.add(new SupportingFile("configuration.mustache", packagePath() + File.separatorChar + submoduleName, "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")); + supportingFiles.add(new SupportingFile("__init__submodule.mustache", packagePath() + File.separatorChar + submoduleName, "__init__.py")); + supportingFiles.add(new SupportingFile("__init__model.mustache", packagePath() + File.separatorChar + submoduleName + File.separatorChar + modelPackage, "__init__.py")); + supportingFiles.add(new SupportingFile("__init__api.mustache", packagePath() + File.separatorChar + submoduleName + 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("\\."); @@ -258,13 +259,13 @@ public void processOpts() { supportingFiles.add(new SupportingFile("__init__.mustache", currentPackagePath, "__init__.py")); } - supportingFiles.add(new SupportingFile("exceptions.mustache", packagePath(), "exceptions.py")); + supportingFiles.add(new SupportingFile("exceptions.mustache", packagePath() + File.separatorChar + submoduleName, "exceptions.py")); if (Boolean.FALSE.equals(excludeTests)) { supportingFiles.add(new SupportingFile("__init__.mustache", testFolder, "__init__.py")); } - supportingFiles.add(new SupportingFile("api_client.mustache", packagePath(), "api_client.py")); + supportingFiles.add(new SupportingFile("api_client.mustache", packagePath() + File.separatorChar + submoduleName, "api_client.py")); if ("asyncio".equals(getLibrary())) { supportingFiles.add(new SupportingFile("asyncio/rest.mustache", packagePath(), "rest.py")); @@ -273,7 +274,7 @@ public void processOpts() { supportingFiles.add(new SupportingFile("tornado/rest.mustache", packagePath(), "rest.py")); additionalProperties.put("tornado", "true"); } else { - supportingFiles.add(new SupportingFile("rest.mustache", packagePath(), "rest.py")); + supportingFiles.add(new SupportingFile("rest.mustache", packagePath() + File.separatorChar + submoduleName, "rest.py")); } modelPackage = this.packageName + "." + modelPackage; @@ -360,12 +361,12 @@ public String getHelp() { @Override public String apiDocFileFolder() { - return (outputFolder + "/" + apiDocPath); + return (outputFolder + "/" + packagePath() + File.separatorChar + submoduleName + File.separatorChar + apiDocPath); } @Override public String modelDocFileFolder() { - return (outputFolder + "/" + modelDocPath); + return (outputFolder + "/" + packagePath() + File.separatorChar + submoduleName + File.separatorChar + modelDocPath); } @Override @@ -392,14 +393,22 @@ public String addRegularExpressionDelimiter(String pattern) { return pattern; } + private String insertSubmoduleToImport(String packageImport) { + String[] arr = packageImport.split("\\."); + String toInsert = "." + submoduleName + "."; + return String.join(toInsert, arr); + } + @Override public String apiFileFolder() { - return outputFolder + File.separatorChar + apiPackage().replace('.', File.separatorChar); + String apiPackageImport = insertSubmoduleToImport(apiPackage()); + return outputFolder + File.separatorChar + apiPackageImport.replace('.', File.separatorChar); } @Override public String modelFileFolder() { - return outputFolder + File.separatorChar + modelPackage().replace('.', File.separatorChar); + String modelPackageImport = insertSubmoduleToImport(modelPackage()); + return outputFolder + File.separatorChar + modelPackageImport.replace('.', File.separatorChar); } @Override diff --git a/modules/openapi-generator/src/main/resources/python/__init__submodule.mustache b/modules/openapi-generator/src/main/resources/python/__init__submodule.mustache new file mode 100644 index 000000000000..e69de29bb2d1 diff --git a/modules/openapi-generator/src/test/java/org/openapitools/codegen/DefaultGeneratorTest.java b/modules/openapi-generator/src/test/java/org/openapitools/codegen/DefaultGeneratorTest.java index f111fd90bc6b..ec23992ee2c8 100644 --- a/modules/openapi-generator/src/test/java/org/openapitools/codegen/DefaultGeneratorTest.java +++ b/modules/openapi-generator/src/test/java/org/openapitools/codegen/DefaultGeneratorTest.java @@ -680,7 +680,7 @@ public void testProcessUserDefinedTemplatesWithConfig() throws IOException { .setGeneratorName("python") .setInputSpec("src/test/resources/3_0/petstore.yaml") .setPackageName("io.something") - .setSubmoduleName("io.something") + .setSubmoduleName("something") .setTemplateDir(templates.toAbsolutePath().toString()) .addAdditionalProperty("files", "src/test/resources/sampleConfig.json:\n\t folder: supportingjson "+ "\n\t destinationFilename: supportingconfig.json \n\t templateType: SupportingFiles") @@ -706,10 +706,10 @@ public void testProcessUserDefinedTemplatesWithConfig() throws IOException { // Assert.assertTrue(new File(output, "sampleConfig.json").exists()); // Generator should report api_client.py as a generated file - TestUtils.ensureContainsFile(files, output, "io/something/api_client.py"); + TestUtils.ensureContainsFile(files, output, "io/something/something/api_client.py"); // Generated file should exist on the filesystem after generation - File apiClient = new File(output, "io/something/api_client.py"); + File apiClient = new File(output, "io/something/something/api_client.py"); Assert.assertTrue(apiClient.exists()); // Generated file should contain our custom packageName diff --git a/modules/openapi-generator/src/test/java/org/openapitools/codegen/python/PythonClientTest.java b/modules/openapi-generator/src/test/java/org/openapitools/codegen/python/PythonClientTest.java index 842d6a2851a3..cdc6d555bdef 100644 --- a/modules/openapi-generator/src/test/java/org/openapitools/codegen/python/PythonClientTest.java +++ b/modules/openapi-generator/src/test/java/org/openapitools/codegen/python/PythonClientTest.java @@ -402,8 +402,8 @@ public void testObjectModelWithRefedAdditionalPropertiesIsGenerated() throws Exc DefaultGenerator generator = new DefaultGenerator(); List files = generator.opts(clientOptInput).generate(); - TestUtils.ensureContainsFile(files, output, "openapi_client/model/a.py"); - TestUtils.ensureContainsFile(files, output, "openapi_client/model/b.py"); + TestUtils.ensureContainsFile(files, output, "openapi_client/cohesity_sdk/model/a.py"); + TestUtils.ensureContainsFile(files, output, "openapi_client/cohesity_sdk/model/b.py"); output.deleteOnExit(); } @@ -420,8 +420,8 @@ public void testFreeFormSchemas() throws Exception { DefaultGenerator generator = new DefaultGenerator(); List files = generator.opts(clientOptInput).generate(); - TestUtils.ensureContainsFile(files, output, "openapi_client/model/free_form_with_validation.py"); - TestUtils.ensureContainsFile(files, output, "openapi_client/model/free_form_interface.py"); + TestUtils.ensureContainsFile(files, output, "openapi_client/cohesity_sdk/model/free_form_with_validation.py"); + TestUtils.ensureContainsFile(files, output, "openapi_client/cohesity_sdk/model/free_form_interface.py"); TestUtils.ensureDoesNotContainsFile(files, output, "openapi_client/model/free_form.py"); output.deleteOnExit(); }