From d7e0009ce528dd54be3ebc84e9ab5b3119dd69e7 Mon Sep 17 00:00:00 2001 From: Scott Sadler Date: Mon, 28 Nov 2016 16:03:11 +0100 Subject: [PATCH] Drop definitions from schemas since they clutter log output --- bigchaindb/common/schema/__init__.py | 23 +++++++------- tests/common/test_schema.py | 45 ++++++++++++++++++++++------ 2 files changed, 48 insertions(+), 20 deletions(-) diff --git a/bigchaindb/common/schema/__init__.py b/bigchaindb/common/schema/__init__.py index 96b2c8be..675b17e7 100644 --- a/bigchaindb/common/schema/__init__.py +++ b/bigchaindb/common/schema/__init__.py @@ -9,15 +9,16 @@ from bigchaindb.common.exceptions import SchemaValidationError def drop_schema_descriptions(node): """ Drop descriptions from schema, since they clutter log output """ - if isinstance(node, list): - for val in node: - drop_schema_descriptions(val) - elif isinstance(node, dict): - if node.get('type') == 'object': - if 'description' in node: - del node['description'] - for val in node.values(): - drop_schema_descriptions(val) + if 'description' in node: + del node['description'] + for n in node.get('properties', {}).values(): + drop_schema_descriptions(n) + for n in node.get('definitions', {}).values(): + drop_schema_descriptions(n) + for n in node.get('anyOf', []): + drop_schema_descriptions(n) + + def _load_schema(name): @@ -25,8 +26,8 @@ def _load_schema(name): path = os.path.join(os.path.dirname(__file__), name + '.yaml') with open(path) as handle: schema = yaml.safe_load(handle) - drop_schema_descriptions(schema) - return path, schema + drop_schema_descriptions(schema) + return path, schema def _validate_schema(schema, body): diff --git a/tests/common/test_schema.py b/tests/common/test_schema.py index 8f611554..c7990c39 100644 --- a/tests/common/test_schema.py +++ b/tests/common/test_schema.py @@ -1,4 +1,3 @@ -from copy import deepcopy from bigchaindb.common.schema import TX_SCHEMA, VOTE_SCHEMA, \ drop_schema_descriptions @@ -29,14 +28,42 @@ def test_vote_schema_additionalproperties(): def test_drop_descriptions(): node = { - 'a': 1, 'description': 'abc', - 'b': [{ - 'type': 'object', - 'description': 'gone, baby', - }] + 'properties': { + 'description': { + 'description': ('The property named "description" should stay' + 'but description meta field goes'), + }, + 'properties': { + 'description': 'this must go' + }, + 'any': { + 'anyOf': [ + { + 'description': 'must go' + } + ] + } + }, + 'definitions': { + 'wat': { + 'description': "go" + } + } } - node2 = deepcopy(node) drop_schema_descriptions(node) - del node2['b'][0]['description'] - assert node == node2 + expected = { + 'properties': { + 'description': {}, + 'properties': {}, + 'any': { + 'anyOf': [ + {} + ] + } + }, + 'definitions': { + 'wat': {}, + } + } + assert node == expected