INNER CODE UNIT · Python

_generate_json_schema

phillipdupuis/pydantic-to-typescript · pydantic2ts/cli/script.py:269

def _generate_json_schema(models: List[type]) -> str:
    """
    Create a top-level '_Master_' model with references to each of the actual models.
    Generate the schema for this model, which will include the schemas for all the
    nested models. Then clean up the schema.
    """
    with ExitStack() as stack:
        models_by_name: Dict[str, type] = {}
        models_as_fields: Dict[str, Tuple[type, Any]] = {}

        for model in models:
            stack.enter_context(_schema_generation_overrides(model))
            name = model.__name__
            models_by_name[name] = model
            models_as_fields[name] = (model, ...)

        use_v1_tools = any(issubclass(m, v1.BaseModel) for m in models)
        create_model = v1.create_model if use_v1_tools else v2.create_model  # type: ignore

View source record →

📰 Research Paper
Loading…
⏳ Fetching content…