pyopenapi / pyswagger

An OpenAPI (fka Swagger) client & converter in python, which is type-safe, dynamic, spec-compliant.
MIT License
385 stars 89 forks source link

Attribute error while making a request with swagger spec 2.0 #171

Open kvnamipara opened 5 years ago

kvnamipara commented 5 years ago

With using open APIs spec v2 of GitHub using pyswagger, I am unable to get desired response.

Traceback (most recent call last):
  File "<input>", line 1, in <module>
  File "/Users/kvnamipara/Desktop/summer/staging/venv3/lib/python3.6/site-packages/pyswagger/contrib/client/requests.py", line 76, in request
    header=rs.headers,
  File "/Users/kvnamipara/Desktop/summer/staging/venv3/lib/python3.6/site-packages/pyswagger/io.py", line 417, in apply_with
    self.__data = r.schema._prim_(data, self.__op._prim_factory, ctx=dict(read=True))
  File "/Users/kvnamipara/Desktop/summer/staging/venv3/lib/python3.6/site-packages/pyswagger/spec/v2_0/objects.py", line 92, in _prim_
    return prim_factory.produce(self, v, ctx)
  File "/Users/kvnamipara/Desktop/summer/staging/venv3/lib/python3.6/site-packages/pyswagger/primitives/__init__.py", line 196, in produce
    val = _2nd(obj, ret, val, ctx)
  File "/Users/kvnamipara/Desktop/summer/staging/venv3/lib/python3.6/site-packages/pyswagger/primitives/comm.py", line 40, in _2nd_pass_obj
    return ret.apply_with(obj, val, ctx)
  File "/Users/kvnamipara/Desktop/summer/staging/venv3/lib/python3.6/site-packages/pyswagger/primitives/_array.py", line 48, in apply_with
    self.extend(map(functools.partial(ctx['factory'].produce, obj.items), val))
  File "/Users/kvnamipara/Desktop/summer/staging/venv3/lib/python3.6/site-packages/pyswagger/primitives/__init__.py", line 196, in produce
    val = _2nd(obj, ret, val, ctx)
  File "/Users/kvnamipara/Desktop/summer/staging/venv3/lib/python3.6/site-packages/pyswagger/primitives/comm.py", line 40, in _2nd_pass_obj
    return ret.apply_with(obj, val, ctx)
  File "/Users/kvnamipara/Desktop/summer/staging/venv3/lib/python3.6/site-packages/pyswagger/primitives/_model.py", line 23, in apply_with
    for k, v in six.iteritems(val):
  File "/Users/kvnamipara/Desktop/summer/staging/venv3/lib/python3.6/site-packages/six.py", line 587, in iteritems
    return iter(d.items(**kw))
AttributeError: 'str' object has no attribute 'items'

On debugging I found that, Api call is made correctly and correct response has been fetched, but then there is error while parsing the response object to pyswagger response object. Can anyone explain me what and where it is going wrong? Thanks.

loganlinn commented 5 years ago

I recently encountered a error: AttributeError: 'list' object has no attribute 'items'

Turns out that one one of the properties in the spec (also 2.0) was marked type: "object" when it should have been type: "array".

So, I would compare your schema against the response data to see if a property is declared as type: "object" when it's actually a string.

Maybe some simple error handling could be added in a future release of this library to add better error messages when a primitive factory raises exception

renamed commented 5 years ago

After spending a long and hard time on it, you sir saved my life!