Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

remove _api suffix for generated files and import #1

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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";

}
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -676,7 +682,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
Expand All @@ -692,9 +699,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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.*;
Expand All @@ -39,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";
Expand Down Expand Up @@ -184,6 +188,8 @@ public void processOpts() {
additionalProperties.put("apiDocPath", apiDocPath);
additionalProperties.put("modelDocPath", modelDocPath);

additionalProperties.put("titlecase", new TitlecaseLambda());

modelPackage = packageName;
apiPackage = packageName;

Expand Down Expand Up @@ -252,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) {
Expand Down Expand Up @@ -285,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() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -134,20 +134,20 @@ 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) -> {
throw new IllegalStateException("Multiple elements: " + a + ", " + b);
})
.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<String, SecurityScheme> securitySchemeMap = openAPI != null ?
(openAPI.getComponents() != null ? openAPI.getComponents().getSecuritySchemes() : null) : null;
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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+)");
Expand All @@ -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 {
Expand All @@ -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());
Expand Down Expand Up @@ -215,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;
Expand All @@ -235,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("\\.");
Expand All @@ -251,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"));
Expand All @@ -266,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;
Expand Down Expand Up @@ -353,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
Expand All @@ -385,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
Expand All @@ -415,9 +431,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`
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -680,6 +680,7 @@ public void testProcessUserDefinedTemplatesWithConfig() throws IOException {
.setGeneratorName("python")
.setInputSpec("src/test/resources/3_0/petstore.yaml")
.setPackageName("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")
Expand All @@ -705,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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand All @@ -43,6 +44,7 @@ public Map<String, String> createOptions() {
ImmutableMap.Builder<String, String> builder = new ImmutableMap.Builder<String, String>();
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")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 = "";
Expand All @@ -41,6 +42,7 @@ public Map<String, String> createOptions() {
ImmutableMap.Builder<String, String> builder = new ImmutableMap.Builder<String, String>();
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")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
Loading