@@ -356,20 +356,26 @@ def _fetch_schema(self, fetchSchemaParam: _FetchSchemaParam = None) -> None:
356356 if not page .exists :
357357 print (f"Error: Page { schema_title } does not exist" )
358358 return
359- if schema_title .startswith ("Category:" ):
359+ # not only in the JsonSchema namespace the schema is located in the main sot
360+ # in all other namespaces, the json_schema slot is used
361+ if schema_title .startswith ("JsonSchema:" ):
362+ schema_str = ""
363+ if page .get_slot_content ("main" ):
364+ schema_str = json .dumps (page .get_slot_content ("main" ))
365+ else :
360366 schema_str = ""
361367 if page .get_slot_content ("jsonschema" ):
362368 schema_str = json .dumps (page .get_slot_content ("jsonschema" ))
363- else :
364- schema_str = page .get_slot_content ("main" )
365- if schema_str and not isinstance (schema_str , str ):
366- schema_str = json .dumps (schema_str )
367369 if (schema_str is None ) or (schema_str == "" ):
368370 print (f"Error: Schema { schema_title } does not exist" )
369- return
371+ schema_str = "{}" # empty schema to make reference work
370372 schema = json .loads (
371- schema_str .replace ("$ref" , "dollarref" )
372- ) # '$' is a special char for root object in jsonpath
373+ schema_str .replace (
374+ "$ref" , "dollarref"
375+ ).replace ( # '$' is a special char for root object in jsonpath
376+ '"allOf": [' , '"allOf": [{},'
377+ ) # fix https://github.com/koxudaxi/datamodel-code-generator/issues/1910
378+ )
373379 print (f"Fetch { schema_title } " )
374380
375381 jsonpath_expr = parse ("$..dollarref" )
@@ -626,15 +632,15 @@ def load_entity(
626632 print ("Error: no schema defined" )
627633
628634 elif len (schemas ) == 1 :
629- cls = schemas [0 ]["title" ]
630- entity : model .Entity = eval ( f"model. { cls } (**jsondata)" )
635+ cls = getattr ( model , schemas [0 ]["title" ])
636+ entity : model .Entity = cls (** jsondata )
631637
632638 else :
633639 bases = []
634640 for schema in schemas :
635- bases .append (eval ( " model." + schema ["title" ]))
641+ bases .append (getattr ( model , schema ["title" ]))
636642 cls = create_model ("Test" , __base__ = tuple (bases ))
637- entity = cls (** jsondata )
643+ entity : model . Entity = cls (** jsondata )
638644
639645 if entity is not None :
640646 # make sure we do not override existing meta data
@@ -957,6 +963,8 @@ def store_entity(
957963 if not isinstance (param .entities , list ):
958964 param .entities = [param .entities ]
959965
966+ param : OSW .StoreEntityParam = param
967+
960968 max_index = len (param .entities )
961969
962970 meta_category = self .site .get_page (
@@ -970,12 +978,12 @@ def store_entity(
970978 meta_category = self .site .get_page (
971979 WtSite .GetPageParam (titles = [param .meta_category_title ])
972980 ).pages [0 ]
973- param . meta_category_template_str = meta_category .get_slot_content (
981+ meta_category_template_str = meta_category .get_slot_content (
974982 "schema_template"
975983 )
976- if param . meta_category_template_str :
984+ if meta_category_template_str :
977985 meta_category_template = compile_handlebars_template (
978- param . meta_category_template_str
986+ meta_category_template_str
979987 )
980988
981989 def store_entity_ (
@@ -1010,10 +1018,26 @@ def store_entity_(
10101018 schema_str = eval_compiled_handlebars_template (
10111019 meta_category_template ,
10121020 page .get_slot_content ("jsondata" ),
1013- {"_page_title" : entity_title },
1021+ {
1022+ "_page_title" : entity_title , # legacy
1023+ "_current_subject_" : entity_title ,
1024+ },
10141025 )
10151026 schema = json .loads (schema_str )
1016- page .set_slot_content ("jsonschema" , schema )
1027+ # put generated schema in definitions section
1028+ # currently only enabled for Characteristics
1029+ if hasattr (model , "MetaCharacteristic" ) and isinstance (
1030+ entity , model .MetaCharacteristic
1031+ ):
1032+ new_schema = {
1033+ "$defs" : {"generated" : schema },
1034+ "allOf" : [{"$ref" : "#/$defs/generated" }],
1035+ }
1036+ new_schema ["@context" ] = schema .pop ("@context" , None )
1037+ new_schema ["title" ] = schema .pop ("title" , "" )
1038+ schema ["title" ] = "Generated" + new_schema ["title" ]
1039+ schema = new_schema
1040+ page .set_slot_content ("jsonschema" , new_schema )
10171041 except Exception as e :
10181042 print (
10191043 f"Schema generation from template failed for " f"{ entity } : { e } "
0 commit comments