|
| 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