From 3fc4959a4391ed3c21a8ba48ff7f3cb9d5480600 Mon Sep 17 00:00:00 2001 From: Yugo Kato Date: Fri, 27 Sep 2024 11:01:49 -0700 Subject: [PATCH] Fix properly handle backslashes in Constraint.pattern --- .../libraries/api/api_client_generator.py | 6 ++++-- .../libraries/api/api_functions/utils/param_type.py | 5 ++++- src/openapi_test_client/libraries/common/constants.py | 1 + 3 files changed, 9 insertions(+), 3 deletions(-) diff --git a/src/openapi_test_client/libraries/api/api_client_generator.py b/src/openapi_test_client/libraries/api/api_client_generator.py index 969e8fa..a560fc4 100644 --- a/src/openapi_test_client/libraries/api/api_client_generator.py +++ b/src/openapi_test_client/libraries/api/api_client_generator.py @@ -29,7 +29,7 @@ from openapi_test_client.libraries.api.api_classes import get_api_classes, init_api_classes from openapi_test_client.libraries.api.types import ParamModel from openapi_test_client.libraries.common.code import diff_code, format_code -from openapi_test_client.libraries.common.constants import TAB, VALID_METHODS +from openapi_test_client.libraries.common.constants import BACKSLASH, TAB, VALID_METHODS from openapi_test_client.libraries.common.misc import ( camel_to_snake, generate_class_name, @@ -386,7 +386,9 @@ def update_existing_endpoints(target_api_class: type[APIClassType] = api_class): ) # Update API function signatures - new_func_signature = endpoint_model_util.generate_func_signature_in_str(endpoint_model) + new_func_signature = endpoint_model_util.generate_func_signature_in_str(endpoint_model).replace( + BACKSLASH, BACKSLASH * 2 + ) updated_api_func_code = re.sub(re.escape(signature), new_func_signature, updated_api_func_code) # Update func body if missing diff --git a/src/openapi_test_client/libraries/api/api_functions/utils/param_type.py b/src/openapi_test_client/libraries/api/api_functions/utils/param_type.py index daa3b84..5bdd13c 100644 --- a/src/openapi_test_client/libraries/api/api_functions/utils/param_type.py +++ b/src/openapi_test_client/libraries/api/api_functions/utils/param_type.py @@ -10,6 +10,7 @@ import openapi_test_client.libraries.api.api_functions.utils.param_model as param_model_util from openapi_test_client.libraries.api.types import Alias, Constraint, Format, ParamDef +from openapi_test_client.libraries.common.constants import BACKSLASH logger = get_logger(__name__) @@ -53,7 +54,9 @@ def get_type_annotation_as_str(tp: Any) -> str: return f"{type(tp).__name__}({repr(tp.value)})" elif isinstance(tp, Constraint): const = ", ".join( - f'{k}={("r" + repr(v) if k == "pattern" else repr(v))}' for k, v in asdict(tp).items() if v is not None + f'{k}={("r" + repr(v).replace(BACKSLASH * 2, BACKSLASH) if k == "pattern" else repr(v))}' + for k, v in asdict(tp).items() + if v is not None ) return f"{type(tp).__name__}({const})" elif tp is NoneType: diff --git a/src/openapi_test_client/libraries/common/constants.py b/src/openapi_test_client/libraries/common/constants.py index 649a998..cbb2179 100644 --- a/src/openapi_test_client/libraries/common/constants.py +++ b/src/openapi_test_client/libraries/common/constants.py @@ -1,2 +1,3 @@ VALID_METHODS = ["get", "post", "put", "delete", "options", "head", "patch", "trace"] TAB = " " * 4 +BACKSLASH = "\\"