Skip to content

Commit

Permalink
Merge pull request #88 from elbart/master
Browse files Browse the repository at this point in the history
PoC: Support swagger ``example`` field for colander
  • Loading branch information
gabisurita authored Jul 30, 2018
2 parents 2ddff4d + 3f23441 commit 77b98a4
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 3 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ nosetests.xml
coverage.xml
*,cover
.hypothesis/
.pytest_cache/

# Translations
*.mo
Expand Down
7 changes: 6 additions & 1 deletion CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,12 @@ CHANGES
0.6.1 (unreleased)
------------------

- Nothing changed yet.
- Support swagger ``example`` field on colander ``SchemaNode`` custom kwarg::

def SomeSchema(colander.MappingSchema):
name = colander.SchemaNode(colander.String(), example='Mr. IceCream')

The ``example`` field is returned in the swagger spec accordingly.


0.6.0 (2018-03-28)
Expand Down
2 changes: 2 additions & 0 deletions cornice_swagger/converters/schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,8 @@ def convert_type(self, schema_node):
converted['description'] = schema_node.description
if schema_node.default is not colander.null:
converted['default'] = schema_node.default
if 'example' in schema_node.__dict__:
converted['example'] = schema_node.example

return converted

Expand Down
1 change: 1 addition & 0 deletions tests/support.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ class BodySchema(colander.MappingSchema):
id = colander.SchemaNode(colander.String())
timestamp = colander.SchemaNode(colander.Int())
obj = MyNestedSchema()
ex = colander.SchemaNode(colander.String(), missing=colander.drop, example='example string')


class QuerySchema(colander.MappingSchema):
Expand Down
15 changes: 13 additions & 2 deletions tests/test_response_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,23 @@ def test_response_schema(self):
self.assertDictEqual(responses['200']['headers'],
{'bar': {'type': 'string'}})

def test_response_schema_with_example(self):
class ResponseSchema(colander.MappingSchema):
body = BodySchema()

response_schemas = {'200': ResponseSchema(description='Return gelatto')}
responses = self.handler.from_schema_mapping(response_schemas)
self.assertTrue('ex' in responses['200']['schema']['properties'])
self.assertTrue('example' in responses['200']['schema']['properties']['ex'])
self.assertEquals(responses['200']['schema']['properties']['ex']['example'],
'example string')

def test_cornice_location_synonyms(self):

class ReponseSchema(colander.MappingSchema):
class ResponseSchema(colander.MappingSchema):
header = HeaderSchema()

response_schemas = {'200': ReponseSchema(description='Return gelatto')}
response_schemas = {'200': ResponseSchema(description='Return gelatto')}
responses = self.handler.from_schema_mapping(response_schemas)

self.assertDictEqual(responses['200']['headers'],
Expand Down

0 comments on commit 77b98a4

Please sign in to comment.