Selaa lähdekoodia

Merge branch 'master' of https://intra.acdp.at/gogs/tanja/cdplib

ogert 4 vuotta sitten
vanhempi
commit
ca7cd306e3
2 muutettua tiedostoa jossa 39 lisäystä ja 20 poistoa
  1. 1 1
      cdplib/db_handlers/MongodbHandler.py
  2. 38 19
      cdplib/db_migration/ParseJsonSchema.py

+ 1 - 1
cdplib/db_handlers/MongodbHandler.py

@@ -173,7 +173,7 @@ class MongodbHandler:
         command = {
                     'collMod': collection_name,
                     'validator': {
-                        '$jsonSchema': parse_obj.schemas[0]
+                        '$jsonSchema': parse_obj.load_and_parse_schema_for_mongodb(schema_path)
                     },
                     'validationLevel': validation_level,
                     'validationAction': validation_action

+ 38 - 19
cdplib/db_migration/ParseJsonSchema.py

@@ -51,15 +51,17 @@ class ParseJsonSchema(ParseDbSchema):
                 with open(schema_path, "r") as f:
                     schema = json.load(f)
 
-                ref_flag = self._analyze_schema(schema)
-
-                if ref_flag:
-                    schema = self._format_schema_for_mongo(schema)
+                definitions_flag = self._analyze_schema(schema)
+                
+                if definitions_flag:
+                    schema = self._clean_desciptions_tags_from_single_quotes(schema)
                     schema = self._dereference_schema(schema)
-                    schema = self._format_schema_for_mongo(schema)
+                    # Need to do it again since sub schema could also contain
+                    # single quotes
+                    schema = self._clean_desciptions_tags_from_single_quotes(schema)
                     self.schemas.append(schema)
+                    
                 else:
-                    schema = self._format_schema_for_mongo(schema)
                     self.schemas.append(schema)
 
             except Exception as e:
@@ -199,7 +201,7 @@ class ParseJsonSchema(ParseDbSchema):
                                  required_only=required_only)
 
         for schema in schemas[1:]:
-
+            
             next_result = self._parse_one(schema=schema,
                                           field_info=field_info,
                                           required_only=required_only)
@@ -340,7 +342,7 @@ class ParseJsonSchema(ParseDbSchema):
 
         return already_parsed
 
-    def read_schema_and_parse_for_mongodb(self, schema_path: str) -> dict:
+    def load_and_parse_schema_for_mongodb(self, schema_path: str) -> dict:
         '''
         We need to deference json before import to Mongo DB pymongo can't deal with references
         :param str schema_path: path to the schema file.
@@ -353,10 +355,15 @@ class ParseJsonSchema(ParseDbSchema):
             schema = json.load(json_file)
 
         definitions_flag = self._analyze_schema(schema)
-
+        
         if definitions_flag:
-            schema = self._format_schema_for_mongo(schema)
+            schema = self._clean_desciptions_tags_from_single_quotes(schema)
             schema = self._dereference_schema(schema)
+             # Need to do it again since sub schema could also contain
+             # single quotes
+            schema = self._clean_desciptions_tags_from_single_quotes(schema)
+            schema = self._format_schema_for_mongo(schema)
+        else:
             schema = self._format_schema_for_mongo(schema)
 
         return schema
@@ -373,12 +380,11 @@ class ParseJsonSchema(ParseDbSchema):
                 definitions_flag = self._analyze_schema(schema[key], definitions_flag)
 
         return definitions_flag
-
-    def _format_schema_for_mongo(self, schema: dict) -> dict:
+    
+    
+    def _clean_desciptions_tags_from_single_quotes(self, schema: dict) -> dict:
         '''
-        We use in the schema tags whih are not supported by mongo an threfore
-        must be taken care of before setting the schema for mongo.
-        :param str schema_path: path to the schema file.
+        :param dict schema: dictonary containing schema
         '''
 
         for key in list(schema):
@@ -388,14 +394,27 @@ class ParseJsonSchema(ParseDbSchema):
                 schema[key] = cleaned_description
 
             if type(schema[key]) == dict:
-                self._format_schema_for_mongo(schema[key])
+                self._clean_desciptions_tags_from_single_quotes(schema[key])
+                
+        return schema
 
-            if key == 'examples':
-                self._remove_examples(schema)
+    def _format_schema_for_mongo(self, schema: dict) -> dict:
+        '''
+        We use in the schema tags whih are not supported by mongo an threfore
+        must be taken care of before setting the schema for mongo.
+        :param str schema_path: path to the schema file.
+        '''
 
+        for key in list(schema):
+
+            if type(schema[key]) == dict:
+                self._format_schema_for_mongo(schema[key])
 
             if key == 'default' or key == 'default_values':
                 self._remove_defaults(schema)
+                
+            if key == 'examples':
+                self._remove_examples(schema)
 
         return schema
 
@@ -413,7 +432,7 @@ class ParseJsonSchema(ParseDbSchema):
         schema = str(schema).replace("'", "\"")
         schema = jsonref.loads(schema, base_uri=base_dir_url)
         schema = deepcopy(schema)
-        #schema.pop('definitions', None)
+
         return schema
 
     def _remove_defaults(self, schema: dict) -> dict: