Skip to content

Commit 7cc9d71

Browse files
committed
Merge pull request #2309 from wing328/model_name_sanitize
[Python] better handling of model name
2 parents a92a9f2 + c51f4c6 commit 7cc9d71

File tree

10 files changed

+193
-4
lines changed

10 files changed

+193
-4
lines changed

modules/swagger-codegen/src/main/java/io/swagger/codegen/CodegenModel.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
public class CodegenModel {
88
public String parent, parentSchema;
99
public String name, classname, description, classVarName, modelJson, dataType;
10+
public String classFilename; // store the class file name, mainly used for import
1011
public String unescapedDescription;
1112
public String discriminator;
1213
public String defaultValue;

modules/swagger-codegen/src/main/java/io/swagger/codegen/DefaultCodegen.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -877,6 +877,7 @@ public CodegenModel fromModel(String name, Model model, Map<String, Model> allDe
877877
m.unescapedDescription = model.getDescription();
878878
m.classname = toModelName(name);
879879
m.classVarName = toVarName(name);
880+
m.classFilename = toModelFilename(name);
880881
m.modelJson = Json.pretty(model);
881882
m.externalDocs = model.getExternalDocs();
882883
m.vendorExtensions = model.getVendorExtensions();

modules/swagger-codegen/src/main/java/io/swagger/codegen/languages/PythonClientCodegen.java

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -212,8 +212,7 @@ public String toParamName(String name) {
212212

213213
@Override
214214
public String toModelName(String name) {
215-
name = sanitizeName(modelNamePrefix + name + modelNameSuffix); // FIXME: a parameter should not be assigned. Also declare the methods parameters as 'final'.
216-
215+
name = sanitizeName(name); // FIXME: a parameter should not be assigned. Also declare the methods parameters as 'final'.
217216
// remove dollar sign
218217
name = name.replaceAll("$", "");
219218

@@ -223,13 +222,25 @@ public String toModelName(String name) {
223222
name = "object_" + name; // e.g. return => ObjectReturn (after camelize)
224223
}
225224

225+
if (!StringUtils.isEmpty(modelNamePrefix)) {
226+
name = modelNamePrefix + "_" + name;
227+
}
228+
229+
if (!StringUtils.isEmpty(modelNameSuffix)) {
230+
name = name + "_" + modelNameSuffix;
231+
}
232+
226233
// camelize the model name
227234
// phone_number => PhoneNumber
228235
return camelize(name);
229236
}
230237

231238
@Override
232239
public String toModelFilename(String name) {
240+
name = sanitizeName(name); // FIXME: a parameter should not be assigned. Also declare the methods parameters as 'final'.
241+
// remove dollar sign
242+
name = name.replaceAll("$", "");
243+
233244
// model name cannot use reserved keyword, e.g. return
234245
if (isReservedWord(name)) {
235246
LOGGER.warn(name + " (reserved word) cannot be used as model filename. Renamed to " + underscore(dropDots("object_" + name)));
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
from __future__ import absolute_import
22

33
# import models into model package
4-
{{#models}}{{#model}}from .{{classVarName}} import {{classname}}{{/model}}
4+
{{#models}}{{#model}}from .{{classFilename}} import {{classname}}{{/model}}
55
{{/models}}

modules/swagger-codegen/src/main/resources/python/__init__package.mustache

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
from __future__ import absolute_import
22

33
# import models into sdk package
4-
{{#models}}{{#model}}from .models.{{classVarName}} import {{classname}}
4+
{{#models}}{{#model}}from .models.{{classFilename}} import {{classname}}
55
{{/model}}{{/models}}
66
# import apis into sdk package
77
{{#apiInfo}}{{#apis}}from .apis.{{classVarName}} import {{classname}}

modules/swagger-codegen/src/test/resources/2_0/petstore.json

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1251,6 +1251,28 @@
12511251
"xml": {
12521252
"name": "Order"
12531253
}
1254+
},
1255+
"$special[model.name]": {
1256+
"properties": {
1257+
"$special[property.name]": {
1258+
"type": "integer",
1259+
"format": "int64"
1260+
}
1261+
},
1262+
"xml": {
1263+
"name": "$special[model.name]"
1264+
}
1265+
},
1266+
"Return": {
1267+
"properties": {
1268+
"return": {
1269+
"type": "integer",
1270+
"format": "int32"
1271+
}
1272+
},
1273+
"xml": {
1274+
"name": "Return"
1275+
}
12541276
}
12551277
}
12561278
}

samples/client/petstore/python/swagger_client/__init__.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,10 @@
55
from .models.category import Category
66
from .models.pet import Pet
77
from .models.tag import Tag
8+
from .models.object_return import ObjectReturn
89
from .models.order import Order
10+
from .models.special_model_name import SpecialModelName
11+
from .models.inline_response_200 import InlineResponse200
912

1013
# import apis into sdk package
1114
from .apis.user_api import UserApi

samples/client/petstore/python/swagger_client/apis/pet_api.py

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -664,6 +664,83 @@ def upload_file(self, pet_id, **kwargs):
664664
callback=params.get('callback'))
665665
return response
666666

667+
def get_pet_by_id_in_object(self, pet_id, **kwargs):
668+
"""
669+
Fake endpoint to test inline arbitrary object return by 'Find pet by ID'
670+
Returns a pet when ID < 10. ID > 10 or nonintegers will simulate API error conditions
671+
672+
This method makes a synchronous HTTP request by default. To make an
673+
asynchronous HTTP request, please define a `callback` function
674+
to be invoked when receiving the response.
675+
>>> def callback_function(response):
676+
>>> pprint(response)
677+
>>>
678+
>>> thread = api.get_pet_by_id_in_object(pet_id, callback=callback_function)
679+
680+
:param callback function: The callback function
681+
for asynchronous request. (optional)
682+
:param int pet_id: ID of pet that needs to be fetched (required)
683+
:return: InlineResponse200
684+
If the method is called asynchronously,
685+
returns the request thread.
686+
"""
687+
688+
all_params = ['pet_id']
689+
all_params.append('callback')
690+
691+
params = locals()
692+
for key, val in iteritems(params['kwargs']):
693+
if key not in all_params:
694+
raise TypeError(
695+
"Got an unexpected keyword argument '%s'"
696+
" to method get_pet_by_id_in_object" % key
697+
)
698+
params[key] = val
699+
del params['kwargs']
700+
701+
# verify the required parameter 'pet_id' is set
702+
if ('pet_id' not in params) or (params['pet_id'] is None):
703+
raise ValueError("Missing the required parameter `pet_id` when calling `get_pet_by_id_in_object`")
704+
705+
resource_path = '/pet/{petId}?response=inline_arbitrary_object'.replace('{format}', 'json')
706+
path_params = {}
707+
if 'pet_id' in params:
708+
path_params['petId'] = params['pet_id']
709+
710+
query_params = {}
711+
712+
header_params = {}
713+
714+
form_params = []
715+
local_var_files = {}
716+
717+
body_params = None
718+
719+
# HTTP header `Accept`
720+
header_params['Accept'] = self.api_client.\
721+
select_header_accept(['application/json', 'application/xml'])
722+
if not header_params['Accept']:
723+
del header_params['Accept']
724+
725+
# HTTP header `Content-Type`
726+
header_params['Content-Type'] = self.api_client.\
727+
select_header_content_type([])
728+
729+
# Authentication setting
730+
auth_settings = ['api_key', 'petstore_auth']
731+
732+
response = self.api_client.call_api(resource_path, 'GET',
733+
path_params,
734+
query_params,
735+
header_params,
736+
body=body_params,
737+
post_params=form_params,
738+
files=local_var_files,
739+
response_type='InlineResponse200',
740+
auth_settings=auth_settings,
741+
callback=params.get('callback'))
742+
return response
743+
667744
def pet_pet_idtesting_byte_arraytrue_get(self, pet_id, **kwargs):
668745
"""
669746
Fake endpoint to test byte array return by 'Find pet by ID'

samples/client/petstore/python/swagger_client/apis/store_api.py

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,77 @@ def get_inventory(self, **kwargs):
190190
callback=params.get('callback'))
191191
return response
192192

193+
def get_inventory_in_object(self, **kwargs):
194+
"""
195+
Fake endpoint to test arbitrary object return by 'Get inventory'
196+
Returns an arbitrary object which is actually a map of status codes to quantities
197+
198+
This method makes a synchronous HTTP request by default. To make an
199+
asynchronous HTTP request, please define a `callback` function
200+
to be invoked when receiving the response.
201+
>>> def callback_function(response):
202+
>>> pprint(response)
203+
>>>
204+
>>> thread = api.get_inventory_in_object(callback=callback_function)
205+
206+
:param callback function: The callback function
207+
for asynchronous request. (optional)
208+
:return: object
209+
If the method is called asynchronously,
210+
returns the request thread.
211+
"""
212+
213+
all_params = []
214+
all_params.append('callback')
215+
216+
params = locals()
217+
for key, val in iteritems(params['kwargs']):
218+
if key not in all_params:
219+
raise TypeError(
220+
"Got an unexpected keyword argument '%s'"
221+
" to method get_inventory_in_object" % key
222+
)
223+
params[key] = val
224+
del params['kwargs']
225+
226+
227+
resource_path = '/store/inventory?response=arbitrary_object'.replace('{format}', 'json')
228+
path_params = {}
229+
230+
query_params = {}
231+
232+
header_params = {}
233+
234+
form_params = []
235+
local_var_files = {}
236+
237+
body_params = None
238+
239+
# HTTP header `Accept`
240+
header_params['Accept'] = self.api_client.\
241+
select_header_accept(['application/json', 'application/xml'])
242+
if not header_params['Accept']:
243+
del header_params['Accept']
244+
245+
# HTTP header `Content-Type`
246+
header_params['Content-Type'] = self.api_client.\
247+
select_header_content_type([])
248+
249+
# Authentication setting
250+
auth_settings = ['api_key']
251+
252+
response = self.api_client.call_api(resource_path, 'GET',
253+
path_params,
254+
query_params,
255+
header_params,
256+
body=body_params,
257+
post_params=form_params,
258+
files=local_var_files,
259+
response_type='object',
260+
auth_settings=auth_settings,
261+
callback=params.get('callback'))
262+
return response
263+
193264
def place_order(self, **kwargs):
194265
"""
195266
Place an order for a pet

samples/client/petstore/python/swagger_client/models/__init__.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,7 @@
55
from .category import Category
66
from .pet import Pet
77
from .tag import Tag
8+
from .object_return import ObjectReturn
89
from .order import Order
10+
from .special_model_name import SpecialModelName
11+
from .inline_response_200 import InlineResponse200

0 commit comments

Comments
 (0)