Skip to content

Commit 7dbd3b6

Browse files
committed
fix the template mustache files to load embeded fields
1 parent f76d60d commit 7dbd3b6

3 files changed

Lines changed: 75 additions & 1 deletion

File tree

scripts/inject_python_sdk_helpers.sh

Lines changed: 71 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -346,13 +346,83 @@ import sys
346346
347347
path = pathlib.Path(sys.argv[1])
348348
text = path.read_text(encoding="utf-8")
349-
updated = re.sub(r"\bbytearray\b", "bytes", text)
349+
updated = text
350+
351+
# Fix 1: bytearray -> bytes (existing fix)
352+
updated = re.sub(r"\bbytearray\b", "bytes", updated)
353+
354+
# Fix 2: "if self.X:" -> "if self.X is not None:" in to_dict() methods.
355+
# Bare truthiness checks on OneOf wrapper fields call __len__ and raise TypeError.
356+
updated = re.sub(
357+
r'^(\s+if self\.[a-zA-Z_]+):$',
358+
r'\1 is not None:',
359+
updated,
360+
flags=re.MULTILINE,
361+
)
362+
363+
# Fix 3: "if _item_X:" -> "if _item_X is not None:" in to_dict() list loops.
364+
updated = re.sub(
365+
r'\bif (_item_[a-zA-Z_]+):',
366+
r'if \1 is not None:',
367+
updated,
368+
)
369+
370+
# Fix 4: Remove all fields from excluded_fields in to_dict().
371+
# The generator marks readOnly fields as excluded, which prevents them from
372+
# appearing in to_dict() output even when the server returns them in responses.
373+
updated = re.sub(
374+
r'excluded_fields: Set\[str\] = set\(\[[^\]]*\]\)',
375+
'excluded_fields: Set[str] = set([])',
376+
updated,
377+
flags=re.DOTALL,
378+
)
379+
350380
if updated != text:
351381
path.write_text(updated, encoding="utf-8")
352382
PYCODE
353383
done < <(find "${models_dir}" -type f -name "*.py" -print0)
354384
fi
355385

386+
# Fix 5: api_client.py getheaders() returns http.client.HTTPMessage during VCR
387+
# playback, which pydantic rejects as Dict. Convert to plain dict explicitly.
388+
api_client_file="${package_dir}/api_client.py"
389+
if [ -f "${api_client_file}" ]; then
390+
python3 - <<'PYCODE' "${api_client_file}"
391+
import pathlib
392+
import sys
393+
394+
path = pathlib.Path(sys.argv[1])
395+
text = path.read_text(encoding="utf-8")
396+
old = " headers = response_data.getheaders(),"
397+
new = (
398+
" raw_headers = response_data.getheaders(),\n"
399+
" headers = dict(raw_headers) if raw_headers is not None else None,"
400+
)
401+
# The old pattern has headers= inside ApiResponse(). Fix it properly:
402+
old2 = (
403+
" return ApiResponse(\n"
404+
" status_code = response_data.status,\n"
405+
" data = return_data,\n"
406+
" headers = response_data.getheaders(),\n"
407+
" raw_data = response_data.data\n"
408+
" )"
409+
)
410+
new2 = (
411+
" raw_headers = response_data.getheaders()\n"
412+
" headers = dict(raw_headers) if raw_headers is not None else None\n"
413+
" return ApiResponse(\n"
414+
" status_code = response_data.status,\n"
415+
" data = return_data,\n"
416+
" headers = headers,\n"
417+
" raw_data = response_data.data\n"
418+
" )"
419+
)
420+
updated = text.replace(old2, new2)
421+
if updated != text:
422+
path.write_text(updated, encoding="utf-8")
423+
PYCODE
424+
fi
425+
356426
readme_path="${sdk_dir}/README.md"
357427
if [ -f "${readme_path}" ]; then
358428
tmp_readme="$(mktemp)"

templates/python/model_anyof.mustache

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,8 @@ class {{classname}}({{#parent}}{{{.}}}{{/parent}}{{^parent}}BaseModel{{/parent}}
199199

200200
if hasattr(self.actual_instance, "to_dict") and callable(self.actual_instance.to_dict):
201201
return self.actual_instance.to_dict()
202+
elif isinstance(self.actual_instance, list):
203+
return [item.to_dict() if hasattr(item, "to_dict") else item for item in self.actual_instance]
202204
else:
203205
return self.actual_instance
204206

templates/python/model_oneof.mustache

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,8 @@ class {{classname}}({{#parent}}{{{.}}}{{/parent}}{{^parent}}BaseModel{{/parent}}
225225

226226
if hasattr(self.actual_instance, "to_dict") and callable(self.actual_instance.to_dict):
227227
return self.actual_instance.to_dict()
228+
elif isinstance(self.actual_instance, list):
229+
return [item.to_dict() if hasattr(item, "to_dict") else item for item in self.actual_instance]
228230
else:
229231
# primitive type
230232
return self.actual_instance

0 commit comments

Comments
 (0)