Skip to content

Commit d720b36

Browse files
authored
Merge pull request #11 from cloudbeds/chore/patch-complex-form-object
chore: patch complex form object with script
2 parents 923dda5 + 862d931 commit d720b36

File tree

3 files changed

+95
-1
lines changed

3 files changed

+95
-1
lines changed

.github/workflows/publish.yaml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,16 @@ jobs:
9898
find $(cat PACKAGE) -mindepth 1 ! -name 'py.typed' -delete
9999
java -jar openapi-generator-cli.jar generate -c openapitools.json
100100
101+
- name: Setup Python for post-generation fixes
102+
if: inputs.version == ''
103+
uses: actions/setup-python@v5
104+
with:
105+
python-version: '3.x'
106+
107+
- name: Fix filters serialization
108+
if: inputs.version == ''
109+
run: python fix_filters_serialization.py
110+
101111
- name: Git Setup
102112
if: inputs.version == ''
103113
run: |

cloudbeds_fiscal_document/api/fiscal_documents_api.py

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5301,7 +5301,24 @@ def _get_fiscal_documents_serialize(
53015301

53025302
if filters is not None:
53035303

5304-
_query_params.append(('filters', filters))
5304+
# Custom form serialization for filters
5305+
if hasattr(filters, 'to_dict'):
5306+
filters_dict = filters.to_dict()
5307+
for key, value in filters_dict.items():
5308+
if value is not None:
5309+
if isinstance(value, list):
5310+
for item in value:
5311+
if hasattr(item, 'value'): # Handle enums
5312+
_query_params.append((key, item.value))
5313+
else:
5314+
_query_params.append((key, item))
5315+
else:
5316+
if hasattr(value, 'value'): # Handle enums
5317+
_query_params.append((key, value.value))
5318+
else:
5319+
_query_params.append((key, value))
5320+
else:
5321+
_query_params.append(('filters', filters))
53055322

53065323
# process the header parameters
53075324
if x_property_id is not None:

fix_filters_serialization.py

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
"""
2+
Post-generation script to fix filters serialization in the OpenAPI-generated Python client.
3+
This script automatically patches the getFiscalDocuments method to properly serialize
4+
the filters parameter as form data instead of URL-encoded query parameter.
5+
"""
6+
7+
import re
8+
import sys
9+
from pathlib import Path
10+
11+
def fix_filters_serialization():
12+
"""Fix the filters serialization in the generated API client."""
13+
14+
api_file = Path("cloudbeds_fiscal_document/api/fiscal_documents_api.py")
15+
16+
if not api_file.exists():
17+
print(f"Error: {api_file} not found")
18+
return False
19+
20+
print(f"Patching filters serialization in {api_file}...")
21+
22+
# Read the current content
23+
content = api_file.read_text()
24+
25+
# Pattern to find the problematic filters serialization
26+
old_pattern = r'(\s+if filters is not None:\s+\n\s+)(_query_params\.append\(\(\'filters\', filters\)\))'
27+
28+
# New serialization logic
29+
new_serialization = r'''\1# Custom form serialization for filters
30+
if hasattr(filters, 'to_dict'):
31+
filters_dict = filters.to_dict()
32+
for key, value in filters_dict.items():
33+
if value is not None:
34+
if isinstance(value, list):
35+
for item in value:
36+
if hasattr(item, 'value'): # Handle enums
37+
_query_params.append((key, item.value))
38+
else:
39+
_query_params.append((key, item))
40+
else:
41+
if hasattr(value, 'value'): # Handle enums
42+
_query_params.append((key, value.value))
43+
else:
44+
_query_params.append((key, value))
45+
else:
46+
_query_params.append(('filters', filters))'''
47+
48+
# Apply the fix
49+
new_content = re.sub(old_pattern, new_serialization, content, flags=re.MULTILINE)
50+
51+
# Check if the replacement was successful
52+
if new_content == content:
53+
print("No filters serialization pattern found to replace")
54+
return True
55+
56+
# Write the updated content back
57+
api_file.write_text(new_content)
58+
print("Successfully patched filters serialization!")
59+
return True
60+
61+
def main():
62+
"""Main function."""
63+
success = fix_filters_serialization()
64+
sys.exit(0 if success else 1)
65+
66+
if __name__ == "__main__":
67+
main()

0 commit comments

Comments
 (0)