# Buggy code
def sort_data_models(
    unsorted_data_models: List[DataModel],
    sorted_data_models: Optional[SortedDataModels] = None,
    require_update_action_models: Optional[List[str]] = None,
) -> Tuple[List[DataModel], SortedDataModels, List[str]]:
    if sorted_data_models is None:
        sorted_data_models = OrderedDict()
    if require_update_action_models is None:
        require_update_action_models = []

    unresolved_references: List[DataModel] = []
    for model in unsorted_data_models:
        if not model.reference_classes:
            sorted_data_models[model.name] = model
        elif (
            model.name in model.reference_classes and len(model.reference_classes) == 1
        ):  # only self-referencing
            sorted_data_models[model.name] = model
            require_update_action_models.append(model.name)
        elif (
            not set(model.reference_classes) - set(model.name) - set(sorted_data_models)
        ):  # reference classes have been resolved
            sorted_data_models[model.name] = model
            if model.name in model.reference_classes:
                require_update_action_models.append(model.name)
        else:
            unresolved_references.append(model)
    if unresolved_references:
        try:
            return sort_data_models(
                unresolved_references, sorted_data_models, require_update_action_models
            )
        except RecursionError:
            unresolved_classes = ", ".join(
                f"[class: {item.name} references: {item.reference_classes}]"
                for item in unresolved_references
            )
            raise Exception(f"A Parser can not resolve classes: {unresolved_classes}.")
    return unresolved_references, sorted_data_models, require_update_action_models


# Traceback (pydantic.error_wrappers.ValidationError)
Traceback (most recent call last):
File "/proj/.venv/bin/datamodel-codegen", line 8, in <module>
sys.exit(main())
File "/proj/.venv/lib/python3.7/site-packages/datamodel_code_generator/__main__.py", line 168, in main
result = parser.parse()
File "/proj/.venv/lib/python3.7/site-packages/datamodel_code_generator/parser/base.py", line 164, in parse
self.parse_raw()
File "/proj/.venv/lib/python3.7/site-packages/datamodel_code_generator/parser/jsonschema.py", line 472, in parse_raw
self.parse_raw_obj(obj_name, raw_obj)
File "/proj/.venv/lib/python3.7/site-packages/datamodel_code_generator/parser/jsonschema.py", line 455, in parse_raw_obj
obj = JsonSchemaObject.parse_obj(raw)
File "/proj/.venv/lib/python3.7/site-packages/pydantic/main.py", line 402, in parse_obj
return cls(**obj)
File "/proj/.venv/lib/python3.7/site-packages/pydantic/main.py", line 283, in __init__
raise validation_error
pydantic.error_wrappers.ValidationError: 2 validation errors for JsonSchemaObject
properties -> apiVersion -> type
str type expected (type=type_error.str)
properties -> kind -> type
str type expected (type=type_error.str)

# Fixed code
def sort_data_models(
    unsorted_data_models: List[DataModel],
    sorted_data_models: Optional[SortedDataModels] = None,
    require_update_action_models: Optional[List[str]] = None,
) -> Tuple[List[DataModel], SortedDataModels, List[str]]:
    if sorted_data_models is None:
        sorted_data_models = OrderedDict()
    if require_update_action_models is None:
        require_update_action_models = []

    unresolved_references: List[DataModel] = []
    for model in unsorted_data_models:
        if not model.reference_classes:
            sorted_data_models[model.name] = model
        elif (
            model.name in model.reference_classes and len(model.reference_classes) == 1
        ):  # only self-referencing
            sorted_data_models[model.name] = model
            require_update_action_models.append(model.name)
        elif (
            not set(model.reference_classes) - {model.name} - set(sorted_data_models)
        ):  # reference classes have been resolved
            sorted_data_models[model.name] = model
            if model.name in model.reference_classes:
                require_update_action_models.append(model.name)
        else:
            unresolved_references.append(model)
    if unresolved_references:
        try:
            return sort_data_models(
                unresolved_references, sorted_data_models, require_update_action_models
            )
        except RecursionError:
            unresolved_classes = ", ".join(
                f"[class: {item.name} references: {item.reference_classes}]"
                for item in unresolved_references
            )
            raise Exception(f"A Parser can not resolve classes: {unresolved_classes}.")
    return unresolved_references, sorted_data_models, require_update_action_models