我用flask, connexion and swagger UI
构建了我的api。我用openapi定义了apispec。我从swagger编辑器创建了swagger python服务器存根,您可以在其中找到sample project on github。当我反序列化用于验证的请求JSON数据时,将使用util.py
,但出现以下错误:
erorr
Traceback (most recent call last):
File "C:\Users\kim\AppData\Local\Programs\Python\Python37\lib\site-packages\flask\app.py", line 2447, in wsgi_app
response = self.full_dispatch_request()
File "C:\Users\kim\AppData\Local\Programs\Python\Python37\lib\site-packages\flask\app.py", line 1952, in full_dispatch_request
rv = self.handle_user_exception(e)
File "C:\Users\kim\AppData\Local\Programs\Python\Python37\lib\site-packages\flask\app.py", line 1821, in handle_user_exception
reraise(exc_type, exc_value, tb)
File "C:\Users\kim\AppData\Local\Programs\Python\Python37\lib\site-packages\flask\_compat.py", line 39, in reraise
raise value
File "C:\Users\kim\AppData\Local\Programs\Python\Python37\lib\site-packages\flask\app.py", line 1950, in full_dispatch_request
rv = self.dispatch_request()
File "C:\Users\kim\AppData\Local\Programs\Python\Python37\lib\site-packages\flask\app.py", line 1936, in dispatch_request
return self.view_functions[rule.endpoint](**req.view_args)
File "C:\Users\kim\AppData\Local\Programs\Python\Python37\lib\site-packages\connexion\decorators\decorator.py", line 48, in wrapper
response = function(request)
File "C:\Users\kim\AppData\Local\Programs\Python\Python37\lib\site-packages\connexion\decorators\security.py", line 327, in wrapper
return function(request)
File "C:\Users\kim\AppData\Local\Programs\Python\Python37\lib\site-packages\connexion\decorators\uri_parsing.py", line 144, in wrapper
response = function(request)
File "C:\Users\kim\AppData\Local\Programs\Python\Python37\lib\site-packages\connexion\decorators\validation.py", line 184, in wrapper
response = function(request)
File "C:\Users\kim\AppData\Local\Programs\Python\Python37\lib\site-packages\connexion\decorators\parameter.py", line 121, in wrapper
return function(**kwargs)
File "C:\Users\kim\codegen_server\openapi_server\controllers\default_controller.py", line 74, in immunomatch_ed_post
body = ImmunomatchEdInput.from_dict(connexion.request.get_json()) # noqa: E501
File "C:\Users\kim\codegen_server\openapi_server\models\immunomatch_ed_input.py", line 62, in from_dict
return util.deserialize_model(dikt, cls)
File "C:\Users\kim\codegen_server\openapi_server\util.py", line 111, in deserialize_model
setattr(instance, attr, _deserialize(value, attr_type))
File "C:\Users\kim\codegen_server\openapi_server\util.py", line 26, in _deserialize
elif type(klass) == typing.GenericMeta:
AttributeError: module 'typing' has no attribute 'GenericMeta'
错误消息说typing
模块没有属性'GenericMeta',我不明白为什么。谁能指出我为什么会发生此错误?是因为typing
模块版本错误还是什么原因?任何可能的想法摆脱这个错误?任何想法?谢谢
更新:我尝试过的代码:
import six
import typing
def _deserialize(data, klass):
"""Deserializes dict, list, str into an object.
:param data: dict, list or str.
:param klass: class literal, or string of class name.
:return: object.
"""
if data is None:
return None
if klass in six.integer_types or klass in (float, str, bool):
return _deserialize_primitive(data, klass)
elif klass == object:
return _deserialize_object(data)
elif klass == datetime.date:
return deserialize_date(data)
elif klass == datetime.datetime:
return deserialize_datetime(data)
elif type(klass) == typing.GenericMeta:
if klass.__extra__ == list:
return _deserialize_list(data, klass.__args__[0])
if klass.__extra__ == dict:
return _deserialize_dict(data, klass.__args__[1])
else:
return deserialize_model(data, klass)
错误,指出此行elif type(klass) == typing.GenericMeta:
没有通过。有什么主意吗?
答案 0 :(得分:2)
您可以将代码更改为以下内容:
elif hasattr(klass, '__origin__'):
if klass.__origin__ == list:
return _deserialize_list(data, klass.__args__[0])
if klass.__origin__ == dict:
return _deserialize_dict(data, klass.__args__[1])