genschema.postprocessing.schema_references

Attributes

Classes

SchemaReferenceExtractionConfig

SchemaCandidate

CandidateGroup

SchemaReferencePostprocessor

Standalone JSON Schema postprocessor that extracts repeated or highly similar

Module Contents

type PathSegment = str | int[source]
type SchemaPath = tuple[PathSegment, ...][source]
type ComparatorFactory = Callable[[], Comparator][source]
type SimilarityMetric = Callable[[frozenset[str], frozenset[str]], float][source]
type MergeStrategy = Callable[[list[dict], 'SchemaReferenceExtractionConfig'], dict][source]
type NameFactory = Callable[[int, 'CandidateGroup', 'SchemaReferenceExtractionConfig'], str][source]
DEFAULT_COMPARATOR_FACTORIES: tuple[ComparatorFactory, Ellipsis][source]
DEFINITION_SECTION_KEYS[source]
STRUCTURAL_CONTAINER_KEYS = ('items', 'additionalProperties', 'contains', 'if', 'then', 'else', 'not', 'propertyNames',...[source]
STRUCTURAL_VARIANT_KEYS = ('anyOf', 'oneOf', 'allOf', 'prefixItems')[source]
MEANINGFUL_PATH_PARTS_BLACKLIST[source]
class SchemaReferenceExtractionConfig[source]
similarity_threshold: float = 0.85[source]
min_total_keys: int = 3[source]
min_occurrences: int = 2[source]
defs_key: str = '$defs'[source]
ref_prefix: str | None = None[source]
merge_base_of: Literal['anyOf', 'oneOf', 'allOf'] = 'anyOf'[source]
merge_pseudo_handler: PseudoArrayHandlerBase | None = None[source]
merge_comparator_factories: tuple[ComparatorFactory, Ellipsis][source]
similarity_metric: SimilarityMetric[source]
merge_strategy: MergeStrategy | None = None[source]
name_factory: NameFactory | None = None[source]
allowed_root_types: tuple[str, Ellipsis] = ('object', 'array')[source]
preserve_common_keywords: bool = True[source]
include_root: bool = False[source]
skip_existing_definitions: bool = True[source]
__post_init__() None[source]
property normalized_ref_prefix: str[source]
class SchemaCandidate[source]
path: SchemaPath[source]
schema: dict[source]
type_signature: tuple[str, Ellipsis][source]
tokens: frozenset[str][source]
total_keys: int[source]
class CandidateGroup[source]
members: list[SchemaCandidate][source]
merged_schema: dict[source]
total_keys: int[source]
benefit: int[source]
definition_name: str = ''[source]
class SchemaReferencePostprocessor[source]

Standalone JSON Schema postprocessor that extracts repeated or highly similar structures into shared definitions and replaces occurrences with $ref.

The postprocessor is intentionally independent from Converter itself: it can be run on any already-built schema. Candidate groups are merged through a fresh internal Converter run so the resulting definition stays aligned with the project’s normal schema-combination pipeline.

classmethod process(
schema: dict,
config: SchemaReferenceExtractionConfig | None = None,
) dict[source]
classmethod extract(
schema: dict,
config: SchemaReferenceExtractionConfig | None = None,
) dict[source]