Skip to content
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,31 @@ public File writeToFile(String filename, String contents) throws IOException {
return output;
}

public String mergeFileContent(String fileName, String content) throws IOException {

File f1 = new File(fileName);

if(!f1.exists()){
return content;
}

FileReader fileReader = new FileReader(f1);
BufferedReader bufferedReader = new BufferedReader(fileReader);
String line,existingContent = "";

while((line = bufferedReader.readLine()) != null) {
if(existingContent=="")
existingContent = line;
else
existingContent += System.lineSeparator() + line;
}

if(!existingContent.isEmpty()) {
content = existingContent + System.lineSeparator() + content;
}
return content;
}

public String readTemplate(String name) {
try {
Reader reader = getTemplateReader(name);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -712,7 +712,11 @@ public Reader getTemplate(String name) {
.defaultValue("")
.compile(template);

writeToFile(outputFilename, tmpl.execute(bundle));
String contents = tmpl.execute(bundle);
if(support.merge)
contents = mergeFileContent(outputFilename, contents);
writeToFile(outputFilename, contents);

File written = new File(outputFilename);
files.add(written);
if (config.isEnablePostProcessFile()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ public class SupportingFile {
public String folder;
public String destinationFilename;

// to add details of submodule generated to main module
public boolean merge = false;

public SupportingFile(String templateFile, String destinationFilename) {
this(templateFile, "", destinationFilename);
}
Expand All @@ -32,6 +35,13 @@ public SupportingFile(String templateFile, String folder, String destinationFile
this.destinationFilename = destinationFilename;
}

public SupportingFile(String templateFile, String folder, String destinationFilename, boolean merge) {
this.templateFile = templateFile;
this.folder = folder;
this.destinationFilename = destinationFilename;
this.merge = merge;
}

@Override
public String toString() {
StringBuilder builder = new StringBuilder();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ public class PythonClientCodegen extends DefaultCodegen implements CodegenConfig
public static final String DEFAULT_LIBRARY = "urllib3";

protected String packageName; // e.g. petstore_api
protected String invokerPackage;
protected String packageVersion;
protected String projectName; // for setup.py, e.g. petstore-api
protected String packageUrl;
Expand Down Expand Up @@ -192,11 +193,22 @@ public void processOpts() {
setPackageVersion("1.0.0");
}

if (additionalProperties.containsKey(CodegenConstants.INVOKER_PACKAGE)) {
setInvokerPackage((String) additionalProperties.get(CodegenConstants.INVOKER_PACKAGE));}
else {
setInvokerPackage("");
}

Boolean generateSourceCodeOnly = false;
if (additionalProperties.containsKey(CodegenConstants.SOURCECODEONLY_GENERATION)) {
generateSourceCodeOnly = Boolean.valueOf(additionalProperties.get(CodegenConstants.SOURCECODEONLY_GENERATION).toString());
}

Boolean isSubModule = false;
if(packageName != null)
if(packageName.contains(".") && invokerPackage != "")
isSubModule = true;

additionalProperties.put(CodegenConstants.PROJECT_NAME, projectName);
additionalProperties.put(CodegenConstants.PACKAGE_NAME, packageName);
additionalProperties.put(CodegenConstants.PACKAGE_VERSION, packageVersion);
Expand Down Expand Up @@ -234,7 +246,7 @@ public void processOpts() {
supportingFiles.add(new SupportingFile("travis.mustache", "", ".travis.yml"));
supportingFiles.add(new SupportingFile("setup.mustache", "", "setup.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"));
Expand All @@ -243,16 +255,24 @@ public void processOpts() {
supportingFiles.add(new SupportingFile("__init__test.mustache", testFolder, "__init__.py"));
}

supportingFiles.add(new SupportingFile("api_client.mustache", packagePath(), "api_client.py"));
if(!isSubModule){
supportingFiles.add(new SupportingFile("api_client.mustache", packagePath(), "api_client.py"));
supportingFiles.add(new SupportingFile("configuration.mustache", packagePath(), "configuration.py"));

if ("asyncio".equals(getLibrary())) {
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", packagePath(), "rest.py"));
additionalProperties.put("tornado", "true");
} else {
supportingFiles.add(new SupportingFile("rest.mustache", packagePath(), "rest.py"));
if ("asyncio".equals(getLibrary())) {
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", packagePath(), "rest.py"));
additionalProperties.put("tornado", "true");
} else {
supportingFiles.add(new SupportingFile("rest.mustache", packagePath(), "rest.py"));
}
}
if(isSubModule){
// If file already exists merge i.e merge API and MODELS data of submodule __init__ with main module __init__
supportingFiles.add(new SupportingFile("__init__api_submodule.mustache", invokerPath() + File.separatorChar + apiPackage, "__init__.py", true));
supportingFiles.add(new SupportingFile("__init__model_submodule.mustache", invokerPath() + File.separatorChar + modelPackage, "__init__.py", true));
}

modelPackage = packageName + "." + modelPackage;
Expand Down Expand Up @@ -570,6 +590,10 @@ public void setPackageName(String packageName) {
this.packageName = packageName;
}

public void setInvokerPackage(String invokerPackage) {
this.invokerPackage = invokerPackage;
}

public void setProjectName(String projectName) {
this.projectName = projectName;
}
Expand All @@ -583,9 +607,15 @@ public void setPackageUrl(String packageUrl) {
}

public String packagePath() {
return packageName.replace('.', File.separatorChar);
return this.packageName.replace('.', File.separatorChar);
}

public String invokerPath() {
return this.invokerPackage.replace('.', File.separatorChar);
}



/**
* Generate Python package name from String `packageName`
* <p>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@

# {{apiPackage}}
{{#apiInfo}}{{#apis}}from {{apiPackage}}.{{classVarName}} import {{classname}}
{{/apis}}{{/apiInfo}}

Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@

# {{modelPackage}}
{{#models}}{{#model}}from {{modelPackage}}.{{classFilename}} import {{classname}}{{/model}}
{{/models}}


Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,13 @@ from __future__ import absolute_import
__version__ = "{{packageVersion}}"

# import apis into sdk package
import {{apiPackage}}
{{#apiInfo}}{{#apis}}from {{apiPackage}}.{{classVarName}} import {{classname}}
{{/apis}}{{/apiInfo}}
# import ApiClient
from {{packageName}}.api_client import ApiClient
from {{packageName}}.configuration import Configuration
# import models into sdk package
import {{modelPackage}}
{{#models}}{{#model}}from {{modelPackage}}.{{classFilename}} import {{classname}}
{{/model}}{{/models}}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# coding: utf-8

# flake8: noqa

{{>partial_header}}

from __future__ import absolute_import

__version__ = "{{packageVersion}}"

# import apis into sdk package
import {{apiPackage}}
{{#apiInfo}}{{#apis}}from {{apiPackage}}.{{classVarName}} import {{classname}}
{{/apis}}{{/apiInfo}}


# import models into sdk package
import {{modelPackage}}
{{#models}}{{#model}}from {{modelPackage}}.{{classFilename}} import {{classname}}
{{/model}}{{/models}}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import re # noqa: F401
# python 2 and python 3 compatibility library
import six

from {{packageName}}.api_client import ApiClient



{{#operations}}
Expand All @@ -22,6 +22,7 @@ class {{classname}}(object):

def __init__(self, api_client=None):
if api_client is None:
from {{packageName}}.api_client import ApiClient
api_client = ApiClient()
self.api_client = api_client
{{#operation}}
Expand Down
2 changes: 2 additions & 0 deletions samples/client/petstore-security-test/python/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ import petstore_api
from petstore_api.rest import ApiException
from pprint import pprint


# create an instance of the API class
api_instance = petstore_api.FakeApi(petstore_api.ApiClient(configuration))
test_code_inject____end____rn_n_r = 'test_code_inject____end____rn_n_r_example' # str | To test code injection */ ' \\\" =end -- \\\\r\\\\n \\\\n \\\\r (optional)
Expand Down Expand Up @@ -86,6 +87,7 @@ Class | Method | HTTP request | Description
- **API key parameter name**: api_key */ ' " =end -- \r\n \n \r
- **Location**: HTTP header


## petstore_auth

- **Type**: OAuth
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ To test code injection */ ' \" =end -- \\r\\n \\n \\r
To test code injection */ ' \" =end -- \\r\\n \\n \\r

### Example

```python
from __future__ import print_function
import time
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,12 @@
__version__ = "1.0.0"

# import apis into sdk package
import petstore_api.api
from petstore_api.api.fake_api import FakeApi

# import ApiClient
from petstore_api.api_client import ApiClient
from petstore_api.configuration import Configuration
# import models into sdk package
import petstore_api.models
from petstore_api.models.model_return import ModelReturn
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
# python 2 and python 3 compatibility library
import six

from petstore_api.api_client import ApiClient



class FakeApi(object):
Expand All @@ -30,6 +30,7 @@ class FakeApi(object):

def __init__(self, api_client=None):
if api_client is None:
from petstore_api.api_client import ApiClient
api_client = ApiClient()
self.api_client = api_client

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ def __call_api(
query_params=None, header_params=None, body=None, post_params=None,
files=None, response_type=None, auth_settings=None,
_return_http_data_only=None, collection_formats=None,
_preload_content=True, _request_timeout=None):
_preload_content=True, _request_timeout=None, _host=None):

config = self.configuration

Expand Down Expand Up @@ -157,7 +157,11 @@ def __call_api(
body = self.sanitize_for_serialization(body)

# request url
url = self.configuration.host + resource_path
if _host is None:
url = self.configuration.host + resource_path
else:
# use server/host defined in path or operation instead
url = _host + resource_path

# perform request and return response
response_data = self.request(
Expand Down Expand Up @@ -290,7 +294,7 @@ def call_api(self, resource_path, method,
body=None, post_params=None, files=None,
response_type=None, auth_settings=None, async_req=None,
_return_http_data_only=None, collection_formats=None,
_preload_content=True, _request_timeout=None):
_preload_content=True, _request_timeout=None, _host=None):
"""Makes the HTTP request (synchronous) and returns deserialized data.

To make an async_req request, set the async_req parameter.
Expand Down Expand Up @@ -333,7 +337,7 @@ def call_api(self, resource_path, method,
body, post_params, files,
response_type, auth_settings,
_return_http_data_only, collection_formats,
_preload_content, _request_timeout)
_preload_content, _request_timeout, _host)
else:
thread = self.pool.apply_async(self.__call_api, (resource_path,
method, path_params, query_params,
Expand All @@ -342,7 +346,9 @@ def call_api(self, resource_path, method,
response_type, auth_settings,
_return_http_data_only,
collection_formats,
_preload_content, _request_timeout))
_preload_content,
_request_timeout,
_host))
return thread

def request(self, method, url, query_params=None, headers=None,
Expand Down
Loading