Skip to content

Commit 75b5a8b

Browse files
authored
Fix swagger openapi 3 (#100)
* Fix swagger path in openapi 3.0 * Fix pylint
1 parent a8cc6b1 commit 75b5a8b

11 files changed

+193
-27
lines changed

Pipfile.lock

Lines changed: 85 additions & 19 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pyms/flask/services/swagger.py

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import connexion
44
from connexion.resolver import RestyResolver
55

6+
from pyms.exceptions import AttrDoesNotExistException
67
from pyms.flask.services.driver import DriverService
78
from pyms.utils import check_package_exists
89

@@ -31,6 +32,14 @@ class Service(DriverService):
3132
"project_dir": PROJECT_DIR
3233
}
3334

35+
@staticmethod
36+
def _get_application_root(config):
37+
try:
38+
application_root = config.APPLICATION_ROOT
39+
except AttrDoesNotExistException:
40+
application_root = "/"
41+
return application_root
42+
3443
def init_app(self, config, path):
3544
"""
3645
Initialize Connexion App. See more info in [Connexion Github](https://github.com/zalando/connexion)
@@ -57,6 +66,7 @@ def init_app(self, config, path):
5766
"""
5867
check_package_exists("connexion")
5968
specification_dir = self.path
69+
application_root = self._get_application_root(config)
6070
if not os.path.isabs(self.path):
6171
specification_dir = os.path.join(path, self.path)
6272

@@ -67,11 +77,13 @@ def init_app(self, config, path):
6777
params = {
6878
"specification": self.file,
6979
"arguments": {'title': config.APP_NAME},
80+
"base_path": application_root,
7081
"options": {"swagger_url": self.url},
7182
}
83+
7284
# Fix Connexion issue https://github.com/zalando/connexion/issues/1135
73-
if config.APPLICATION_ROOT and config.APPLICATION_ROOT != "/":
74-
params["base_path"] = config.APPLICATION_ROOT
85+
if application_root == "/":
86+
params["base_path"] = ""
7587

7688
app.add_api(**params)
7789
# Invert the objects, instead connexion with a Flask object, a Flask object with

tests/config-tests-flask-swagger.yml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ pyms:
77
DEBUG: true
88
TESTING: true
99
APP_NAME: "Python Microservice With Flask in tests"
10-
APPLICATION_ROOT: /
1110
test_var: general
1211
subservice1:
1312
test: input

tests/config-tests-flask.yml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ pyms:
33
DEBUG: true
44
TESTING: true
55
APP_NAME: "Python Microservice With Flask"
6-
APPLICATION_ROOT: /
76
test_var: general
87
subservice1:
98
test: input

tests/config-tests-metrics.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,4 @@ pyms:
99
DEBUG: true
1010
TESTING: true
1111
APP_NAME: "Python Microservice with Jaeger"
12-
APPLICATION_ROOT: /
12+
APPLICATION_ROOT: "/"

tests/config-tests-swagger_3.yml

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
pyms:
2+
services:
3+
metrics:
4+
enabled: false
5+
swagger:
6+
path: ""
7+
file: "swagger3.yaml"
8+
url: "/ws-doc/"
9+
config:
10+
DEBUG: true
11+
TESTING: true
12+
APP_NAME: "Python Microservice Swagger Openapi 3"
13+
APPLICATION_ROOT: ""
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
pyms:
2+
services:
3+
metrics:
4+
enabled: false
5+
swagger:
6+
path: ""
7+
file: "swagger3.yaml"
8+
url: "/ws-doc2/"
9+
config:
10+
DEBUG: true
11+
TESTING: true
12+
APP_NAME: "Python Microservice Swagger Openapi 3 No abspath"
13+
APPLICATION_ROOT: "/test-api-path2"

tests/config-tests.yml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ pyms:
1414
debug: true
1515
testing: true
1616
app_name: "Python Microservice"
17-
application_root: /
1817
test_var: general
1918
subservice1:
2019
test: input

tests/swagger3.yaml

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
---
2+
openapi: 3.0.2
3+
info:
4+
title: Testing
5+
version: 1.0.0
6+
description: Testing
7+
termsOfService: http://swagger.io/terms/
8+
contact:
9+
name: Testing
10+
11+
servers:
12+
- url: http://localhost:8080/test/path/
13+
description: ""
14+
paths:
15+
/test-url:
16+
summary: get test
17+
get:
18+
responses:
19+
"200":
20+
description: OK
21+
operationId: "tests.test_flask.home"

tests/test_metrics.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,15 @@
44
from prometheus_client import generate_latest
55

66
from pyms.constants import CONFIGMAP_FILE_ENVIRONMENT
7-
from tests.common import MyMicroservice
7+
from tests.common import MyMicroserviceNoSingleton
88

99

1010
class TestMetricsFlask(unittest.TestCase):
1111
BASE_DIR = os.path.dirname(os.path.abspath(__file__))
1212

1313
def setUp(self):
1414
os.environ[CONFIGMAP_FILE_ENVIRONMENT] = os.path.join(self.BASE_DIR, "config-tests-metrics.yml")
15-
ms = MyMicroservice(path=__file__)
15+
ms = MyMicroserviceNoSingleton(path=__file__)
1616
ms.reload_conf()
1717
self.app = ms.create_app()
1818
self.client = self.app.test_client()

0 commit comments

Comments
 (0)