Skip to content

Commit

Permalink
modify spec base_path on request
Browse files Browse the repository at this point in the history
  • Loading branch information
dtkav committed Dec 17, 2018
1 parent 647ef8f commit 1cd4b63
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 2 deletions.
13 changes: 11 additions & 2 deletions connexion/apis/flask_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
from connexion.decorators.produces import NoContent
from connexion.handlers import AuthErrorHandler
from connexion.lifecycle import ConnexionRequest, ConnexionResponse
from connexion.spec import Specification
from connexion.utils import Jsonifier, is_json_mimetype

logger = logging.getLogger('connexion.apis.flask_api')
Expand All @@ -35,10 +36,18 @@ def add_openapi_json(self):
logger.debug('Adding spec json: %s/%s', self.base_path,
self.options.openapi_spec_path)
endpoint_name = "{name}_openapi_json".format(name=self.blueprint.name)
print("endpoint name", endpoint_name)

def spec_for_url():
""" Modify base_path in the spec based on incoming url
This fixes problems with reverse proxies changing the path.
"""
openapi_json_route = ".".join([self.blueprint.name, endpoint_name])
base_path = flask.url_for(openapi_json_route)
return flask.jsonify(self.specification.with_base_path(base_path).raw)

self.blueprint.add_url_rule(self.options.openapi_spec_path,
endpoint_name,
lambda: flask.jsonify(self.specification.raw))
spec_for_url)

def add_swagger_ui(self):
"""
Expand Down
6 changes: 6 additions & 0 deletions connexion/spec.py
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,12 @@ def load(cls, spec, arguments=None):
return cls.from_file(spec, arguments=arguments)
return cls.from_dict(spec)

def with_base_path(self, base_path):
# FIXME creating a whole new spec is slow
new_spec = Specification.from_dict(self.raw)
new_spec.base_path = base_path
return new_spec


class Swagger2Specification(Specification):
yaml_name = 'swagger.yaml'
Expand Down

0 comments on commit 1cd4b63

Please sign in to comment.