Process a set of models and generate unique names for them to be used as keys in the JSON Schema definitions. By default the names are the same as the class name. But if two models in different Python modules have the same name (e.g. "users.Model" and "items.Model"), the generated names
(unique_models: TypeModelSet)
| 320 | |
| 321 | |
| 322 | def get_model_name_map(unique_models: TypeModelSet) -> Dict[TypeModelOrEnum, str]: |
| 323 | """ |
| 324 | Process a set of models and generate unique names for them to be used as keys in the JSON Schema |
| 325 | definitions. By default the names are the same as the class name. But if two models in different Python |
| 326 | modules have the same name (e.g. "users.Model" and "items.Model"), the generated names will be |
| 327 | based on the Python module path for those conflicting models to prevent name collisions. |
| 328 | |
| 329 | :param unique_models: a Python set of models |
| 330 | :return: dict mapping models to names |
| 331 | """ |
| 332 | name_model_map = {} |
| 333 | conflicting_names: Set[str] = set() |
| 334 | for model in unique_models: |
| 335 | model_name = normalize_name(model.__name__) |
| 336 | if model_name in conflicting_names: |
| 337 | model_name = get_long_model_name(model) |
| 338 | name_model_map[model_name] = model |
| 339 | elif model_name in name_model_map: |
| 340 | conflicting_names.add(model_name) |
| 341 | conflicting_model = name_model_map.pop(model_name) |
| 342 | name_model_map[get_long_model_name(conflicting_model)] = conflicting_model |
| 343 | name_model_map[get_long_model_name(model)] = model |
| 344 | else: |
| 345 | name_model_map[model_name] = model |
| 346 | return {v: k for k, v in name_model_map.items()} |
| 347 | |
| 348 | |
| 349 | def get_flat_models_from_model(model: Type['BaseModel'], known_models: Optional[TypeModelSet] = None) -> TypeModelSet: |
no test coverage detected