Change reserved word handling in python client.

* First, remove the beginning underscores.
* Then, append underscore if the var is reserved word or number.
This commit is contained in:
geekerzp 2015-08-19 17:48:04 +08:00
parent dc1992ad45
commit 894d571ea5
11 changed files with 62 additions and 72 deletions

View File

@ -119,7 +119,7 @@ public class PythonClientCodegen extends DefaultCodegen implements CodegenConfig
@Override @Override
public String escapeReservedWord(String name) { public String escapeReservedWord(String name) {
return name + "_"; return "_" + name;
} }
@Override @Override
@ -179,14 +179,14 @@ public class PythonClientCodegen extends DefaultCodegen implements CodegenConfig
// petId => pet_id // petId => pet_id
name = underscore(dropDots(name)); name = underscore(dropDots(name));
// remove leading underscore
name = name.replaceAll("^_*", "");
// for reserved word or word starting with number, append _ // for reserved word or word starting with number, append _
if (reservedWords.contains(name) || name.matches("^\\d.*")) { if (reservedWords.contains(name) || name.matches("^\\d.*")) {
name = escapeReservedWord(name); name = escapeReservedWord(name);
} }
// remove leading underscore
name = name.replaceAll("^_*", "");
return name; return name;
} }

View File

@ -202,11 +202,9 @@ class ApiClient(object):
# and attributes which value is not None. # and attributes which value is not None.
# Convert attribute name to json key in # Convert attribute name to json key in
# model definition for request. # model definition for request.
obj_dict = {obj.attribute_map[key[1:]]: val obj_dict = {obj.attribute_map[attr]: getattr(obj, attr)
for key, val in iteritems(obj.__dict__) for attr, _ in iteritems(obj.swagger_types)
if key != 'swagger_types' if getattr(obj, attr) is not None}
and key != 'attribute_map'
and val is not None}
return {key: self.sanitize_for_serialization(val) return {key: self.sanitize_for_serialization(val)
for key, val in iteritems(obj_dict)} for key, val in iteritems(obj_dict)}

View File

@ -23,9 +23,9 @@ import urllib3
try: try:
import httplib import httplib
except ImportError: except ImportError:
# python3 # for python3
import http.client as httplib import http.client as httplib
import sys import sys
import logging import logging

View File

@ -86,18 +86,17 @@ class {{classname}}(object):
""" """
result = {} result = {}
for name, prop in iteritems(self.__dict__): for attr, _ in iteritems(self.swagger_types):
if name == "attribute_map" or name == "swagger_types": value = getattr(self, attr)
continue if isinstance(value, list):
if isinstance(prop, list): result[attr] = list(map(
result[name[1:]] = list(map(
lambda x: x.to_dict() if hasattr(x, "to_dict") else x, lambda x: x.to_dict() if hasattr(x, "to_dict") else x,
prop value
)) ))
elif hasattr(prop, "to_dict"): elif hasattr(value, "to_dict"):
result[name[1:]] = prop.to_dict() result[attr] = value.to_dict()
else: else:
result[name[1:]] = prop result[attr] = value
return result return result

View File

@ -202,11 +202,9 @@ class ApiClient(object):
# and attributes which value is not None. # and attributes which value is not None.
# Convert attribute name to json key in # Convert attribute name to json key in
# model definition for request. # model definition for request.
obj_dict = {obj.attribute_map[key[1:]]: val obj_dict = {obj.attribute_map[attr]: getattr(obj, attr)
for key, val in iteritems(obj.__dict__) for attr, _ in iteritems(obj.swagger_types)
if key != 'swagger_types' if getattr(obj, attr) is not None}
and key != 'attribute_map'
and val is not None}
return {key: self.sanitize_for_serialization(val) return {key: self.sanitize_for_serialization(val)
for key, val in iteritems(obj_dict)} for key, val in iteritems(obj_dict)}

View File

@ -23,9 +23,9 @@ import urllib3
try: try:
import httplib import httplib
except ImportError: except ImportError:
# python3 # for python3
import http.client as httplib import http.client as httplib
import sys import sys
import logging import logging

View File

@ -97,18 +97,17 @@ class Category(object):
""" """
result = {} result = {}
for name, prop in iteritems(self.__dict__): for attr, _ in iteritems(self.swagger_types):
if name == "attribute_map" or name == "swagger_types": value = getattr(self, attr)
continue if isinstance(value, list):
if isinstance(prop, list): result[attr] = list(map(
result[name[1:]] = list(map(
lambda x: x.to_dict() if hasattr(x, "to_dict") else x, lambda x: x.to_dict() if hasattr(x, "to_dict") else x,
prop value
)) ))
elif hasattr(prop, "to_dict"): elif hasattr(value, "to_dict"):
result[name[1:]] = prop.to_dict() result[attr] = value.to_dict()
else: else:
result[name[1:]] = prop result[attr] = value
return result return result

View File

@ -203,18 +203,17 @@ class Order(object):
""" """
result = {} result = {}
for name, prop in iteritems(self.__dict__): for attr, _ in iteritems(self.swagger_types):
if name == "attribute_map" or name == "swagger_types": value = getattr(self, attr)
continue if isinstance(value, list):
if isinstance(prop, list): result[attr] = list(map(
result[name[1:]] = list(map(
lambda x: x.to_dict() if hasattr(x, "to_dict") else x, lambda x: x.to_dict() if hasattr(x, "to_dict") else x,
prop value
)) ))
elif hasattr(prop, "to_dict"): elif hasattr(value, "to_dict"):
result[name[1:]] = prop.to_dict() result[attr] = value.to_dict()
else: else:
result[name[1:]] = prop result[attr] = value
return result return result

View File

@ -203,18 +203,17 @@ class Pet(object):
""" """
result = {} result = {}
for name, prop in iteritems(self.__dict__): for attr, _ in iteritems(self.swagger_types):
if name == "attribute_map" or name == "swagger_types": value = getattr(self, attr)
continue if isinstance(value, list):
if isinstance(prop, list): result[attr] = list(map(
result[name[1:]] = list(map(
lambda x: x.to_dict() if hasattr(x, "to_dict") else x, lambda x: x.to_dict() if hasattr(x, "to_dict") else x,
prop value
)) ))
elif hasattr(prop, "to_dict"): elif hasattr(value, "to_dict"):
result[name[1:]] = prop.to_dict() result[attr] = value.to_dict()
else: else:
result[name[1:]] = prop result[attr] = value
return result return result

View File

@ -97,18 +97,17 @@ class Tag(object):
""" """
result = {} result = {}
for name, prop in iteritems(self.__dict__): for attr, _ in iteritems(self.swagger_types):
if name == "attribute_map" or name == "swagger_types": value = getattr(self, attr)
continue if isinstance(value, list):
if isinstance(prop, list): result[attr] = list(map(
result[name[1:]] = list(map(
lambda x: x.to_dict() if hasattr(x, "to_dict") else x, lambda x: x.to_dict() if hasattr(x, "to_dict") else x,
prop value
)) ))
elif hasattr(prop, "to_dict"): elif hasattr(value, "to_dict"):
result[name[1:]] = prop.to_dict() result[attr] = value.to_dict()
else: else:
result[name[1:]] = prop result[attr] = value
return result return result

View File

@ -247,18 +247,17 @@ class User(object):
""" """
result = {} result = {}
for name, prop in iteritems(self.__dict__): for attr, _ in iteritems(self.swagger_types):
if name == "attribute_map" or name == "swagger_types": value = getattr(self, attr)
continue if isinstance(value, list):
if isinstance(prop, list): result[attr] = list(map(
result[name[1:]] = list(map(
lambda x: x.to_dict() if hasattr(x, "to_dict") else x, lambda x: x.to_dict() if hasattr(x, "to_dict") else x,
prop value
)) ))
elif hasattr(prop, "to_dict"): elif hasattr(value, "to_dict"):
result[name[1:]] = prop.to_dict() result[attr] = value.to_dict()
else: else:
result[name[1:]] = prop result[attr] = value
return result return result