Skip to content

Commit ba5b539

Browse files
committed
refactor(types): rename transform_in_post to transform_in_body and improve docs
The `transform_in_post` attribute controls whether data transformation (via `get_for_api`) occurs for request bodies. Since this applies to PUT and PATCH requests as well as POST, `transform_in_body` is a more accurate name. This change also improves code documentation to clarify the data transformation flow between `types.py` and `utils.py`.
1 parent e132683 commit ba5b539

File tree

3 files changed

+26
-9
lines changed

3 files changed

+26
-9
lines changed

gitlab/types.py

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,13 @@ def validate_attrs(
3737

3838

3939
class GitlabAttribute:
40+
# Used in utils._transform_types() to decide if we should call get_for_api()
41+
# on the attribute when transform_data is False (e.g. for POST/PUT/PATCH).
42+
#
43+
# This allows us to force transformation of data even when sending JSON bodies,
44+
# which is useful for types like CommaSeparatedStringAttribute.
45+
transform_in_body = False
46+
4047
def __init__(self, value: Any = None) -> None:
4148
self._value = value
4249

@@ -91,9 +98,10 @@ def get_for_api(self, *, key: str) -> tuple[str, Any]:
9198

9299

93100
class CommaSeparatedListAttribute(_ListArrayAttribute):
94-
"""For values which are sent to the server as a Comma Separated Values
95-
(CSV) string. We allow them to be specified as a list and we convert it
96-
into a CSV"""
101+
"""
102+
For values which are sent to the server as a Comma Separated Values (CSV) string
103+
in query parameters (GET), but as a list/array in JSON bodies (POST/PUT).
104+
"""
97105

98106

99107
class CommaSeparatedStringAttribute(_ListArrayAttribute):
@@ -103,7 +111,10 @@ class CommaSeparatedStringAttribute(_ListArrayAttribute):
103111
to a string even in JSON bodies (POST/PUT requests).
104112
"""
105113

106-
transform_in_post = True
114+
# Used in utils._transform_types() to ensure the value is converted to a string
115+
# via get_for_api() even when transform_data is False (e.g. for POST/PUT/PATCH).
116+
# This is needed because some APIs require a CSV string instead of a JSON array.
117+
transform_in_body = True
107118

108119

109120
class LowercaseStringAttribute(GitlabAttribute):

gitlab/utils.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -198,9 +198,15 @@ def _transform_types(
198198
files[attr_name] = (key, data.pop(attr_name))
199199
continue
200200

201-
if not transform_data and not getattr(
202-
gitlab_attribute, "transform_in_post", False
203-
):
201+
# If transform_data is False, it means we are preparing data for a JSON body
202+
# (POST/PUT/PATCH). In this case, we normally skip transformation because
203+
# most types (like ArrayAttribute) only need transformation for query
204+
# parameters (GET).
205+
#
206+
# However, some types (like CommaSeparatedStringAttribute) need to be
207+
# transformed even in JSON bodies (e.g. converting a list to a CSV string).
208+
# The 'transform_in_body' flag on the attribute class controls this behavior.
209+
if not transform_data and not gitlab_attribute.transform_in_body:
204210
continue
205211

206212
if isinstance(gitlab_attribute, types.GitlabAttribute):

tests/unit/test_types.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -157,5 +157,5 @@ def test_comma_separated_string_attribute() -> None:
157157
attr.set_from_cli("")
158158
assert attr.get() == []
159159

160-
# Verify transform_in_post is True
161-
assert types.CommaSeparatedStringAttribute.transform_in_post is True
160+
# Verify transform_in_body is True
161+
assert types.CommaSeparatedStringAttribute.transform_in_body is True

0 commit comments

Comments
 (0)