From 5d925bdfaf946ee49e88647419afefe2a63febdf Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Krzysztof=20Le=C5=9Bniak?= <klesniak@syncad.com>
Date: Thu, 9 Jul 2020 14:14:09 +0200
Subject: [PATCH] schema generator will now merge with existing schema

This means that existing schema will not be overwritten, but merged.
This allows to manually edit generated schema and these changes will not
be lost after subsequent schema generation.
---
 hived/pyresttest_api_tests/schema_generator.py | 18 +++++++++++++++---
 1 file changed, 15 insertions(+), 3 deletions(-)

diff --git a/hived/pyresttest_api_tests/schema_generator.py b/hived/pyresttest_api_tests/schema_generator.py
index c174c222..bb831ce7 100644
--- a/hived/pyresttest_api_tests/schema_generator.py
+++ b/hived/pyresttest_api_tests/schema_generator.py
@@ -17,7 +17,7 @@ def json_schema_generator(config):
     output.output_file = config['output_file']
     return output
 
-def gen_schema(json):
+def gen_schema(json, existing_schema):
     """Returns schema for given json string
 
     :json: json string
@@ -25,9 +25,20 @@ def gen_schema(json):
 
     """
     builder = SchemaBuilder()
+    builder.add_schema(existing_schema)
     builder.add_object(json)
     return builder.to_json(indent=2)
 
+def read_schema(file_name):
+    """Read json schema from a file.
+
+    :file_name: name of the file containing json
+    :returns: json schema as python object
+
+    """
+    with open(file_name, "r") as f:
+        return json.loads(f.read())
+
 def write(file_name, data):
     """Writes given data to file named filename
 
@@ -49,8 +60,9 @@ class JSONSchemaGenerator(validators.AbstractValidator):
         schema_file = string.Template(self.schema_file).safe_substitute(context.get_values())
         output_file = string.Template(self.output_file).safe_substitute(context.get_values())
         write(output_file, json.dumps(output, indent=2))
-        schema = gen_schema(output)
-        write(schema_file, schema)
+        existing_schema = read_schema(schema_file)
+        updated_schema = gen_schema(output, existing_schema)
+        write(schema_file, updated_schema)
         return False
 
 VALIDATORS = { 'json_schema_generate': json_schema_generator }
-- 
GitLab