@@ -346,13 +346,83 @@ import sys
346346
347347path = pathlib.Path(sys.argv[1])
348348text = 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+
350380if updated != text:
351381 path.write_text(updated, encoding="utf-8")
352382PYCODE
353383 done < <( find " ${models_dir} " -type f -name " *.py" -print0)
354384fi
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+
356426readme_path=" ${sdk_dir} /README.md"
357427if [ -f " ${readme_path} " ]; then
358428 tmp_readme=" $( mktemp) "
0 commit comments