From d12c00acb6b0f30dea93b4d42d4354fa3ac996af Mon Sep 17 00:00:00 2001 From: belthlemar Date: Mon, 4 Mar 2024 19:22:38 +0100 Subject: [PATCH] fix(variants): don't raise Recursive error when creating big variant tree --- antarest/study/storage/variantstudy/model/model.py | 8 +++----- .../variant_blueprint/test_variant_manager.py | 13 +++++++++++++ 2 files changed, 16 insertions(+), 5 deletions(-) diff --git a/antarest/study/storage/variantstudy/model/model.py b/antarest/study/storage/variantstudy/model/model.py index 1e51032ce4..3d881f6429 100644 --- a/antarest/study/storage/variantstudy/model/model.py +++ b/antarest/study/storage/variantstudy/model/model.py @@ -1,4 +1,4 @@ -from typing import List, Optional, Tuple, Union +from typing import List, NamedTuple, Optional, Tuple, Union from pydantic import BaseModel @@ -26,9 +26,7 @@ class CommandResultDTO(BaseModel): message: str -class VariantTreeDTO(BaseModel): +class VariantTreeDTO(NamedTuple): + # Don't use BaseModel as this could trigger Recursion exceptions, since we're using Pydantic with a version prior to v2. node: StudyMetadataDTO children: List["VariantTreeDTO"] - - -VariantTreeDTO.update_forward_refs() diff --git a/tests/integration/variant_blueprint/test_variant_manager.py b/tests/integration/variant_blueprint/test_variant_manager.py index df3cf590e4..8a300e75da 100644 --- a/tests/integration/variant_blueprint/test_variant_manager.py +++ b/tests/integration/variant_blueprint/test_variant_manager.py @@ -238,3 +238,16 @@ def test_comments(client: TestClient, admin_access_token: str, variant_id: str) # Asserts comments did not disappear res = client.get(f"/v1/studies/{variant_id}/comments", headers=admin_headers) assert res.json() == comment + + +def test_recursive_variant_tree(client: TestClient, admin_access_token: str): + admin_headers = {"Authorization": f"Bearer {admin_access_token}"} + base_study_res = client.post("/v1/studies?name=foo", headers=admin_headers) + base_study_id = base_study_res.json() + parent_id = base_study_res.json() + for k in range(150): + res = client.post(f"/v1/studies/{base_study_id}/variants?name=variant_{k}", headers=admin_headers) + base_study_id = res.json() + # Asserts that we do not trigger a Recursive Exception + res = client.get(f"/v1/studies/{parent_id}/variants", headers=admin_headers) + assert res.status_code == 200