Skip to content

Python codegen unable to convert "number" types #1230

@iwsmith

Description

@iwsmith

Hello,

If you have a swagger definition that include a number:

      score:
        type: number

A python model is generated with a type of "Number":

 39         self.swagger_types = {
 40             'paper_id': 'str',
 41             'publisher': 'str',
 42             'score': 'Number'
 43         }

When this is deserialized this results in an error:

Error
Traceback (most recent call last):
  File "/Users/iwsmith/Workspace/babel_frontend/test/test_search.py", line 12, in test_default
    r = self.api.app_search("blackholes")
  File "/Users/iwsmith/anaconda/envs/py3.4/lib/python3.4/site-packages/babel_client-2.0-py3.4.egg/babel_client/apis/default_api.py", line 648, in app_search
    callback=params.get('callback'))
  File "/Users/iwsmith/anaconda/envs/py3.4/lib/python3.4/site-packages/babel_client-2.0-py3.4.egg/babel_client/api_client.py", line 315, in call_api
    response_type, auth_settings, callback)
  File "/Users/iwsmith/anaconda/envs/py3.4/lib/python3.4/site-packages/babel_client-2.0-py3.4.egg/babel_client/api_client.py", line 151, in __call_api
    deserialized_data = self.deserialize(response_data, response_type)
  File "/Users/iwsmith/anaconda/envs/py3.4/lib/python3.4/site-packages/babel_client-2.0-py3.4.egg/babel_client/api_client.py", line 235, in deserialize
    return self.__deserialize(data, response_type)
  File "/Users/iwsmith/anaconda/envs/py3.4/lib/python3.4/site-packages/babel_client-2.0-py3.4.egg/babel_client/api_client.py", line 278, in __deserialize
    return self.__deserialize_model(data, klass)
  File "/Users/iwsmith/anaconda/envs/py3.4/lib/python3.4/site-packages/babel_client-2.0-py3.4.egg/babel_client/api_client.py", line 560, in __deserialize_model
    setattr(instance, attr, self.__deserialize(value, attr_type))
  File "/Users/iwsmith/anaconda/envs/py3.4/lib/python3.4/site-packages/babel_client-2.0-py3.4.egg/babel_client/api_client.py", line 253, in __deserialize
    for sub_data in data]
  File "/Users/iwsmith/anaconda/envs/py3.4/lib/python3.4/site-packages/babel_client-2.0-py3.4.egg/babel_client/api_client.py", line 253, in <listcomp>
    for sub_data in data]
  File "/Users/iwsmith/anaconda/envs/py3.4/lib/python3.4/site-packages/babel_client-2.0-py3.4.egg/babel_client/api_client.py", line 278, in __deserialize
    return self.__deserialize_model(data, klass)
  File "/Users/iwsmith/anaconda/envs/py3.4/lib/python3.4/site-packages/babel_client-2.0-py3.4.egg/babel_client/api_client.py", line 560, in __deserialize_model
    setattr(instance, attr, self.__deserialize(value, attr_type))
  File "/Users/iwsmith/anaconda/envs/py3.4/lib/python3.4/site-packages/babel_client-2.0-py3.4.egg/babel_client/api_client.py", line 267, in __deserialize
    klass = eval('models.' + klass)
  File "<string>", line 1, in <module>
AttributeError: 'module' object has no attribute 'Number'

With line 267 listed below

237     def __deserialize(self, data, klass):
246         if data is None:
247             return None
248 
249         if type(klass) == str:
250             if klass.startswith('list['):
251                 sub_kls = re.match('list\[(.*)\]', klass).group(1)
252                 return [self.__deserialize(sub_data, sub_kls)
253                         for sub_data in data]
254 
255             if klass.startswith('dict('):
256                 sub_kls = re.match('dict\(([^,]*), (.*)\)', klass).group(2)
257                 return {k: self.__deserialize(v, sub_kls)
258                         for k, v in iteritems(data)}
259 
260             # convert str to class
261             # for native types
262             if klass in ['int', 'float', 'str', 'bool',
263                          "date", 'datetime', "object"]:
264                 klass = eval(klass)
265             # for model types
266             else:
267                 klass = eval('models.' + klass)
268 
269         if klass in [int, float, str, bool]:
270             return self.__deserialize_primitive(data, klass)
271         elif klass == object:
272             return self.__deserialize_object()
273         elif klass == date:
274             return self.__deserialize_date(data)
275         elif klass == datetime:
276             return self.__deserialize_datatime(data)
277         else:
278             return self.__deserialize_model(data, klass)

I am not sure what the intent here was, but perhaps Number should result in a float being used instead of, well, whatever models.Number was supposed to do/be?

Thanks!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions