10000 Merge pull request #12 from core-api/dont-include-empty-host-or-scheme · ndcolling/python-openapi-codec@a118bac · GitHub
[go: up one dir, main page]

Skip to content

Commit a118bac

Browse files
authored
Merge pull request core-api#12 from core-api/dont-include-empty-host-or-scheme
Don't include empty host or scheme in schema output
2 parents 5f26e5f + d6389c7 commit a118bac

File tree

4 files changed

+24
-8
lines changed

4 files changed

+24
-8
lines changed

README.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,11 @@ To create a swagger schema from a `coreapi.Document`, use the codec directly.
2525

2626
## Using with the Python Client Library
2727

28+
Install `coreapi` and the `openapi-codec`.
29+
30+
$ pip install coreapi
31+
$ pip install openapi-codec
32+
2833
To use the Python client library to interact with a service that exposes a Swagger schema,
2934
include the codec in [the `decoders` argument][decoders].
3035

@@ -47,7 +52,8 @@ At this point you can now start to interact with the API:
4752

4853
Once the `openapi-codec` package is installed, the codec will automatically become available to the command line client.
4954

50-
$ pip install coreapi-cli openapi-codec
55+
$ pip install coreapi-cli
56+
$ pip install openapi-codec
5157
$ coreapi codecs show
5258
Codec name Media type Support Package
5359
corejson | application/coreapi+json | encoding, decoding | coreapi==2.0.7

openapi_codec/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
from openapi_codec.decode import _parse_document
99

1010

11-
__version__ = '1.1.0'
11+
__version__ = '1.1.1'
1212

1313

1414
class OpenAPICodec(BaseCodec):

openapi_codec/encode.py

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,17 +9,22 @@ def generate_swagger_object(document):
99
"""
1010
parsed_url = urlparse.urlparse(document.url)
1111

12-
return {
12+
swagger = {
1313
'swagger': '2.0',
1414
'info': {
1515
'title': document.title,
1616
'version': '' # Required by the spec
1717
},
18-
'paths': _get_paths_object(document),
19-
'host': parsed_url.netloc,
20-
'schemes': [parsed_url.scheme]
18+
'paths': _get_paths_object(document)
2119
}
2220

21+
if parsed_url.netloc:
22+
swagger['host'] = parsed_url.netloc
23+
if parsed_url.scheme:
24+
swagger['schemes'] = [parsed_url.scheme]
25+
26+
return swagger
27+
2328

2429
def _add_tag_prefix(item):
2530
operation_id, link, tags = item

tests/test_encode.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
class TestBasicInfo(TestCase):
77
def setUp(self):
8-
self.document = coreapi.Document(title='Example API')
8+
self.document = coreapi.Document(title='Example API', url='https://www.example.com/')
99
self.swagger = generate_swagger_object(self.document)
1010

1111
def test_info(self):
@@ -23,9 +23,14 @@ def test_swagger_version(self):
2323

2424
def test_host(self):
2525
self.assertIn('host', self.swagger)
26-
expected = ''
26+
expected = 'www.example.com'
2727
self.assertEquals(self.swagger['host'], expected)
2828

29+
def test_schemes(self):
30+
self.assertIn('schemes', self.swagger)
31+
expected = ['https']
32+
self.assertEquals(self.swagger['schemes'], expected)
33+
2934

3035
class TestPaths(TestCase):
3136
def setUp(self):

0 commit comments

Comments
 (0)
0