jsonschema.exceptions- 具有架构的验证器的未知类型 'str':

jsonschema.exceptions- Unknown type 'str' for validator with schema:

我为我的 api 使用了 flaskconnexion,我从 swagger 编辑器中获得了 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

我的 python 依赖项

这是我的 python 依赖项:

connexion == 2.6.0
swagger-ui-bundle == 0.0.2
python_dateutil == 2.6.0
setuptools >= 21.0.0
Werkzeug==0.16.1

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

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

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

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