Python jsonschema.exceptions-未知类型';str';对于具有架构的验证程序:

Python jsonschema.exceptions-未知类型';str';对于具有架构的验证程序:,python,flask,error-handling,swagger,jsonschema,Python,Flask,Error Handling,Swagger,Jsonschema,我使用flask和connexion作为我的api,我从swagger editor获得python服务器存根,它是根据api规范生成的。但是,我的一个端点使用json数据发出请求,它应该由json模式验证器验证。为此,我使用了jsonschemapython库。但是,现在当我使用示例json数据测试端点时,我得到了以下异常: > (env) C:\Users\kim\imm_python_server>python -m test_server > * Se

我使用
flask
connexion
作为我的api,我从swagger editor获得python服务器存根,它是根据api规范生成的。但是,我的一个端点使用
json
数据发出请求,它应该由json模式验证器验证。为此,我使用了
jsonschema
python库。但是,现在当我使用示例json数据测试端点时,我得到了以下异常:

>     (env) C:\Users\kim\imm_python_server>python -m test_server
>      * Serving Flask app "__main__" (lazy loading)
>      * Environment: production
>        WARNING: This is a development server. Do not use it in a production deployment.
>        Use a production WSGI server instead.
>      * Debug mode: off
>      * Running on http://0.0.0.0:8080/ (Press CTRL+C to quit)
>     127.0.0.1 - - [18/May/2020 20:14:07] "[37mGET /api/v1/ui/ HTTP/1.1[0m" 200 -
>     127.0.0.1 - - [18/May/2020 20:14:07] "[37mGET /api/v1/ui/favicon-32x32.png HTTP/1.1[0m" 200 -
>     127.0.0.1 - - [18/May/2020 20:14:07] "[37mGET /api/v1/openapi.json HTTP/1.1[0m" 200 -
>     127.0.0.1 - - [18/May/2020 20:14:13] "[1m[31mGET /api/v1/about HTTP/1.1[0m" 401 -
>     127.0.0.1 - - [18/May/2020 20:14:25] "[37mGET /api/v1/about HTTP/1.1[0m" 200 -
>     C:\Users\kim\AppData\Local\Programs\Python\Python37\lib\site-packages\connexion\apis\flask_api.py:199:
> FutureWarning: Implicit (flask) JSON serialization will change in the
> next major version. This is triggered because a response body is being
> serialized as JSON even though the mimetype is not a JSON type. This
> will be replaced by something that is mimetype-specific and may raise
> an error instead of silently converting everything to JSON. Please
> make sure to specify media/mime types in your specs.
>       FutureWarning  # a Deprecation targeted at application users.
>     127.0.0.1 - - [18/May/2020 20:14:38] "[37mGET /api/v1/rsession HTTP/1.1[0m" 200 -
>     [2020-05-18 20:14:50,506] ERROR in app: Exception on /api/v1/immunomatch_ed [POST]
>     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 158, in wrapper
>         self.validate_schema(data, request.url)
>       File "C:\Users\kim\AppData\Local\Programs\Python\Python37\lib\site-packages\connexion\decorators\validation.py",
> line 195, in validate_schema
>         self.validator.validate(data)
>       File "C:\Users\kim\AppData\Local\Programs\Python\Python37\lib\site-packages\jsonschema\validators.py",
> line 122, in validate
>         for error in self.iter_errors(*args, **kwargs):
>       File "C:\Users\kim\AppData\Local\Programs\Python\Python37\lib\site-packages\jsonschema\validators.py",
> line 98, in iter_errors
>         for error in errors:
>       File "C:\Users\kim\AppData\Local\Programs\Python\Python37\lib\site-packages\jsonschema\_validators.py",
> line 291, in properties_draft4
>         schema_path=property,
>       File "C:\Users\kim\AppData\Local\Programs\Python\Python37\lib\site-packages\jsonschema\validators.py",
> line 114, in descend
>         for error in self.iter_errors(instance, schema):
>       File "C:\Users\kim\AppData\Local\Programs\Python\Python37\lib\site-packages\jsonschema\validators.py",
> line 98, in iter_errors
>         for error in errors:
>       File "C:\Users\kim\AppData\Local\Programs\Python\Python37\lib\site-packages\connexion\json_schema.py",
> line 64, in validate_type
>         if not any(validator.is_type(instance, type) for type in types):
>       File "C:\Users\kim\AppData\Local\Programs\Python\Python37\lib\site-packages\connexion\json_schema.py",
> line 64, in <genexpr>
>         if not any(validator.is_type(instance, type) for type in types):
>       File "C:\Users\kim\AppData\Local\Programs\Python\Python37\lib\site-packages\jsonschema\validators.py",
> line 127, in is_type
>         raise UnknownType(type, instance, self.schema)
>     jsonschema.exceptions.UnknownType:
>     Unknown type 'str' for validator with schema, While checking instance:0

我不明白为什么会出现这个错误,我相信这是来自
jsonschema
的bug。我怎样才能让这一切顺利进行?有人能猜到这是怎么回事吗?有什么想法吗?谢谢

当提供的类型未注册以进行验证时,通常会出现此问题。在您的情况下,它是
str
。JSON模式为字符串、布尔值等少数类型提供了OOTB支持

https://json-schema.org/understanding-json-schema/reference/type.html

对于其余的类型,您必须编写自己的自定义验证器。

当提供的类型未注册以进行验证时,通常会出现此问题。在您的情况下,它是
str
。JSON模式为字符串、布尔值等少数类型提供了OOTB支持

https://json-schema.org/understanding-json-schema/reference/type.html

对于其余的类型,您必须编写自己的自定义验证器。

您能详细说明您的想法吗?我可能会尝试一下?谢谢你能详细阐述你的想法吗?我可以试试吗?谢谢