Better support for examples in responses

Walk the response schema to generate examples.
This commit is contained in:
Richard van der Hoff 2016-10-14 11:16:17 +01:00
parent 0f5e924ac9
commit af84ca09a0

View file

@ -384,16 +384,33 @@ def get_example_for_schema(schema):
return schema.get('type', '??') return schema.get('type', '??')
def get_example_for_param(param): def get_example_for_param(param):
"""Returns a stringified example for a parameter"""
if 'x-example' in param: if 'x-example' in param:
return param['x-example'] return param['x-example']
schema = param.get('schema') schema = param.get('schema')
if not schema: if not schema:
return None return None
if 'example' in schema: return json.dumps(get_example_for_schema(schema), indent=2)
return schema['example']
return json.dumps(get_example_for_schema(param['schema']),
indent=2)
def get_example_for_response(response):
"""Returns a stringified example for a response"""
exampleobj = None
if 'examples' in response:
exampleobj = response["examples"].get("application/json")
# the openapi spec suggests that examples in the 'examples' section should
# be formatted as raw objects rather than json-formatted strings, but we
# have lots of the latter in our spec, which work with the swagger UI,
# so grandfather them in.
if isinstance(exampleobj, basestring):
return exampleobj
if exampleobj is None:
schema = response.get('schema')
if schema:
exampleobj = get_example_for_schema(schema)
if exampleobj is None:
return None
return json.dumps(exampleobj, indent=2)
class MatrixUnits(Units): class MatrixUnits(Units):
def _load_swagger_meta(self, api, group_name): def _load_swagger_meta(self, api, group_name):
@ -455,7 +472,7 @@ class MatrixUnits(Units):
if not good_response and code == 200: if not good_response and code == 200:
good_response = res good_response = res
description = res.get("description", "") description = res.get("description", "")
example = res.get("examples", {}).get("application/json", "") example = get_example_for_response(res)
endpoint["responses"].append({ endpoint["responses"].append({
"code": code, "code": code,
"description": description, "description": description,