connexion icon indicating copy to clipboard operation
connexion copied to clipboard

Support Sanic

Open ioggstream opened this issue 5 years ago • 11 comments

Fixes #496 .

Changes proposed in this pull request:

  • support Sanic
  • creating a sanic_api.py and sanic_app.py modules
  • add relevant tests

ioggstream avatar Jun 11 '20 11:06 ioggstream

Is it ready to merge? Looks good to me

kigawas avatar Oct 26 '20 05:10 kigawas

@kigawas still there are tests failing, and more should be added. A discussion with the connexion community should be started to check they are interested in supporting sanic.

ioggstream avatar Oct 26 '20 10:10 ioggstream

@kigawas still there are tests failing, and more should be added. A discussion with the connexion community should be started to check they are interested in supporting sanic.

Lots of errors like TypeError: Can't instantiate abstract class SanicApi with abstract methods make_security_handler_factory

Maybe it's better to add flake8, black and mypy to make life easier

kigawas avatar Oct 26 '20 14:10 kigawas

@kigawas still there are tests failing, and more should be added. A discussion with the connexion community should be started to check they are interested in supporting sanic.

--- a/connexion/apis/abstract.py
+++ b/connexion/apis/abstract.py
@@ -37,7 +37,8 @@ class AbstractAPI(metaclass=AbstractAPIMeta):
     def __init__(self, specification, base_path=None, arguments=None,
                  validate_responses=False, strict_validation=False, resolver=None,
                  auth_all_paths=False, debug=False, resolver_error_handler=None,
-                 validator_map=None, pythonic_params=False, pass_context_arg_name=None, options=None):
+                 validator_map=None, pythonic_params=False, pass_context_arg_name=None, options=None,
+                 ):
         """
         :type specification: pathlib.Path | dict
         :type base_path: str | None
@@ -101,6 +102,8 @@ class AbstractAPI(metaclass=AbstractAPIMeta):
         logger.debug('pass_context_arg_name: %s', pass_context_arg_name)
         self.pass_context_arg_name = pass_context_arg_name

+        self.security_handler_factory = self.make_security_handler_factory(pass_context_arg_name)
+
         if self.options.openapi_spec_available:
             self.add_openapi_json()
             self.add_openapi_yaml()
@@ -143,6 +146,11 @@ class AbstractAPI(metaclass=AbstractAPIMeta):
         Adds a 404 error handler to authenticate and only expose the 404 status if the security validation pass.
         """

+    @staticmethod
+    @abc.abstractmethod
+    def make_security_handler_factory(pass_context_arg_name):
+        """ Create SecurityHandlerFactory to create all security check handlers """
+
     def add_operation(self, path, method):
         """
         Adds one operation to the api.

Solution

index d2050b1..feb095d 100644
--- a/connexion/apis/flask_api.py
+++ b/connexion/apis/flask_api.py
@@ -9,6 +9,7 @@ from connexion.handlers import AuthErrorHandler
 from connexion.jsonifier import Jsonifier
 from connexion.lifecycle import ConnexionRequest, ConnexionResponse
 from connexion.utils import is_json_mimetype, yamldumper
+from connexion.security import FlaskSecurityHandlerFactory
 from werkzeug.local import LocalProxy

 logger = logging.getLogger('connexion.apis.flask_api')
@@ -16,6 +17,11 @@ logger = logging.getLogger('connexion.apis.flask_api')

 class FlaskApi(AbstractAPI):

+    @staticmethod
+    def make_security_handler_factory(pass_context_arg_name):
+        """ Create default SecurityHandlerFactory to create all security check handlers """
+        return FlaskSecurityHandlerFactory(pass_context_arg_name)
+
     def _set_base_path(self, base_path):
         super(FlaskApi, self)._set_base_path(base_path)
         self._set_blueprint()

kigawas avatar Oct 27 '20 06:10 kigawas

@ioggstream

We are actively using this PR and it works pretty well, but there also come several bugs like

  1. For the value in url query, only the last character or number will get passed into handlers (like abc=123&def=456, you'll only get {'abc': '3', 'def': '6'})

  2. For uuid arguments, an extra dot appears (like you'll get .dc1332bd-c104-4d6f-b3ee-8ea83dccb028 instead of dc1332bd-c104-4d6f-b3ee-8ea83dccb028. Probably this is a connexion bug)

kigawas avatar Oct 27 '20 07:10 kigawas

@ioggstream

We are actively using this PR and it works pretty well, but there also come several bugs ... That's great! Happy that it works...

I am happy to review and merge your patches, but we really need to engage with connexion folks to continue :)

  1. For the value in url query, only the last character or number will get passed into handlers (like abc=123&def=456, you'll only get {'abc': '3', 'def': '6'})
  2. For uuid arguments, an extra dot appears

ioggstream avatar Oct 27 '20 07:10 ioggstream

I am happy to review and merge your patches, but we really need to engage with connexion folks to continue :)

@zalando @hjacobs

I apologise if it bothers you, but it'd be really nice to support sanic 😃

kigawas avatar Oct 27 '20 07:10 kigawas

This pr would fix most of the errors mentioned above, except one.

https://github.com/ioggstream/connexion/pull/4

Here is the pytest result (for only tests/sanic/test_sanic_bootstrap.py)

=============================================================================== short test summary info ================================================================================
FAILED tests/sanic/test_sanic_bootstrap.py::test_app_with_different_uri_parser - AssertionError: assert ['d'] == ['a', 'b', 'c']
===================================================================== 1 failed, 42 passed, 2654 warnings in 10.84s =====================================================================

For full log: https://gist.github.com/kigawas/b26a4905d4f7118bd70ed8c532fd2321

kigawas avatar Nov 06 '20 05:11 kigawas

Is this still active? Would be super useful (: Thanks!

Tzvika-m avatar Feb 28 '21 13:02 Tzvika-m

Is this still active? Would be super useful (: Thanks!

This PR should work itself, but the maintenance of this repo is not active I guess

kigawas avatar Mar 02 '21 11:03 kigawas

Hi folks, if there is some interest in the connexion community we can continue to work on it....

ioggstream avatar Mar 02 '21 14:03 ioggstream

Connexion 3.0 will support ASGI frameworks out of the box. See https://github.com/spec-first/connexion/issues/1395

RobbeSneyders avatar Feb 17 '23 16:02 RobbeSneyders