Merge pull request #394 from matrix-org/rav/required_response_fields

Mark required fields in response objects as such
pull/977/head
Richard van der Hoff 8 years ago committed by GitHub
commit 1f45724bfd

@ -23,6 +23,8 @@
(`#376 <https://github.com/matrix-org/matrix-doc/pull/376>`_). (`#376 <https://github.com/matrix-org/matrix-doc/pull/376>`_).
- Correct inconsistent specification of ``redacted_because`` fields and their - Correct inconsistent specification of ``redacted_because`` fields and their
values (`#378 <https://github.com/matrix-org/matrix-doc/pull/378>`_). values (`#378 <https://github.com/matrix-org/matrix-doc/pull/378>`_).
- Mark required fields in response objects as such
(`#394 <https://github.com/matrix-org/matrix-doc/pull/394>`_).
- Changes to the API which will be backwards-compatible for clients: - Changes to the API which will be backwards-compatible for clients:

@ -96,8 +96,7 @@ def inherit_parents(obj):
return result return result
def get_json_schema_object_fields(obj, enforce_title=False, def get_json_schema_object_fields(obj, enforce_title=False):
mark_required=True):
# Algorithm: # Algorithm:
# f.e. property => add field info (if field is object then recurse) # f.e. property => add field info (if field is object then recurse)
if obj.get("type") != "object": if obj.get("type") != "object":
@ -175,8 +174,7 @@ def get_json_schema_object_fields(obj, enforce_title=False,
try: try:
logger.debug("Processing property %s.%s", obj_title, key_name) logger.debug("Processing property %s.%s", obj_title, key_name)
required = key_name in required_keys required = key_name in required_keys
res = process_prop(key_name, props[key_name], required, res = process_prop(key_name, props[key_name], required)
mark_required)
first_table_rows.append(res["row"]) first_table_rows.append(res["row"])
tables.extend(res["tables"]) tables.extend(res["tables"])
@ -196,7 +194,7 @@ def get_json_schema_object_fields(obj, enforce_title=False,
return tables return tables
def process_prop(key_name, prop, required, mark_required): def process_prop(key_name, prop, required):
prop = inherit_parents(prop) prop = inherit_parents(prop)
value_type = None value_type = None
@ -213,7 +211,6 @@ def process_prop(key_name, prop, required, mark_required):
nested_objects = get_json_schema_object_fields( nested_objects = get_json_schema_object_fields(
prop, prop,
enforce_title=True, enforce_title=True,
mark_required=mark_required,
) )
value_type = nested_objects[0]["title"] value_type = nested_objects[0]["title"]
value_id = value_type value_id = value_type
@ -226,7 +223,6 @@ def process_prop(key_name, prop, required, mark_required):
nested_objects = get_json_schema_object_fields( nested_objects = get_json_schema_object_fields(
items, items,
enforce_title=True, enforce_title=True,
mark_required=mark_required,
) )
value_id = nested_objects[0]["title"] value_id = nested_objects[0]["title"]
value_type = "[%s]" % value_id value_type = "[%s]" % value_id
@ -269,7 +265,7 @@ def process_prop(key_name, prop, required, mark_required):
value_type = " or ".join(value_type) value_type = " or ".join(value_type)
if required and mark_required: if required:
desc = "**Required.** " + desc desc = "**Required.** " + desc
return { return {
@ -284,10 +280,9 @@ def process_prop(key_name, prop, required, mark_required):
} }
def get_tables_for_schema(schema, mark_required=True): def get_tables_for_schema(schema):
schema = inherit_parents(schema) schema = inherit_parents(schema)
tables = get_json_schema_object_fields(schema, tables = get_json_schema_object_fields(schema)
mark_required=mark_required)
# the result may contain duplicates, if objects are referred to more than # the result may contain duplicates, if objects are referred to more than
# once. Filter them out. # once. Filter them out.
@ -470,9 +465,7 @@ class MatrixUnits(Units):
elif res_type and Units.prop(good_response, "schema/properties"): elif res_type and Units.prop(good_response, "schema/properties"):
# response is an object: # response is an object:
schema = good_response["schema"] schema = good_response["schema"]
res_tables = get_tables_for_schema(schema, res_tables = get_tables_for_schema(schema)
mark_required=False,
)
endpoint["res_tables"].extend(res_tables) endpoint["res_tables"].extend(res_tables)
elif res_type and Units.prop(good_response, "schema/items"): elif res_type and Units.prop(good_response, "schema/items"):
# response is an array: # response is an array:

Loading…
Cancel
Save