8000 encode and test format · core-api/python-openapi-codec@00b0b0e · GitHub
[go: up one dir, main page]

Skip to content
This repository was archived by the owner on Mar 18, 2019. It is now read-only.

Commit 00b0b0e

Browse files
committed
encode and test format
1 parent c376ffb commit 00b0b0e

File tree

2 files changed

+34
-0
lines changed

2 files changed

+34
-0
lines changed

openapi_codec/encode.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,10 @@ def _get_field_type(field):
128128
}.get(field.schema.__class__, 'string')
129129

130130

131+
def _get_field_format(field):
132+
return field.schema and field.schema.format
133+
134+
131135
def _get_parameters(link, encoding):
132136
"""
133137
Generates Swagger Parameter Item object.
@@ -140,6 +144,7 @@ def _get_parameters(link, encoding):
< 8000 code>140144
location = get_location(link, field)
141145
field_description = _get_field_description(field)
142146
field_type = _get_field_type(field)
147+
field_format = _get_field_format(field)
143148
if location == 'form':
144149
if encoding in ('multipart/form-data', 'application/x-www-form-urlencoded'):
145150
# 'formData' in swagger MUST be one of these media types.
@@ -152,6 +157,8 @@ def _get_parameters(link, encoding):
152157
}
153158
if field_type == 'array':
154159
parameter['items'] = {'type': 'string'}
160+
if field_format:
161+
parameter['format'] = field_format
155162
parameters.append(parameter)
156163
else:
157164
# Expand coreapi fields with location='form' into a single swagger
@@ -179,6 +186,8 @@ def _get_parameters(link, encoding):
179186
'description': field_description,
180187
'schema': schema
181188
}
189+
if field_format:
190+
parameter['format'] = field_format
182191
parameters.append(parameter)
183192
else:
184193
parameter = {
@@ -190,6 +199,8 @@ def _get_parameters(link, encoding):
190199
}
191200
if field_type == 'array':
192201
parameter['items'] = {'type': 'string'}
202+
if field_format:
203+
parameter['format'] = field_format
193204
parameters.append(parameter)
194205

195206
if properties:

tests/test_encode.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,3 +106,26 @@ def test_expected_fields(self):
106106
'type': 'string' # Everything is a string for now.
107107
}
108108
self.assertEquals(self.swagger[0], expected)
109+
110+
111+
class TestFormat(TestCase):
112+
def setUp(self):
113+
self.field = coreapi.Field(
114+
name='published_before',
115+
required=True,
116+
location='query',
117+
schema=coreschema.String(description='Filter by published date.', format='date')
118+
)
119+
self.swagger = _get_parameters(coreapi.Link(fields=[self.field]), encoding='')
120+
121+
def test_expected_fields(self):
122+
self.assertEquals(len(self.swagger), 1)
123+
expected = {
124+
'name': self.field.name,
125+
'required': self.field.required,
126+
'in': 'query',
127+
'description': self.field.schema.description,
128+
'type': 'string',
129+
'format': 'date'
130+
}
131+
self.assertEquals(self.swagger[0], expected)

0 commit comments

Comments
 (0)
2911
0