@@ -399,6 +399,92 @@ def aggregate_generated_schemas(
399399 return AggregateGeneratedSchemasResult (aggregated_schema = schema )
400400
401401
402+ def merge_generated_definitions (schema : JsonType ):
403+ """Merges the generated definitions into the schema.
404+ Example:
405+ {
406+ "$defs": {
407+ "generated": {
408+ "title": "Generated",
409+ "description": "Generated schema",
410+ "description*": {
411+ "en": "Actual description",
412+ }
413+ }
414+ },
415+ "$ref": "#/$defs/generated",
416+ "title": "Overwritten"
417+ }
418+ will be merged into the schema
419+ {
420+ "title": "Overwritten",
421+ "description": "Actual description"
422+ }
423+ """
424+
425+ generated_content = schema .get ("$defs" , {}).get ("generated" )
426+
427+ if generated_content :
428+ # Check if "$ref": "#/$defs/generated" exists directly in the dictionary
429+ if schema .get ("$ref" ) == "#/$defs/generated" :
430+
431+ # if schema has no description use the multi-lang description
432+ # (pref: en, else first) in the $defs section
433+ if (
434+ "description" not in schema
435+ or schema ["description" ] is None
436+ or schema ["description" ] == ""
437+ ):
438+ if (
439+ "description*" in generated_content
440+ and len (generated_content ["description*" ]) > 0
441+ ):
442+ first_key = list (generated_content ["description*" ].keys ())[0 ]
443+ schema ["description" ] = generated_content ["description*" ].get (
444+ "en" , generated_content ["description*" ][first_key ]
445+ )
446+ else :
447+ schema ["description" ] = generated_content .get ("description" )
448+
449+ schema = merge_deep (deepcopy (generated_content ), schema )
450+ schema .pop ("$ref" , None ) # Remove the reference after merging
451+
452+ # Check if "$ref": "#/$defs/generated" is contained in "allOf"
453+ if "allOf" in schema :
454+ for _index , item in enumerate (schema ["allOf" ]):
455+ if item .get ("$ref" ) == "#/$defs/generated" :
456+ # Remove the reference after merging
457+ for i , v in enumerate (schema ["allOf" ]):
458+ if v .get ("$ref" ) == "#/$defs/generated" :
459+ del schema ["allOf" ][i ]
460+ break
461+ # if schema has no description use the multi-lang description
462+ # (pref: en, else first) in the $defs section
463+ if (
464+ "description" not in schema
465+ or schema ["description" ] is None
466+ or schema ["description" ] == ""
467+ ):
468+ if (
469+ "description*" in generated_content
470+ and len (generated_content ["description*" ]) > 0
471+ ):
472+ first_key = list (generated_content ["description*" ].keys ())[
473+ 0
474+ ]
475+ schema ["description" ] = generated_content [
476+ "description*"
477+ ].get ("en" , generated_content ["description*" ][first_key ])
478+ else :
479+ schema ["description" ] = generated_content .get ("description" )
480+ schema = merge_deep (deepcopy (generated_content ), schema )
481+ break
482+
483+ # Remove "generated" from "$defs"
484+ schema .get ("$defs" , {}).pop ("generated" , None )
485+ return schema
486+
487+
402488def escape_json_strings (obj : JsonType ) -> JsonType :
403489 """replace double quotes `"` with escaped double quotes `\" ` in
404490 and non-standard escape-squences in strings.
0 commit comments