10000 Version 1.3.0 · dd/python-openapi-codec@89066f3 · GitHub
[go: up one dir, main page]

Skip to content

Commit 89066f3

Browse files
committed
Version 1.3.0
1 parent ae04db0 commit 89066f3

File tree

6 files changed

+83
-36
lines changed

6 files changed

+83
-36
lines changed

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.2.1'
11+
__version__ = '1.3.0'
1212

1313

1414
class OpenAPICodec(BaseCodec):

openapi_codec/decode.py

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
from coreapi import Document, Link, Field
22
from coreapi.compat import string_types, urlparse
33
from coreapi.exceptions import ParseError
4+
import coreschema
45

56

67
def _parse_document(data, base_url=None):
@@ -37,22 +38,40 @@ def _parse_document(data, base_url=None):
3738
schema = _get_dict(parameter, 'schema', dereference_using=data)
3839
expanded = _expand_schema(schema)
3940
if expanded is not None:
41+
# TODO: field schemas.
4042
expanded_fields = [
41-
Field(name=field_name, location='form', required=is_required, description=field_description)
43+
Field(
44+
name=field_name,
45+
location='form',
46+
required=is_required,
47+
schema=coreschema.String(description=field_description)
48+
)
4249
for field_name, is_required, field_description in expanded
4350
if not any([field.name == field_name for field in fields])
4451
]
4552
fields += expanded_fields
4653
else:
54+
# TODO: field schemas.
4755
field_description = _get_string(parameter, 'description')
48-
field = Field(name=name, location='body', required=required, description=field_description)
56+
field = Field(
57+
name=name,
58+
location='body',
59+
required=required,
60+
schema=coreschema.String(description=field_description)
61+
)
4962
fields.append(field)
5063
else:
5164
if location == 'formData':
5265
has_form = True
5366
location = 'form'
5467
field_description = _get_string(parameter, 'description')
55-
field = Field(name=name, location=location, required=required, description=field_description)
68+
# TODO: field schemas.
69+
field = Field(
70+
name=name,
71+
location=location,
72+
required=required,
73+
schema=coreschema.String(description=field_description)
74+
)
5675
fields.append(field)
5776

5877
link_consumes = get_strings(_get_list(operation, 'consumes', consumes))

openapi_codec/encode.py

Lines changed: 27 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import coreschema
12
from collections import OrderedDict
23
from coreapi.compat import urlparse
34
from openapi_codec.utils import get_method, get_encoding, get_location, get_links_from_document
@@ -97,6 +98,20 @@ def _get_operation(operation_id, link, tags):
9798
return operation
9899

99100

101+
def _get_schema_type(schema):
102+
if schema is None:
103+
return 'string'
104+
105+
return {
106+
coreschema.String: 'string',
107+
coreschema.Integer: 'integer',
108+
coreschema.Number: 'number',
109+
coreschema.Boolean: 'boolean',
110+
coreschema.Array: 'array',
111+
coreschema.Object: 'object',
112+
}.get(schema.__class__, 'string')
113+
114+
100115
def _get_parameters(link, encoding):
101116
"""
102117
Generates Swagger Parameter Item object.
@@ -107,31 +122,30 @@ def _get_parameters(link, encoding):
107122

108123
for field in link.fields:
109124
location = get_location(link, field)
125+
field_description = field.schema.description if field.schema else ''
126+
field_type = _get_schema_type(field.schema)
110127
if location == 'form':
111128
if encoding in ('multipart/form-data', 'application/x-www-form-urlencoded'):
112129
# 'formData' in swagger MUST be one of these media types.
113130
parameter = {
114131
'name': field.name,
115132
'required': field.required,
116133
'in': 'formData',
117-
'description': field.description,
118-
'type': field.type or 'string',
134+
'description': field_description,
135+
'type': field_type,
119136
}
120-
if field.type == 'array':
137+
if field_type == 'array':
121138
parameter['items'] = {'type': 'string'}
122139
parameters.append(parameter)
123140
else:
124141
# Expand coreapi fields with location='form' into a single swagger
125142
# parameter, with a schema containing multiple properties.
126-
use_type = field.type or 'string'
127-
if use_type == 'file':
128-
use_type = 'string'
129143

130144
schema_property = {
131-
'description': field.description,
132-
'type': use_type,
145+
'description': field_description,
146+
'type': field_type,
133147
}
134-
if field.type == 'array':
148+
if field_type == 'array':
135149
schema_property['items'] = {'type': 'string'}
136150
properties[field.name] = schema_property
137151
if field.required:
@@ -146,7 +160,7 @@ def _get_parameters(link, encoding):
146160
'name': field.name,
147161
'required': field.required,
148162
'in': location,
149-
'description': field.description,
163+
'description': field_description,
150164
'schema': schema
151165
}
152166
parameters.append(parameter)
@@ -155,10 +169,10 @@ def _get_parameters(link, encoding):
155169
'name': field.name,
156170
'required': field.required,
157171
'in': location,
158-
'description': field.description,
159-
'type': field.type or 'string',
172+
'description': field_description,
173+
'type': field_type or 'string',
160174
}
161-
if field.type == 'array':
175+
if field_type == 'array':
162176
parameter['items'] = {'type': 'string'}
163177
parameters.append(parameter)
164178

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ def get_package_data(package):
6161
author_email='tom@tomchristie.com',
6262
packages=get_packages('openapi_codec'),
6363
package_data=get_package_data('openapi_codec'),
64-
install_requires=['coreapi>=2.1.0'],
64+
install_requires=['coreapi>=2.2.0'],
6565
classifiers=[
6666
'Intended Audience :: Developers',
6767
'License :: OSI Approved :: BSD License',

tests/test_encode.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import coreapi
2+
import coreschema
23
from openapi_codec.encode import generate_swagger_object, _get_parameters
34
from unittest import TestCase
45

@@ -86,7 +87,7 @@ def setUp(self):
8687
name='email',
8788
required=True,
8889
location='query',
89-
description='A valid email address.'
90+
schema=coreschema.String(description='A valid email address.')
9091
)
9192
self.swagger = _get_parameters(coreapi.Link(fields=[self.field]), encoding='')
9293

@@ -96,7 +97,7 @@ def test_expected_fields(self):
9697
'name': self.field.name,
9798
'required': self.field.required,
9899
'in': 'query',
99-
'description': self.field.description,
100+
'description': self.field.schema.description,
100101
'type': 'string' # Everything is a string for now.
101102
}
102103
self.assertEquals(self.swagger[0], expected)

tests/test_mappings.py

Lines changed: 29 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
from openapi_codec import OpenAPICodec
22
import coreapi
3+
import coreschema
34

45

56
codec = OpenAPICodec()
@@ -10,15 +11,15 @@
1011
'simple_link': coreapi.Link('/simple_link/', description='example link'),
1112
'location': {
1213
'query': coreapi.Link('/location/query/', fields=[
13-
coreapi.Field(name='a', description='example field', required=True),
14+
coreapi.Field(name='a', required=True, schema=coreschema.String(description='example field')),
1415
coreapi.Field(name='b')
1516
]),
1617
'form': coreapi.Link('/location/form/', action='post', fields=[
17-
coreapi.Field(name='a', description='example field', required=True),
18+
coreapi.Field(name='a', required=True, schema=coreschema.String(description='example field')),
1819
coreapi.Field(name='b'),
1920
]),
2021
'body': coreapi.Link('/location/body/', action='post', fields=[
21-
coreapi.Field(name='example', location='body', description='example field')
22+
coreapi.Field(name='example', location='body')
2223
]),
2324
'path': coreapi.Link('/location/path/{id}/', fields=[
2425
coreapi.Field(name='id', location='path', required=True)
@@ -69,12 +70,13 @@ def test_mapping():
6970
coreapi.Field(
7071
name='a',
7172
location='query',
72-
description='example field',
73+
schema=coreschema.String(description='example field'),
7374
required=True
7475
),
7576
coreapi.Field(
7677
name='b',
77-
location='query'
78+
location='query',
79+
schema=coreschema.String()
7880
)
7981
]
8082
)
@@ -86,7 +88,8 @@ def test_mapping():
8688
coreapi.Field(
8789
name='id',
8890
location='path',
89-
required=True
91+
required=True,
92+
schema=coreschema.String()
9093
)
9194
]
9295
)
@@ -100,11 +103,12 @@ def test_mapping():
100103
name='a',
101104
location='form',
102105
required=True,
103-
description='example field'
106+
schema=coreschema.String(description='example field')
104107
),
105108
coreapi.Field(
106109
name='b',
107-
location='form'
110+
location='form',
111+
schema=coreschema.String()
108112
)
109113
]
110114
)
@@ -117,8 +121,9 @@ def test_mapping():
117121
coreapi.Field(
118122
name='example',
119123
location='body',
120-
description='example field'
124+
schema=coreschema.String()
121125
)
126+
122127
]
123128
)
124129

@@ -130,11 +135,13 @@ def test_mapping():
130135
coreapi.Field(
131136
name='a',
132137
location='form',
133-
required=True
138+
required=True,
139+
schema=coreschema.String()
134140
),
135141
coreapi.Field(
136142
name='b',
137-
location='form'
143+
location='form',
144+
schema=coreschema.String()
138145
)
139146
]
140147
)
@@ -147,11 +154,13 @@ def test_mapping():
147154
coreapi.Field(
148155
name='a',
149156
location='form',
150-
required=True
157+
required=True,
158+
schema=coreschema.String()
151159
),
152160
coreapi.Field(
153161
name='b',
154-
location='form'
162+
location='form',
163+
schema=coreschema.String()
155164
)
156165
]
157166
)
@@ -164,7 +173,8 @@ def test_mapping():
164173
coreapi.Field(
165174
name='example',
166175
location='body',
167-
required=True
176+
required=True,
177+
schema=coreschema.String()
168178
)
169179
]
170180
)
@@ -178,7 +188,8 @@ def test_mapping():
178188
fields=[
179189
coreapi.Field(
180190
name='example',
181-
location='body'
191+
location='body',
192+
schema=coreschema.String()
182193
)
183194
]
184195
)
@@ -190,7 +201,9 @@ def test_mapping():
190201
fields=[
191202
coreapi.Field(
192203
name='example',
193-
location='body'
204+
location='body',
205+
206+
schema=coreschema.String()
194207
)
195208
]
196209
)

0 commit comments

Comments
 (0)
0