mirror of
https://github.com/valitydev/openapi-generator.git
synced 2024-11-08 03:18:53 +00:00
Merge pull request #864 from geekerzp/python-cli-option
[Python] Enabling cli config options for python generator
This commit is contained in:
commit
e60bd564cb
@ -1,5 +1,6 @@
|
||||
package io.swagger.codegen.languages;
|
||||
|
||||
import io.swagger.codegen.CliOption;
|
||||
import io.swagger.codegen.CodegenConfig;
|
||||
import io.swagger.codegen.CodegenType;
|
||||
import io.swagger.codegen.DefaultCodegen;
|
||||
@ -13,25 +14,19 @@ import java.util.Arrays;
|
||||
import java.util.HashSet;
|
||||
|
||||
public class PythonClientCodegen extends DefaultCodegen implements CodegenConfig {
|
||||
protected String module = "SwaggerPetstore";
|
||||
protected String invokerPackage;
|
||||
protected String eggPackage;
|
||||
protected String packageName = null;
|
||||
protected String packageVersion = null;
|
||||
|
||||
public PythonClientCodegen() {
|
||||
super();
|
||||
|
||||
eggPackage = module + "-python";
|
||||
|
||||
invokerPackage = eggPackage + File.separatorChar + module;
|
||||
|
||||
modelPackage = "models";
|
||||
apiPackage = "api";
|
||||
outputFolder = "generated-code" + File.separatorChar + "python";
|
||||
modelTemplateFiles.put("model.mustache", ".py");
|
||||
apiTemplateFiles.put("api.mustache", ".py");
|
||||
templateDir = "python";
|
||||
|
||||
apiPackage = invokerPackage + File.separatorChar + "apis";
|
||||
modelPackage = invokerPackage + File.separatorChar + "models";
|
||||
|
||||
languageSpecificPrimitives.clear();
|
||||
languageSpecificPrimitives.add("int");
|
||||
languageSpecificPrimitives.add("float");
|
||||
@ -60,14 +55,43 @@ public class PythonClientCodegen extends DefaultCodegen implements CodegenConfig
|
||||
"print", "class", "exec", "in", "raise", "continue", "finally", "is",
|
||||
"return", "def", "for", "lambda", "try"));
|
||||
|
||||
additionalProperties.put("module", module);
|
||||
cliOptions.clear();
|
||||
cliOptions.add(new CliOption("packageName", "python package name (convension: under_score), default: swagger_client"));
|
||||
cliOptions.add(new CliOption("packageVersion", "python package version, default: 1.0.0"));
|
||||
}
|
||||
|
||||
supportingFiles.add(new SupportingFile("README.mustache", eggPackage, "README.md"));
|
||||
supportingFiles.add(new SupportingFile("setup.mustache", eggPackage, "setup.py"));
|
||||
supportingFiles.add(new SupportingFile("api_client.mustache", invokerPackage, "api_client.py"));
|
||||
supportingFiles.add(new SupportingFile("rest.mustache", invokerPackage, "rest.py"));
|
||||
supportingFiles.add(new SupportingFile("configuration.mustache", invokerPackage, "configuration.py"));
|
||||
supportingFiles.add(new SupportingFile("__init__package.mustache", invokerPackage, "__init__.py"));
|
||||
@Override
|
||||
public void processOpts() {
|
||||
super.processOpts();
|
||||
|
||||
if (additionalProperties.containsKey("packageName")) {
|
||||
setPackageName((String) additionalProperties.get("packageName"));
|
||||
}
|
||||
else {
|
||||
setPackageName("swagger_client");
|
||||
}
|
||||
|
||||
if (additionalProperties.containsKey("packageVersion")) {
|
||||
setPackageVersion((String) additionalProperties.get("packageVersion"));
|
||||
}
|
||||
else {
|
||||
setPackageVersion("1.0.0");
|
||||
}
|
||||
|
||||
additionalProperties.put("packageName", packageName);
|
||||
additionalProperties.put("packageVersion", packageVersion);
|
||||
|
||||
String swaggerFolder = packageName;
|
||||
|
||||
modelPackage = swaggerFolder + File.separatorChar + "models";
|
||||
apiPackage = swaggerFolder + File.separatorChar + "apis";
|
||||
|
||||
supportingFiles.add(new SupportingFile("README.mustache", "", "README.md"));
|
||||
supportingFiles.add(new SupportingFile("setup.mustache", "", "setup.py"));
|
||||
supportingFiles.add(new SupportingFile("api_client.mustache", swaggerFolder, "api_client.py"));
|
||||
supportingFiles.add(new SupportingFile("rest.mustache", swaggerFolder, "rest.py"));
|
||||
supportingFiles.add(new SupportingFile("configuration.mustache", swaggerFolder, "configuration.py"));
|
||||
supportingFiles.add(new SupportingFile("__init__package.mustache", swaggerFolder, "__init__.py"));
|
||||
supportingFiles.add(new SupportingFile("__init__model.mustache", modelPackage, "__init__.py"));
|
||||
supportingFiles.add(new SupportingFile("__init__api.mustache", apiPackage, "__init__.py"));
|
||||
}
|
||||
@ -225,4 +249,23 @@ public class PythonClientCodegen extends DefaultCodegen implements CodegenConfig
|
||||
return underscore(operationId);
|
||||
}
|
||||
|
||||
public void setPackageName(String packageName) {
|
||||
this.packageName = packageName;
|
||||
}
|
||||
|
||||
public void setPackageVersion(String packageVersion) {
|
||||
this.packageVersion = packageVersion;
|
||||
}
|
||||
|
||||
/**
|
||||
* Generate Python package name from String `packageName`
|
||||
*
|
||||
* (PEP 0008) Python packages should also have short, all-lowercase names,
|
||||
* although the use of underscores is discouraged.
|
||||
*/
|
||||
public String generatePackageName(String packageName) {
|
||||
return underscore(packageName.replaceAll("[^\\w]+", ""));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
@ -1,6 +1,9 @@
|
||||
import sys
|
||||
from setuptools import setup, find_packages
|
||||
|
||||
NAME = "{{packageName}}"
|
||||
VERSION = "{{packageVersion}}"
|
||||
|
||||
{{#apiInfo}}{{#apis}}{{^hasMore}}
|
||||
|
||||
# To install the library, open a Terminal shell, then run this
|
||||
@ -15,8 +18,8 @@ from setuptools import setup, find_packages
|
||||
REQUIRES = ["urllib3 >= 1.10", "six >= 1.9", "certifi"]
|
||||
|
||||
setup(
|
||||
name="{{module}}",
|
||||
version="{{version}}",
|
||||
name=NAME,
|
||||
version=VERSION,
|
||||
description="{{appName}}",
|
||||
author_email="{{infoEmail}}",
|
||||
url="{{infoUrl}}",
|
||||
|
@ -1,6 +1,9 @@
|
||||
import sys
|
||||
from setuptools import setup, find_packages
|
||||
|
||||
NAME = "swagger_client"
|
||||
VERSION = "1.0.0"
|
||||
|
||||
|
||||
|
||||
# To install the library, open a Terminal shell, then run this
|
||||
@ -15,8 +18,8 @@ from setuptools import setup, find_packages
|
||||
REQUIRES = ["urllib3 >= 1.10", "six >= 1.9", "certifi"]
|
||||
|
||||
setup(
|
||||
name="SwaggerPetstore",
|
||||
version="1.0.0",
|
||||
name=NAME,
|
||||
version=VERSION,
|
||||
description="Swagger Petstore",
|
||||
author_email="apiteam@swagger.io",
|
||||
url="",
|
Before Width: | Height: | Size: 42 KiB After Width: | Height: | Size: 42 KiB |
@ -11,8 +11,8 @@ import os
|
||||
import time
|
||||
import unittest
|
||||
|
||||
import SwaggerPetstore
|
||||
import SwaggerPetstore.configuration
|
||||
import swagger_client
|
||||
import swagger_client.configuration
|
||||
|
||||
HOST = 'http://petstore.swagger.io/v2'
|
||||
|
||||
@ -20,20 +20,20 @@ HOST = 'http://petstore.swagger.io/v2'
|
||||
class ApiClientTests(unittest.TestCase):
|
||||
|
||||
def setUp(self):
|
||||
self.api_client = SwaggerPetstore.ApiClient(HOST)
|
||||
self.api_client = swagger_client.ApiClient(HOST)
|
||||
|
||||
def test_configuration(self):
|
||||
SwaggerPetstore.configuration.api_key['api_key'] = '123456'
|
||||
SwaggerPetstore.configuration.api_key_prefix['api_key'] = 'PREFIX'
|
||||
SwaggerPetstore.configuration.username = 'test_username'
|
||||
SwaggerPetstore.configuration.password = 'test_password'
|
||||
swagger_client.configuration.api_key['api_key'] = '123456'
|
||||
swagger_client.configuration.api_key_prefix['api_key'] = 'PREFIX'
|
||||
swagger_client.configuration.username = 'test_username'
|
||||
swagger_client.configuration.password = 'test_password'
|
||||
|
||||
header_params = {'test1': 'value1'}
|
||||
query_params = {'test2': 'value2'}
|
||||
auth_settings = ['api_key', 'unknown']
|
||||
|
||||
# test prefix
|
||||
self.assertEqual('PREFIX', SwaggerPetstore.configuration.api_key_prefix['api_key'])
|
||||
self.assertEqual('PREFIX', swagger_client.configuration.api_key_prefix['api_key'])
|
||||
|
||||
# update parameters based on auth setting
|
||||
self.api_client.update_params_for_auth(header_params, query_params, auth_settings)
|
||||
@ -44,8 +44,8 @@ class ApiClientTests(unittest.TestCase):
|
||||
self.assertEqual(query_params['test2'], 'value2')
|
||||
|
||||
# test basic auth
|
||||
self.assertEqual('test_username', SwaggerPetstore.configuration.username)
|
||||
self.assertEqual('test_password', SwaggerPetstore.configuration.password)
|
||||
self.assertEqual('test_username', swagger_client.configuration.username)
|
||||
self.assertEqual('test_password', swagger_client.configuration.password)
|
||||
|
||||
def test_select_header_accept(self):
|
||||
accepts = ['APPLICATION/JSON', 'APPLICATION/XML']
|
||||
@ -114,7 +114,7 @@ class ApiClientTests(unittest.TestCase):
|
||||
|
||||
data = self.api_client.deserialize(json, 'dict(str, Pet)')
|
||||
self.assertTrue(isinstance(data, dict))
|
||||
self.assertTrue(isinstance(data['pet'], SwaggerPetstore.Pet))
|
||||
self.assertTrue(isinstance(data['pet'], swagger_client.Pet))
|
||||
|
||||
# dict(str, int)
|
||||
json = {
|
||||
@ -128,6 +128,3 @@ class ApiClientTests(unittest.TestCase):
|
||||
def test_deserialize_to_object(self):
|
||||
data = self.api_client.deserialize("", "object")
|
||||
self.assertTrue(type(data) == object)
|
||||
|
||||
|
||||
|
@ -3,7 +3,7 @@
|
||||
"""
|
||||
Run the tests.
|
||||
$ pip install nose (optional)
|
||||
$ cd SwaggerPetstore-python
|
||||
$ cd swagger_client-python
|
||||
$ nosetests -v
|
||||
"""
|
||||
|
||||
@ -11,25 +11,25 @@ import os
|
||||
import time
|
||||
import unittest
|
||||
|
||||
import SwaggerPetstore
|
||||
from SwaggerPetstore.rest import ApiException
|
||||
import swagger_client
|
||||
from swagger_client.rest import ApiException
|
||||
|
||||
|
||||
class ApiExceptionTests(unittest.TestCase):
|
||||
|
||||
def setUp(self):
|
||||
self.api_client = SwaggerPetstore.ApiClient()
|
||||
self.pet_api = SwaggerPetstore.PetApi(self.api_client)
|
||||
self.api_client = swagger_client.ApiClient()
|
||||
self.pet_api = swagger_client.PetApi(self.api_client)
|
||||
self.setUpModels()
|
||||
|
||||
def setUpModels(self):
|
||||
self.category = SwaggerPetstore.Category()
|
||||
self.category = swagger_client.Category()
|
||||
self.category.id = int(time.time())
|
||||
self.category.name = "dog"
|
||||
self.tag = SwaggerPetstore.Tag()
|
||||
self.tag = swagger_client.Tag()
|
||||
self.tag.id = int(time.time())
|
||||
self.tag.name = "blank"
|
||||
self.pet = SwaggerPetstore.Pet()
|
||||
self.pet = swagger_client.Pet()
|
||||
self.pet.id = int(time.time())
|
||||
self.pet.name = "hello kity"
|
||||
self.pet.photo_urls = ["http://foo.bar.com/1", "http://foo.bar.com/2"]
|
@ -3,7 +3,7 @@
|
||||
"""
|
||||
Run the tests.
|
||||
$ pip install nose (optional)
|
||||
$ cd SwaggerPetstore-python
|
||||
$ cd swagger_client-python
|
||||
$ nosetests -v
|
||||
"""
|
||||
|
||||
@ -11,8 +11,8 @@ import os
|
||||
import time
|
||||
import unittest
|
||||
|
||||
import SwaggerPetstore
|
||||
from SwaggerPetstore.rest import ApiException
|
||||
import swagger_client
|
||||
from swagger_client.rest import ApiException
|
||||
|
||||
HOST = 'http://petstore.swagger.io/v2'
|
||||
|
||||
@ -20,8 +20,8 @@ HOST = 'http://petstore.swagger.io/v2'
|
||||
class PetApiTests(unittest.TestCase):
|
||||
|
||||
def setUp(self):
|
||||
self.api_client = SwaggerPetstore.ApiClient(HOST)
|
||||
self.pet_api = SwaggerPetstore.PetApi(self.api_client)
|
||||
self.api_client = swagger_client.ApiClient(HOST)
|
||||
self.pet_api = swagger_client.PetApi(self.api_client)
|
||||
self.setUpModels()
|
||||
self.setUpFiles()
|
||||
|
||||
@ -30,13 +30,13 @@ class PetApiTests(unittest.TestCase):
|
||||
time.sleep(1)
|
||||
|
||||
def setUpModels(self):
|
||||
self.category = SwaggerPetstore.Category()
|
||||
self.category = swagger_client.Category()
|
||||
self.category.id = int(time.time())
|
||||
self.category.name = "dog"
|
||||
self.tag = SwaggerPetstore.Tag()
|
||||
self.tag = swagger_client.Tag()
|
||||
self.tag.id = int(time.time())
|
||||
self.tag.name = "blank"
|
||||
self.pet = SwaggerPetstore.Pet()
|
||||
self.pet = swagger_client.Pet()
|
||||
self.pet.id = int(time.time())
|
||||
self.pet.name = "hello kity"
|
||||
self.pet.photo_urls = ["http://foo.bar.com/1", "http://foo.bar.com/2"]
|
||||
@ -50,22 +50,22 @@ class PetApiTests(unittest.TestCase):
|
||||
self.foo = os.path.join(self.test_file_dir, "foo.png")
|
||||
|
||||
def test_create_api_instance(self):
|
||||
pet_api = SwaggerPetstore.PetApi()
|
||||
pet_api2 = SwaggerPetstore.PetApi()
|
||||
api_client3 = SwaggerPetstore.ApiClient()
|
||||
pet_api = swagger_client.PetApi()
|
||||
pet_api2 = swagger_client.PetApi()
|
||||
api_client3 = swagger_client.ApiClient()
|
||||
api_client3.user_agent = 'api client 3'
|
||||
api_client4 = SwaggerPetstore.ApiClient()
|
||||
api_client4 = swagger_client.ApiClient()
|
||||
api_client4.user_agent = 'api client 4'
|
||||
pet_api3 = SwaggerPetstore.PetApi(api_client3)
|
||||
pet_api3 = swagger_client.PetApi(api_client3)
|
||||
|
||||
# same default api client
|
||||
self.assertEqual(pet_api.api_client, pet_api2.api_client)
|
||||
# confirm using the default api client in the config module
|
||||
self.assertEqual(pet_api.api_client, SwaggerPetstore.configuration.api_client)
|
||||
self.assertEqual(pet_api.api_client, swagger_client.configuration.api_client)
|
||||
# 2 different api clients are not the same
|
||||
self.assertNotEqual(api_client3, api_client4)
|
||||
# customized pet api not using the default api client
|
||||
self.assertNotEqual(pet_api3.api_client, SwaggerPetstore.configuration.api_client)
|
||||
self.assertNotEqual(pet_api3.api_client, swagger_client.configuration.api_client)
|
||||
# customized pet api not using the old pet api's api client
|
||||
self.assertNotEqual(pet_api3.api_client, pet_api2.api_client)
|
||||
|
Loading…
Reference in New Issue
Block a user