Skip to content

Commit

Permalink
Merge branch 'master' into mock
Browse files Browse the repository at this point in the history
  • Loading branch information
hjacobs committed Oct 4, 2016
2 parents ff5596a + 2a888c3 commit 8fad1ff
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 3 deletions.
11 changes: 9 additions & 2 deletions connexion/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,13 @@ def compatibility_layer(spec):
return spec


def canonical_base_url(base_path):
"""
Make given "basePath" a canonical base URL which can be prepended to paths starting with "/".
"""
return base_path.rstrip('/')


class Api(object):
"""
Single API that corresponds to a flask blueprint
Expand Down Expand Up @@ -104,9 +111,9 @@ def __init__(self, swagger_yaml_path, base_url=None, arguments=None,
# https://github.com/swagger-api/swagger-spec/blob/master/versions/2.0.md#fixed-fields
# If base_url is not on provided then we try to read it from the swagger.yaml or use / by default
if base_url is None:
self.base_url = self.specification.get('basePath', '') # type: dict
self.base_url = canonical_base_url(self.specification.get('basePath', ''))
else:
self.base_url = base_url
self.base_url = canonical_base_url(base_url)
self.specification['basePath'] = base_url

# A list of MIME types the APIs can produce. This is global to all APIs but can be overridden on specific
Expand Down
25 changes: 25 additions & 0 deletions tests/fixtures/simple/basepath-slash.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
swagger: "2.0"

info:
title: "Test basePath == /"
version: "1.0"

basePath: /

paths:
/greeting/{name}:
post:
summary: Generate greeting
description: Generates a greeting message.
operationId: fakeapi.hello.post_greeting
responses:
'200':
description: greeting response
schema:
type: object
parameters:
- name: name
in: path
description: Name of the person to greet.
required: true
type: string
15 changes: 14 additions & 1 deletion tests/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import pathlib
import tempfile

from connexion.api import Api
from connexion.api import Api, canonical_base_url
from connexion.exceptions import InvalidSpecification, ResolverError
from swagger_spec_validator.common import SwaggerValidationError
from yaml import YAMLError
Expand All @@ -13,6 +13,13 @@
TEST_FOLDER = pathlib.Path(__file__).parent


def test_canonical_base_url():
assert canonical_base_url('') == ''
assert canonical_base_url('/') == ''
assert canonical_base_url('/api') == '/api'
assert canonical_base_url('/api/') == '/api'


def test_api():
api = Api(TEST_FOLDER / "fixtures/simple/swagger.yaml", "/api/v1.0", {})
assert api.blueprint.name == '/api/v1_0'
Expand All @@ -24,6 +31,12 @@ def test_api():
assert api2.blueprint.url_prefix == '/v1.0'


def test_api_basepath_slash():
api = Api(TEST_FOLDER / "fixtures/simple/basepath-slash.yaml", None, {})
assert api.blueprint.name == ''
assert api.blueprint.url_prefix == ''


def test_template():
api1 = Api(TEST_FOLDER / "fixtures/simple/swagger.yaml", "/api/v1.0", {'title': 'test'})
assert api1.specification['info']['title'] == 'test'
Expand Down

0 comments on commit 8fad1ff

Please sign in to comment.