From 94b13c01218864fac8f9a8e9ce3544194c589719 Mon Sep 17 00:00:00 2001 From: Daniel Wagner-Hall Date: Mon, 7 Sep 2015 17:14:36 +0100 Subject: [PATCH 1/3] Show all responses, not just the successful one This still filters out responses lacking either a description or an example --- .../matrix_templates/templates/http-api.tmpl | 17 +++++++++++++++-- templating/matrix_templates/units.py | 16 +++++++++++----- 2 files changed, 26 insertions(+), 7 deletions(-) diff --git a/templating/matrix_templates/templates/http-api.tmpl b/templating/matrix_templates/templates/http-api.tmpl index 5e80f1686..1384daab5 100644 --- a/templating/matrix_templates/templates/http-api.tmpl +++ b/templating/matrix_templates/templates/http-api.tmpl @@ -46,6 +46,19 @@ Example request:: {{endpoint.example.req | indent_block(2)}} -Example response:: +{% if endpoint.example.responses|length > 0 -%} +Response{{"s" if endpoint.example.responses|length > 1 else "" }}: - {{endpoint.example.res | indent_block(2)}} \ No newline at end of file +{% endif -%} + +{% for res in endpoint.example.responses -%} + +**Status code {{res["code"]}}:** + +{{res["description"]}} + +Example:: + + {{res["example"] | indent_block(2)}} + +{% endfor %} diff --git a/templating/matrix_templates/units.py b/templating/matrix_templates/units.py index d607160fd..42ce3f225 100644 --- a/templating/matrix_templates/units.py +++ b/templating/matrix_templates/units.py @@ -177,11 +177,17 @@ class MatrixUnits(Units): endpoint["req_param_by_loc"][p["loc"]] = [] endpoint["req_param_by_loc"][p["loc"]].append(p) - # add example response if it has one - res = single_api["responses"][200] # get the 200 OK response - endpoint["example"]["res"] = res.get("examples", {}).get( - "application/json", "" - ) + endpoint["example"]["responses"] = [] # Ordered list of maps + for code, res in single_api["responses"].items(): + description = res.get("description", "") + example = res.get("examples", {}).get("application/json", "") + if description and example: + endpoint["example"]["responses"].append({ + "code": code, + "description": description, + "example": example, + }) + # form example request if it has one. It "has one" if all params # have either "x-example" or a "schema" with an "example". params_missing_examples = [ From 0a9f61029a82ad67f9b02e054fd5238e976905d3 Mon Sep 17 00:00:00 2001 From: Daniel Wagner-Hall Date: Tue, 8 Sep 2015 11:16:48 +0100 Subject: [PATCH 2/3] Allow for missing responses key --- templating/matrix_templates/units.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/templating/matrix_templates/units.py b/templating/matrix_templates/units.py index 42ce3f225..5852b438f 100644 --- a/templating/matrix_templates/units.py +++ b/templating/matrix_templates/units.py @@ -178,7 +178,7 @@ class MatrixUnits(Units): endpoint["req_param_by_loc"][p["loc"]].append(p) endpoint["example"]["responses"] = [] # Ordered list of maps - for code, res in single_api["responses"].items(): + for code, res in single_api.get("responses", {}).items(): description = res.get("description", "") example = res.get("examples", {}).get("application/json", "") if description and example: From 5031c26f7b4b39ece5bb04df4be38ff0b3000292 Mon Sep 17 00:00:00 2001 From: Daniel Wagner-Hall Date: Tue, 8 Sep 2015 11:21:19 +0100 Subject: [PATCH 3/3] Make res optional, and rename to good_response --- templating/matrix_templates/units.py | 42 ++++++++++++++++------------ 1 file changed, 24 insertions(+), 18 deletions(-) diff --git a/templating/matrix_templates/units.py b/templating/matrix_templates/units.py index 5852b438f..c4ed8358c 100644 --- a/templating/matrix_templates/units.py +++ b/templating/matrix_templates/units.py @@ -116,7 +116,8 @@ class MatrixUnits(Units): "res_tables": [], "example": { "req": "", - "res": "" + "responses": [], + "good_response": "" } } self.log(".o.O.o. Endpoint: %s %s" % (method, path)) @@ -177,8 +178,10 @@ class MatrixUnits(Units): endpoint["req_param_by_loc"][p["loc"]] = [] endpoint["req_param_by_loc"][p["loc"]].append(p) - endpoint["example"]["responses"] = [] # Ordered list of maps + good_response = None for code, res in single_api.get("responses", {}).items(): + if not good_response and code == 200: + good_response = res description = res.get("description", "") example = res.get("examples", {}).get("application/json", "") if description and example: @@ -222,22 +225,25 @@ class MatrixUnits(Units): ) # add response params if this API has any. - res_type = Units.prop(res, "schema/type") - if res_type and res_type not in ["object", "array"]: - # response is a raw string or something like that - endpoint["res_tables"].append({ - "title": None, - "rows": [{ - "key": res["schema"].get("name", ""), - "type": res_type, - "desc": res.get("description", "") - }] - }) - elif res_type and Units.prop(res, "schema/properties"): # object - res_tables = get_json_schema_object_fields(res["schema"]) - for table in res_tables: - if "no-table" not in table: - endpoint["res_tables"].append(table) + if good_response: + res_type = Units.prop(good_response, "schema/type") + if res_type and res_type not in ["object", "array"]: + # response is a raw string or something like that + endpoint["res_tables"].append({ + "title": None, + "rows": [{ + "key": good_response["schema"].get("name", ""), + "type": res_type, + "desc": res.get("description", "") + }] + }) + elif res_type and Units.prop(good_response, "schema/properties"): + # response is an object: + schema = good_response["schema"] + res_tables = get_json_schema_object_fields(schema) + for table in res_tables: + if "no-table" not in table: + endpoint["res_tables"].append(table) endpoints.append(endpoint)