Skip to content

Commit

Permalink
Fix properly handle backslashes in Constraint.pattern
Browse files Browse the repository at this point in the history
  • Loading branch information
yugokato committed Sep 27, 2024
1 parent 3944baa commit 3fc4959
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 3 deletions.
6 changes: 4 additions & 2 deletions src/openapi_test_client/libraries/api/api_client_generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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__)

Expand Down Expand Up @@ -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:
Expand Down
1 change: 1 addition & 0 deletions src/openapi_test_client/libraries/common/constants.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
VALID_METHODS = ["get", "post", "put", "delete", "options", "head", "patch", "trace"]
TAB = " " * 4
BACKSLASH = "\\"

0 comments on commit 3fc4959

Please sign in to comment.