|
14 | 14 |
|
15 | 15 | from pyms.exceptions import AttrDoesNotExistException
|
16 | 16 | from pyms.flask.services.driver import DriverService
|
17 |
| -from pyms.utils import check_package_exists |
| 17 | +from pyms.utils.utils import check_package_exists, import_class |
18 | 18 |
|
19 | 19 | SWAGGER_PATH = "swagger"
|
20 | 20 | SWAGGER_FILE = "swagger.yaml"
|
@@ -62,7 +62,14 @@ class Service(DriverService):
|
62 | 62 | """
|
63 | 63 |
|
64 | 64 | config_resource = "swagger"
|
65 |
| - default_values = {"path": SWAGGER_PATH, "file": SWAGGER_FILE, "url": SWAGGER_URL, "project_dir": PROJECT_DIR} |
| 65 | + default_values = { |
| 66 | + "path": SWAGGER_PATH, |
| 67 | + "file": SWAGGER_FILE, |
| 68 | + "url": SWAGGER_URL, |
| 69 | + "project_dir": PROJECT_DIR, |
| 70 | + "validator_map": {}, |
| 71 | + "validate_responses": True, |
| 72 | + } |
66 | 73 |
|
67 | 74 | @staticmethod
|
68 | 75 | def _get_application_root(config) -> str:
|
@@ -97,27 +104,34 @@ def init_app(self, config, path: Path) -> Flask:
|
97 | 104 | :return: Flask
|
98 | 105 | """
|
99 | 106 | check_package_exists("connexion")
|
| 107 | + |
| 108 | + # Set paths |
100 | 109 | specification_dir = self.path
|
101 | 110 | application_root = self._get_application_root(config)
|
102 | 111 | if not os.path.isabs(self.path):
|
103 | 112 | specification_dir = os.path.join(path, self.path)
|
104 | 113 |
|
105 |
| - app = connexion.App(__name__, specification_dir=specification_dir, resolver=RestyResolver(self.project_dir)) |
106 |
| - |
| 114 | + # Prepare params |
| 115 | + validator_map = {k: import_class(v) for k, v in self.validator_map.items()} |
107 | 116 | params = {
|
108 | 117 | "specification": get_bundled_specs(Path(os.path.join(specification_dir, self.file)))
|
109 | 118 | if prance
|
110 | 119 | else self.file,
|
111 | 120 | "arguments": {"title": config.APP_NAME},
|
112 | 121 | "base_path": application_root,
|
113 | 122 | "options": {"swagger_url": self.url},
|
| 123 | + "validator_map": validator_map, |
| 124 | + "validate_responses": self.validate_responses, |
114 | 125 | }
|
115 | 126 |
|
116 | 127 | # Fix Connexion issue https://github.com/zalando/connexion/issues/1135
|
117 | 128 | if application_root == "/":
|
118 | 129 | del params["base_path"]
|
119 | 130 |
|
| 131 | + # Initialize connexion |
| 132 | + app = connexion.App(__name__, specification_dir=specification_dir, resolver=RestyResolver(self.project_dir)) |
120 | 133 | app.add_api(**params)
|
| 134 | + |
121 | 135 | # Invert the objects, instead connexion with a Flask object, a Flask object with
|
122 | 136 | application = app.app
|
123 | 137 | application.connexion_app = app
|
|
0 commit comments