|
| 1 | +#!/usr/bin/env python |
| 2 | + |
| 3 | +# -------------------------------------------------------------------------------------------- |
| 4 | +# Copyright (c) Microsoft Corporation. All rights reserved. |
| 5 | +# Licensed under the MIT License. See License.txt in the project root for license information. |
| 6 | +# -------------------------------------------------------------------------------------------- |
| 7 | + |
| 8 | +# -------------------------------------------------------------------------------------------- |
| 9 | +# This checker is designed to clean up reported changes related to libraries migrating from |
| 10 | +# swagger -> typespec where models that where previously flattened are no longer flattened. |
| 11 | +# The checker will check for the following: |
| 12 | +# 1. Check if a model has a new `properties`` field |
| 13 | +# 2. Check if there is a corresponding `<model name>Properties` model |
| 14 | +# 3. Remove entries related to the new `properties`` field from the diff |
| 15 | +# 4. Remove entries related to the new `<model name>Properties` model from the diff |
| 16 | +# 5. Remove entries related losing properties on the original model |
| 17 | +# -------------------------------------------------------------------------------------------- |
| 18 | + |
| 19 | +import copy |
| 20 | +import sys |
| 21 | +import os |
| 22 | +sys.path.append(os.path.abspath("../../scripts/breaking_changes_checker")) |
| 23 | + |
| 24 | + |
| 25 | +class UnflattenedModelsChecker: |
| 26 | + def run_check(self, breaking_changes: list, features_added: list, *, diff: dict, stable_nodes: dict, current_nodes: dict, **kwargs) -> tuple[list, list]: |
| 27 | + properties_model_entry = () |
| 28 | + properties_field_entry = () |
| 29 | + |
| 30 | + breaking_changes_copy = copy.deepcopy(breaking_changes) |
| 31 | + features_added_copy = copy.deepcopy(features_added) |
| 32 | + # Look for new "*Properties" models and their corresponding properties field in the original model |
| 33 | + for fa in features_added: |
| 34 | + _, change_type, module_name, class_name, *args = fa |
| 35 | + if change_type == "AddedClass" and class_name.endswith("Properties"): |
| 36 | + properties_model_entry = fa |
| 37 | + # Check if the corresponding model has a properties field |
| 38 | + original_model_name = class_name[:-len("Properties")] |
| 39 | + if original_model_name in stable_nodes[module_name]["class_nodes"]: |
| 40 | + original_model = stable_nodes[module_name]["class_nodes"][original_model_name] |
| 41 | + for fa2 in features_added: |
| 42 | + _, change_type2, module_name2, class_name2, *args2 = fa2 |
| 43 | + # Check if the class name is the original model name |
| 44 | + if class_name2 and class_name2 == original_model_name: |
| 45 | + if change_type2 == "AddedClassProperty" and "properties" == args2[0]: |
| 46 | + properties_field_entry = fa2 |
| 47 | + break |
| 48 | + if not properties_field_entry or not properties_model_entry: |
| 49 | + continue |
| 50 | + # Check the breaking changes list for entries about removing any of the properties that existed in the original model |
| 51 | + for prop in original_model["properties"]: |
| 52 | + for breaking_change in breaking_changes_copy: |
| 53 | + _, bc_change_type, bc_module_name, bc_class_name, *bc_args = breaking_change |
| 54 | + if (bc_change_type == "RemovedOrRenamedInstanceAttribute" and |
| 55 | + bc_class_name == original_model_name and |
| 56 | + bc_args[0] == prop): |
| 57 | + # If we find a breaking change about removing a property, delete it from the breaking changes list |
| 58 | + breaking_changes_copy.remove(breaking_change) |
| 59 | + break |
| 60 | + if properties_model_entry and properties_field_entry: |
| 61 | + # If we found both the properties model and the properties field, remove them from the features added list |
| 62 | + # since this means the model was unflattened |
| 63 | + features_added_copy.remove(properties_model_entry) |
| 64 | + features_added_copy.remove(properties_field_entry) |
| 65 | + properties_field_entry, properties_model_entry = (), () |
| 66 | + return breaking_changes_copy, features_added_copy |
0 commit comments