diff --git a/.gitignore b/.gitignore index 45936a3..47b6bdd 100644 --- a/.gitignore +++ b/.gitignore @@ -44,6 +44,7 @@ nosetests.xml coverage.xml *,cover .hypothesis/ +.pytest_cache/ # Translations *.mo diff --git a/CHANGES.rst b/CHANGES.rst index a7ecf72..45b0705 100644 --- a/CHANGES.rst +++ b/CHANGES.rst @@ -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) diff --git a/cornice_swagger/converters/schema.py b/cornice_swagger/converters/schema.py index 1d36fda..b4ca986 100644 --- a/cornice_swagger/converters/schema.py +++ b/cornice_swagger/converters/schema.py @@ -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 diff --git a/tests/support.py b/tests/support.py index 08145ed..e89f3f4 100644 --- a/tests/support.py +++ b/tests/support.py @@ -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): diff --git a/tests/test_response_handler.py b/tests/test_response_handler.py index 4bc94f9..4edaaca 100644 --- a/tests/test_response_handler.py +++ b/tests/test_response_handler.py @@ -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'],