Improve API examples in the spec

* Show response codes even if we don't have examples for them
 * Walk the objects to build param examples if none are given at the top level
pull/977/head
Richard van der Hoff 9 years ago
parent 2d28e5abce
commit 6b23598a26

@ -44,22 +44,25 @@ Example request:
{{endpoint.example.req | indent_block(2)}} {{endpoint.example.req | indent_block(2)}}
{% if endpoint.example.responses|length > 0 -%} {% if endpoint.responses|length > 0 -%}
Response{{"s" if endpoint.example.responses|length > 1 else "" }}: Response{{"s" if endpoint.responses|length > 1 else "" }}:
{% endif -%} {% endif -%}
{% for res in endpoint.example.responses -%} {% for res in endpoint.responses -%}
**Status code {{res["code"]}}:** **Status code {{res["code"]}}:**
{{res["description"]}} {{res["description"]}}
{% if res["example"] -%}
Example Example
.. code:: json .. code:: json
{{res["example"] | indent_block(2)}} {{res["example"] | indent_block(2)}}
{% endfor %} {% endif -%}
{% endfor %}

@ -273,9 +273,30 @@ def get_tables_for_schema(schema, mark_required=True):
return filtered return filtered
def get_example_for_schema(schema):
"""Returns a python object representing a suitable example for this object"""
if 'example' in schema:
return schema['example']
if 'properties' in schema:
res = {}
for prop_name, prop in schema['properties'].iteritems():
logger.debug("Parsing property %r" % prop_name)
prop_example = get_example_for_schema(prop)
res[prop_name] = prop_example
return res
if 'items' in schema:
return [get_example_for_schema(schema['items'])]
return schema.get('type', '??')
def get_example_for_param(param):
if 'x-example' in param:
return param['x-example']
if 'schema' not in param:
return None
return json.dumps(get_example_for_schema(param['schema']),
indent=2)
class MatrixUnits(Units): class MatrixUnits(Units):
def _load_swagger_meta(self, api, group_name): def _load_swagger_meta(self, api, group_name):
endpoints = [] endpoints = []
for path in api["paths"]: for path in api["paths"]:
@ -293,10 +314,9 @@ class MatrixUnits(Units):
"req_param_by_loc": {}, "req_param_by_loc": {},
"req_body_tables": [], "req_body_tables": [],
"res_tables": [], "res_tables": [],
"responses": [],
"example": { "example": {
"req": "", "req": "",
"responses": [],
"good_response": ""
} }
} }
self.log(" ------- Endpoint: %s %s ------- " % (method, path)) self.log(" ------- Endpoint: %s %s ------- " % (method, path))
@ -330,60 +350,51 @@ class MatrixUnits(Units):
# endfor[param] # endfor[param]
good_response = None good_response = None
for code, res in single_api.get("responses", {}).items(): for code in sorted(single_api.get("responses", {}).keys()):
res = single_api["responses"][code]
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 = res.get("examples", {}).get("application/json", "")
if description and example: endpoint["responses"].append({
endpoint["example"]["responses"].append({ "code": code,
"code": code, "description": description,
"description": description, "example": example,
"example": example, })
})
# form example request if it has one. It "has one" if all params path_template = api.get("basePath", "").rstrip("/") + path
# have either "x-example" or a "schema" with an "example". qps = []
params_missing_examples = [ body = ""
p for p in single_api.get("parameters", []) if ( for param in single_api.get("parameters", []):
"x-example" not in p and example = get_example_for_param(param)
not Units.prop(p, "schema/example")
) if not example:
] self.log(
if len(params_missing_examples) == 0: "The parameter %s is missing an example." %
path_template = api.get("basePath", "").rstrip("/") + path param["name"])
qps = [] continue
body = ""
for param in single_api.get("parameters", []): if param["in"] == "path":
if param["in"] == "path": path_template = path_template.replace(
path_template = path_template.replace( "{%s}" % param["name"], urllib.quote(example)
"{%s}" % param["name"], urllib.quote( )
param["x-example"] elif param["in"] == "body":
) body = example
) elif param["in"] == "query":
elif param["in"] == "body": if type(example) == list:
body = param["schema"]["example"] for value in example:
elif param["in"] == "query": qps.append((param["name"], value))
example = param["x-example"]
if type(example) == list:
for value in example:
qps.append((param["name"], value))
else: else:
qps.append((param["name"], example)) qps.append((param["name"], example))
query_string = "" if len(qps) == 0 else "?"+urllib.urlencode(qps)
if body:
endpoint["example"]["req"] = "%s %s%s HTTP/1.1\nContent-Type: application/json\n\n%s" % (
method.upper(), path_template, query_string, body
)
else:
endpoint["example"]["req"] = "%s %s%s HTTP/1.1\n\n" % (
method.upper(), path_template, query_string
)
query_string = "" if len(qps) == 0 else "?"+urllib.urlencode(qps)
if body:
endpoint["example"]["req"] = "%s %s%s HTTP/1.1\nContent-Type: application/json\n\n%s" % (
method.upper(), path_template, query_string, body
)
else: else:
self.log( endpoint["example"]["req"] = "%s %s%s HTTP/1.1\n\n" % (
"The following parameters are missing examples :( \n %s" % method.upper(), path_template, query_string
[ p["name"] for p in params_missing_examples ]
) )
# add response params if this API has any. # add response params if this API has any.

Loading…
Cancel
Save