Search code examples
pythonflaskswaggerjsonschemaconnexion

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


I used flask and connexion for my api, I got python server stub from swagger editor and it was generated based on api spec. However, one of my endpoint make request with json data, it should be validated by json schema validator. I used jsonschema python library for that. But, now when I test endpoint with sample json data, I got following exception:

>     (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

my python dependencies

here is my python depencies:

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

I quite don't understand why I got this error, I believe it is bug from jsonschema. How can I get this going right? anyone have possible guess what's going on with this? any thought? thanks


Solution

  • This issue usually comes when the provided type is unregistered for validation. In your case, it's str. JSON Schema provides OOTB support for few types like string, boolean, etc, check

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

    For the rest of the types, you have to write your own custom validator.