File size: 57,958 Bytes
998c988
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
import os
import requests
import yaml
import json
import re
import xml.dom.minidom
import xml.etree.ElementTree as ET
from pathlib import Path
from urllib.parse import urlparse
from typing import Dict, List, Any, Optional, Tuple, Set
from collections import defaultdict

class ApiSchemaGeneratorV5:
    """

    A class to dynamically process API specifications and generate appropriate output files

    like datasource_plugin_meta.json and default_schema.orx files.

    

    Version 5: Added protection against circular references in schema objects

    """
    
    def __init__(self, api_spec_url: str, api_name: str = None, selected_endpoints: List[str] = None):
        """

        Initialize the ApiSchemaGeneratorV5 with an API specification URL.

        

        Args:

            api_spec_url: URL or path to the API specification (OpenAPI/Swagger)

            api_name: Optional name for the API (will be extracted from spec if not provided)

            selected_endpoints: Optional list of endpoint paths to include in the schema

        """
        self.api_spec_url = api_spec_url
        self.api_name = api_name
        self.api_spec = None
        self.api_info = None
        self.auth_info = None
        self.endpoints = None
        self.schema_objects = None
        self.common_parameters = {}  # Store common parameters across endpoints
        self.selected_endpoints = selected_endpoints  # Store selected endpoints
        
    def fetch_api_spec(self) -> dict:
        """Fetch and parse the API specification"""
        try:
            if self.api_spec_url.startswith(('http://', 'https://')):
                response = requests.get(self.api_spec_url)
                response.raise_for_status()
                content = response.text
            else:
                with open(self.api_spec_url, 'r') as f:
                    content = f.read()
            
            # Determine if it's JSON or YAML
            try:
                spec = json.loads(content)
            except json.JSONDecodeError:
                spec = yaml.safe_load(content)
            
            self.api_spec = spec
            return spec
        except Exception as e:
            print(f"Error fetching API specification: {str(e)}")
            return {}
    
    def extract_api_info(self) -> dict:
        """Extract essential information from the API specification"""
        if not self.api_spec:
            self.fetch_api_spec()
        
        if not self.api_spec:
            return {}
        
        api_info = {
            'title': self.api_spec.get('info', {}).get('title', 'Unknown API'),
            'description': self.api_spec.get('info', {}).get('description', ''),
            'version': self.api_spec.get('info', {}).get('version', '1.0.0'),
            'endpoints': {},
            'auth': None,
            'schemas': {}
        }
        
        # Set API name if not provided
        if not self.api_name:
            self.api_name = self._sanitize_name(api_info['title'])
        
        # Extract authentication info if present
        if 'components' in self.api_spec and 'securitySchemes' in self.api_spec['components']:
            api_info['auth'] = self._extract_auth_info(self.api_spec)
        
        # Extract common parameters if present
        if 'components' in self.api_spec and 'parameters' in self.api_spec['components']:
            self.common_parameters = self.api_spec['components']['parameters']
        
        # Extract paths and their parameters
        if 'paths' in self.api_spec:
            for path, methods in self.api_spec['paths'].items():
                # Extract path-level parameters
                path_params = []
                if 'parameters' in methods:
                    path_params = methods['parameters']
                
                api_info['endpoints'][path] = {}
                for method, details in methods.items():
                    if method.lower() in ['get', 'post', 'put', 'delete', 'patch']:
                        # Combine path-level and method-level parameters
                        all_params = path_params.copy() if path_params else []
                        if 'parameters' in details:
                            all_params.extend(details['parameters'])
                        
                        # Process parameters
                        params = []
                        for param in all_params:
                            # Handle parameter references
                            if '$ref' in param:
                                ref = param['$ref']
                                param_name = ref.split('/')[-1]
                                if param_name in self.common_parameters:
                                    param = self.common_parameters[param_name]
                            
                            # Extract parameter schema
                            param_schema = param.get('schema', {})
                            param_type = None
                            
                            # Try to get the type from the schema
                            if param_schema:
                                param_type = param_schema.get('type')
                                
                                # If schema has a reference, resolve it
                                if '$ref' in param_schema:
                                    ref_schema = self._resolve_schema_reference(param_schema['$ref'])
                                    if ref_schema:
                                        param_type = ref_schema.get('type')
                            
                            # Default to string if no type is found
                            if not param_type:
                                param_type = 'string'
                            
                            params.append({
                                'name': param.get('name'),
                                'in': param.get('in'),
                                'required': param.get('required', False),
                                'type': param_type,
                                'enum': param_schema.get('enum'),
                                'description': param.get('description', '')
                            })
                        
                        # Extract requestBody if present
                        request_body_schema = None
                        if 'requestBody' in details:
                            content = details['requestBody'].get('content', {})
                            if 'application/json' in content:
                                request_body_schema = content['application/json'].get('schema')
                        
                        # Extract response schema
                        response_schema = None
                        if 'responses' in details:
                            for status_code, response in details['responses'].items():
                                if status_code.startswith('2'):  # 2xx responses
                                    if 'content' in response:
                                        for content_type, content_details in response['content'].items():
                                            if 'schema' in content_details:
                                                response_schema = content_details['schema']
                                                break
                                    break
                        
                        api_info['endpoints'][path][method] = {
                            'summary': details.get('summary', ''),
                            'description': details.get('description', ''),
                            'operationId': details.get('operationId', ''),
                            'parameters': params,
                            'requestBody': request_body_schema,
                            'responseSchema': response_schema,
                            'security': details.get('security')
                        }
        
        # Extract schema objects
        if 'components' in self.api_spec and 'schemas' in self.api_spec['components']:
            api_info['schemas'] = self.api_spec['components']['schemas']
        
        self.api_info = api_info
        self.auth_info = api_info['auth']
        self.endpoints = api_info['endpoints']
        self.schema_objects = api_info['schemas']
        
        return api_info
    
    def _extract_auth_info(self, spec: dict) -> dict:
        """Extract detailed authentication information from API spec"""
        auth_info = {}
        
        if 'securitySchemes' in spec.get('components', {}):
            for scheme_name, scheme_details in spec['components']['securitySchemes'].items():
                auth_type = scheme_details.get('type')
                
                # Extract common info
                auth_info[scheme_name] = {
                    'type': auth_type,
                    'description': scheme_details.get('description', '')
                }
                
                # Extract type-specific info
                if auth_type == 'apiKey':
                    auth_info[scheme_name].update({
                        'name': scheme_details.get('name'),
                        'in': scheme_details.get('in')  # header, query, or cookie
                    })
                elif auth_type == 'http':
                    auth_info[scheme_name].update({
                        'scheme': scheme_details.get('scheme')  # basic, bearer, etc.
                    })
                elif auth_type == 'oauth2':
                    flows = scheme_details.get('flows', {})
                    auth_info[scheme_name]['flows'] = {}
                    
                    for flow_type, flow_details in flows.items():
                        auth_info[scheme_name]['flows'][flow_type] = {
                            'authorizationUrl': flow_details.get('authorizationUrl'),
                            'tokenUrl': flow_details.get('tokenUrl'),
                            'refreshUrl': flow_details.get('refreshUrl'),
                            'scopes': flow_details.get('scopes', {})
                        }
        
        return auth_info
    
    def _sanitize_name(self, name: str) -> str:
        """Sanitize a name to be used as an identifier"""
        if not name:
            return "api"
        
        # Remove special characters and replace spaces with underscores
        sanitized = re.sub(r'[^a-zA-Z0-9_\s]', '', name)
        sanitized = re.sub(r'\s+', '_', sanitized).lower()
        
        return sanitized
    
    def _extract_server_url(self) -> str:
        """Extract server URL from API specification"""
        if not self.api_spec:
            self.fetch_api_spec()
        
        # Extract server information
        servers = self.api_spec.get('servers', [])
        if servers and 'url' in servers[0]:
            return servers[0]['url']
        
        # Default URL if no servers defined
        return "https://api.example.com"
    
    def _extract_maxretries(self) -> str:
        """Extract maximum retries from API specification"""
        if not self.api_spec:
            self.fetch_api_spec()
        
        # Look for rate limiting information in the API spec
        # Check in extensions
        if 'x-ratelimit-retries' in self.api_spec:
            return str(self.api_spec['x-ratelimit-retries'])
        
        # Check in info section
        if 'info' in self.api_spec and 'x-ratelimit-retries' in self.api_spec['info']:
            return str(self.api_spec['info']['x-ratelimit-retries'])
        
        # Check in components section
        if 'components' in self.api_spec and 'x-ratelimit-retries' in self.api_spec['components']:
            return str(self.api_spec['components']['x-ratelimit-retries'])
        
        # Default value if not found in API spec
        return "3"
    
    def _extract_timeout(self) -> str:
        """Extract timeout from API specification"""
        if not self.api_spec:
            self.fetch_api_spec()
        
        # Look for timeout information in the API spec
        # Check in extensions
        if 'x-timeout' in self.api_spec:
            return str(self.api_spec['x-timeout'])
        
        # Check in info section
        if 'info' in self.api_spec and 'x-timeout' in self.api_spec['info']:
            return str(self.api_spec['info']['x-timeout'])
        
        # Check in components section
        if 'components' in self.api_spec and 'x-timeout' in self.api_spec['components']:
            return str(self.api_spec['components']['x-timeout'])
        
        # Default value if not found in API spec
        return "60"
    
    def _get_auth_meta_fields(self) -> List[Dict[str, Any]]:
        """Generate meta fields for authentication based on the API spec"""
        meta_fields = []
        added_fields = set()  # Track added field names to avoid duplication
        
        # Use the custom API name for all field descriptions
        api_name_title = self.api_name.title()
        
        if not self.auth_info:
            # Default to basic auth if no auth info is found
            meta_fields.extend([
                {
                "name": "url",
                "description": f"{api_name_title} URL",
                "sectionName": "Connection Info",
                "defaultValue": self._extract_server_url(),
                "dataType": "STRING",
                "isRequired": True,
                "regex": ""
                },
                {
                    "name": "username",
                    "description": f"{api_name_title} Username",
                    "sectionName": "Connection Info",
                    "defaultValue": "[username]",
                    "dataType": "STRING",
                    "isRequired": True,
                    "regex": ""
                },
                {
                    "name": "password",
                    "description": f"{api_name_title} Password",
                    "sectionName": "Connection Info",
                    "defaultValue": "[password]",
                    "dataType": "STRING",
                    "isRequired": True,
                    "regex": ""
                }
            ])
            added_fields.update(["url", "username", "password"])
        else:
            # Add URL field
            meta_fields.append({
                "name": "url",
                "description": f"{api_name_title} URL",
                "sectionName": "Connection Info",
                "defaultValue": self._extract_server_url(),
                "dataType": "STRING",
                "isRequired": True,
                "regex": ""
            })
            added_fields.add("url")
            
            # Process each auth scheme
            for scheme_name, scheme_details in self.auth_info.items():
                auth_type = scheme_details.get('type')
                
                if auth_type == 'apiKey' and "apitoken" not in added_fields:
                    meta_fields.append({
                        "name": "apitoken",
                        "description": f"{api_name_title} API Token",
                        "sectionName": "Connection Info",
                        "defaultValue": "[your_api_token]",
                        "dataType": "STRING",
                        "isRequired": True,
                        "regex": ""
                    })
                    added_fields.add("apitoken")
                elif auth_type == 'http':
                    scheme = scheme_details.get('scheme', '').lower()
                    if scheme == 'basic':
                        if "username" not in added_fields:
                            meta_fields.append({
                                "name": "username",
                                "description": f"{api_name_title} Username",
                                "sectionName": "Connection Info",
                                "defaultValue": "[username]",
                                "dataType": "STRING",
                                "isRequired": True,
                                "regex": ""
                            })
                            added_fields.add("username")
                        
                        if "password" not in added_fields:
                            meta_fields.append({
                                "name": "password",
                                "description": f"{api_name_title} Password",
                                "sectionName": "Connection Info",
                                "defaultValue": "[password]",
                                "dataType": "STRING",
                                "isRequired": True,
                                "regex": ""
                            })
                            added_fields.add("password")
                    elif scheme == 'bearer' and "token" not in added_fields:
                        meta_fields.append({
                            "name": "token",
                            "description": f"{api_name_title} Bearer Token",
                            "sectionName": "Connection Info",
                            "defaultValue": "[your_bearer_token]",
                            "dataType": "STRING",
                            "isRequired": True,
                            "regex": ""
                        })
                        added_fields.add("token")
                elif auth_type == 'oauth2':
                    flows = scheme_details.get('flows', {})
                    
                    # Check for client credentials flow
                    if 'clientCredentials' in flows:
                        if "clientId" not in added_fields:
                            meta_fields.append({
                                "name": "clientId",
                                "description": f"{api_name_title} Client ID",
                                "sectionName": "Connection Info",
                                "defaultValue": "[your_client_id]",
                                "dataType": "STRING",
                                "isRequired": True,
                                "regex": ""
                            })
                            added_fields.add("clientId")
                        
                        if "clientSecret" not in added_fields:
                            meta_fields.append({
                                "name": "clientSecret",
                                "description": f"{api_name_title} Client Secret",
                                "sectionName": "Connection Info",
                                "defaultValue": "[your_client_secret]",
                                "dataType": "STRING",
                                "isRequired": True,
                                "regex": ""
                            })
                            added_fields.add("clientSecret")
                        
                        if "tokenUrl" not in added_fields:
                            meta_fields.append({
                                "name": "tokenUrl",
                                "description": f"{api_name_title} Token URL",
                                "sectionName": "Connection Info",
                                "defaultValue": flows['clientCredentials'].get('tokenUrl', ''),
                                "dataType": "STRING",
                                "isRequired": True,
                                "regex": ""
                            })
                            added_fields.add("tokenUrl")
                    # Check for password flow
                    elif 'password' in flows:
                        if "username" not in added_fields:
                            meta_fields.append({
                                "name": "username",
                                "description": f"{api_name_title} Username",
                                "sectionName": "Connection Info",
                                "defaultValue": "[username]",
                                "dataType": "STRING",
                                "isRequired": True,
                                "regex": ""
                            })
                            added_fields.add("username")
                        
                        if "password" not in added_fields:
                            meta_fields.append({
                                "name": "password",
                                "description": f"{api_name_title} Password",
                                "sectionName": "Connection Info",
                                "defaultValue": "[password]",
                                "dataType": "STRING",
                                "isRequired": True,
                                "regex": ""
                            })
                            added_fields.add("password")
                        
                        if "clientId" not in added_fields:
                            meta_fields.append({
                                "name": "clientId",
                                "description": f"{api_name_title} Client ID",
                                "sectionName": "Connection Info",
                                "defaultValue": "[your_client_id]",
                                "dataType": "STRING",
                                "isRequired": True,
                                "regex": ""
                            })
                            added_fields.add("clientId")
                        
                        if "clientSecret" not in added_fields:
                            meta_fields.append({
                                "name": "clientSecret",
                                "description": f"{api_name_title} Client Secret",
                                "sectionName": "Connection Info",
                                "defaultValue": "[your_client_secret]",
                                "dataType": "STRING",
                                "isRequired": True,
                                "regex": ""
                            })
                            added_fields.add("clientSecret")
                        
                        if "tokenUrl" not in added_fields:
                            meta_fields.append({
                                "name": "tokenUrl",
                                "description": f"{api_name_title} Token URL",
                                "sectionName": "Connection Info",
                                "defaultValue": flows['password'].get('tokenUrl', ''),
                                "dataType": "STRING",
                                "isRequired": True,
                                "regex": ""
                            })
                            added_fields.add("tokenUrl")
                    # Check for authorization code flow
                    elif 'authorizationCode' in flows:
                        if "clientId" not in added_fields:
                            meta_fields.append({
                                "name": "clientId",
                                "description": f"{api_name_title} Client ID",
                                "sectionName": "Connection Info",
                                "defaultValue": "[your_client_id]",
                                "dataType": "STRING",
                                "isRequired": True,
                                "regex": ""
                            })
                            added_fields.add("clientId")
                        
                        if "clientSecret" not in added_fields:
                            meta_fields.append({
                                "name": "clientSecret",
                                "description": f"{api_name_title} Client Secret",
                                "sectionName": "Connection Info",
                                "defaultValue": "[your_client_secret]",
                                "dataType": "STRING",
                                "isRequired": True,
                                "regex": ""
                            })
                            added_fields.add("clientSecret")
                        
                        if "authorizationUrl" not in added_fields:
                            meta_fields.append({
                                "name": "authorizationUrl",
                                "description": f"{api_name_title} Authorization URL",
                                "sectionName": "Connection Info",
                                "defaultValue": flows['authorizationCode'].get('authorizationUrl', ''),
                                "dataType": "STRING",
                                "isRequired": True,
                                "regex": ""
                            })
                            added_fields.add("authorizationUrl")
                        
                        if "tokenUrl" not in added_fields:
                            meta_fields.append({
                                "name": "tokenUrl",
                                "description": f"{api_name_title} Token URL",
                                "sectionName": "Connection Info",
                                "defaultValue": flows['authorizationCode'].get('tokenUrl', ''),
                                "dataType": "STRING",
                                "isRequired": True,
                                "regex": ""
                            })
                            added_fields.add("tokenUrl")
        
        # Add request parameters from API spec
        if "maxretries" not in added_fields:
            maxretries_value = self._extract_maxretries()
            meta_fields.append({
                "name": "maxretries",
                "description": "Maximum Request Retries",
                "sectionName": "Request Metering",
                "defaultValue": maxretries_value,
                "dataType": "NUMBER",
                "isRequired": True
            })
        
        if "timeout" not in added_fields:
            timeout_value = self._extract_timeout()
            meta_fields.append({
                "name": "timeout",
                "description": "Request Timeout (seconds)",
                "sectionName": "Request Metering",
                "defaultValue": timeout_value,
                "dataType": "NUMBER",
                "isRequired": True
            })
        
        return meta_fields
    
    def generate_datasource_plugin_meta(self) -> dict:
        """Generate the datasource plugin meta JSON file"""
        if not self.api_info:
            self.extract_api_info()
        
        # Handle case where API info couldn't be extracted
        if not self.api_info:
            self.api_info = {
                'title': 'Unknown API',
                'description': '',
                'version': '1.0.0',
                'endpoints': {},
                'auth': None,
                'schemas': {}
            }
        
        # Use the custom API name for all references in the metadata
        api_name_title = self.api_name.title()
        api_name_lower = self.api_name.lower()
        
        meta_data = {
            "name": api_name_title,
            "description": self.api_info.get('description') or f"{api_name_title} Directory",
            "backendCategory": "custom",
            "userCreated": False,
            "icon": f"/{api_name_lower}.svg",
            "isSchemaExtractable": False,
            "meta": self._get_auth_meta_fields()
        }
        
        return meta_data
    
    def _resolve_schema_reference(self, ref: str) -> dict:
        """Resolve a schema reference to the actual schema definition"""
        if not ref.startswith('#/components/schemas/'):
            return {}
        
        schema_name = ref.split('/')[-1]
        if self.schema_objects and schema_name in self.schema_objects:
            return self.schema_objects[schema_name]
        
        return {}
    
    def _extract_properties_from_schema(self, schema: dict) -> dict:
        """Extract properties from a schema, handling references and composition"""
        if not schema or not isinstance(schema, dict):
            return {}
        
        # Handle direct reference
        if '$ref' in schema:
            return self._extract_properties_from_schema(self._resolve_schema_reference(schema['$ref']))
        
        # Get direct properties
        properties = schema.get('properties', {})
        
        # Handle allOf composition
        if 'allOf' in schema:
            for sub_schema in schema['allOf']:
                sub_properties = self._extract_properties_from_schema(sub_schema)
                properties.update(sub_properties)
        
        # Handle oneOf composition
        if 'oneOf' in schema:
            # For oneOf, we'll take properties from the first schema as a representative
            if schema['oneOf'] and isinstance(schema['oneOf'][0], dict):
                first_schema = schema['oneOf'][0]
                sub_properties = self._extract_properties_from_schema(first_schema)
                properties.update(sub_properties)
        
        # Handle anyOf composition
        if 'anyOf' in schema:
            # For anyOf, we'll take properties from all schemas
            for sub_schema in schema['anyOf']:
                sub_properties = self._extract_properties_from_schema(sub_schema)
                properties.update(sub_properties)
        
        return properties
    
    def _extract_common_parameters(self) -> Dict[str, Dict[str, Any]]:
        """Extract common parameters from the API spec"""
        # Return empty dict as we're not using common parameters anymore
        return {}
    
    def _extract_data_models(self) -> List[Dict[str, Any]]:
        """Extract data models from schema objects that are relevant to selected endpoints"""
        data_models = []
        processed_schemas = set()
        relevant_schemas = set()
        
        if not self.schema_objects:
            return data_models
        
        # First, identify which schemas are referenced by the selected endpoints
        if self.selected_endpoints:
            # Extract endpoints based on selected paths and methods
            selected_paths = {}
            for path, method in self.selected_endpoints:
                if path not in selected_paths:
                    selected_paths[path] = []
                selected_paths[path].append(method.lower())
            
            # Process endpoints to find referenced schemas
            for path, methods in self.endpoints.items():
                if path not in selected_paths:
                    continue
                    
                for method, details in methods.items():
                    if method.lower() not in selected_paths.get(path, []):
                        continue
                    
                    # Check response schema
                    response_schema = details.get('responseSchema')
                    if response_schema:
                        self._collect_referenced_schemas(response_schema, relevant_schemas)
                    
                    # Check request body schema
                    request_body = details.get('requestBody')
                    if request_body:
                        self._collect_referenced_schemas(request_body, relevant_schemas)
                    
                    # Check parameter schemas
                    for param in details.get('parameters', []):
                        param_schema = param.get('schema', {})
                        if param_schema:
                            self._collect_referenced_schemas(param_schema, relevant_schemas)
        else:
            # If no endpoints are selected, include all schemas
            relevant_schemas = set(self.schema_objects.keys())
        
        # Process only the relevant schema objects
        for schema_name in relevant_schemas:
            if schema_name in processed_schemas:
                continue
                
            schema_def = self.schema_objects.get(schema_name)
            if not schema_def:
                continue
            
            # Skip non-object schemas
            if schema_def.get('type') and schema_def.get('type') != 'object':
                continue
            
            table = self._create_table_from_schema(schema_name, schema_def)
            if table:
                # Add source information to the table
                table["Source"] = "Components/Schemas"
                table["Type"] = "DATA_MODEL"
                data_models.append(table)
                processed_schemas.add(schema_name)
        
        return data_models
    
    def _collect_referenced_schemas(self, schema: dict, referenced_schemas: set, visited_refs: set = None) -> None:
        """

        Recursively collect all schema references from a schema object.

        

        Args:

            schema: The schema object to process

            referenced_schemas: Set to collect referenced schema names

            visited_refs: Set to track already visited references to prevent circular reference issues

        """
        if not schema or not isinstance(schema, dict):
            return
        
        # Initialize visited_refs if not provided
        if visited_refs is None:
            visited_refs = set()
        
        # Handle direct reference
        if '$ref' in schema:
            ref = schema['$ref']
            if ref.startswith('#/components/schemas/'):
                schema_name = ref.split('/')[-1]
                
                # Skip if we've already processed this reference to avoid circular references
                if ref in visited_refs:
                    return
                
                # Add to referenced schemas and mark as visited
                referenced_schemas.add(schema_name)
                visited_refs.add(ref)
                
                # Recursively collect references from the referenced schema
                if self.schema_objects and schema_name in self.schema_objects:
                    self._collect_referenced_schemas(self.schema_objects[schema_name], referenced_schemas, visited_refs)
        
        # Handle array items
        if schema.get('type') == 'array' and 'items' in schema:
            self._collect_referenced_schemas(schema['items'], referenced_schemas, visited_refs)
        
        # Handle object properties
        if schema.get('type') == 'object' and 'properties' in schema:
            for prop_name, prop_def in schema['properties'].items():
                self._collect_referenced_schemas(prop_def, referenced_schemas, visited_refs)
        
        # Handle composition schemas
        for comp_key in ['allOf', 'oneOf', 'anyOf']:
            if comp_key in schema:
                for sub_schema in schema[comp_key]:
                    self._collect_referenced_schemas(sub_schema, referenced_schemas, visited_refs)
    
    def _extract_endpoints(self) -> List[Dict[str, Any]]:
        """Extract endpoint information"""
        endpoints = []
        
        # Extract common parameters
        common_params = self._extract_common_parameters()

        selected_paths = {}
        if self.selected_endpoints:
            for path, method in self.selected_endpoints:
                if path not in selected_paths:
                    selected_paths[path] = []
                selected_paths[path].append(method.lower())
        
        # Process endpoints
        for path, methods in self.endpoints.items():
            # Skip endpoints that are not in the selected_endpoints list if it's provided
            if self.selected_endpoints is not None and path not in selected_paths:
                continue
            for method, details in methods.items():
                # Skip methods that are not in the selected methods for this path
                if self.selected_endpoints is not None and method.lower() not in selected_paths.get(path, []):
                    continue
                if method.lower() in ['get', 'post', 'put', 'delete', 'patch']:
                    endpoint = {
                        "ID": self._sanitize_name(f"{method}_{path}"),
                        "Name": details.get('operationId') or f"{method.upper()} {path}",
                        "Path": path,
                        "Method": method.upper(),
                        "Summary": details.get('summary', ''),
                        "Description": details.get('description', ''),
                        "Parameters": [],
                        "Type": "ENDPOINT"
                    }
                    
                    # Process parameters
                    for param in details.get('parameters', []):
                        param_key = f"{param.get('name')}:{param.get('in')}"
                        
                        # Extract parameter schema and type
                        param_schema = param.get('schema', {})
                        param_type = None
                        
                        # Try to get the type from the schema
                        if param_schema:
                            param_type = param_schema.get('type')
                            
                            # If schema has a reference, resolve it
                            if '$ref' in param_schema:
                                ref_schema = self._resolve_schema_reference(param_schema['$ref'])
                                if ref_schema:
                                    param_type = ref_schema.get('type')
                        
                        # Default to string if no type is found
                        if not param_type:
                            param_type = 'string'
                        
                        # Check if this is a common parameter
                        if param_key in common_params:
                            endpoint["Parameters"].append({
                                "Name": param.get('name'),
                                "In": param.get('in'),
                                "Required": param.get('required', False),
                                "Type": param_type,  # Include type even for common parameters
                                "IsCommon": True,
                                "CommonRef": param_key
                            })
                        else:
                            endpoint["Parameters"].append({
                                "Name": param.get('name'),
                                "In": param.get('in'),
                                "Required": param.get('required', False),
                                "Type": param_type,
                                "Enum": param_schema.get('enum'),
                                "Description": param.get('description', ''),
                                "IsCommon": False
                            })
                    
                    # Process response schema
                    response_schema = details.get('responseSchema')
                    if response_schema:
                        if '$ref' in response_schema:
                            ref = response_schema['$ref']
                            schema_name = ref.split('/')[-1]
                            endpoint["ResponseModel"] = schema_name
                            endpoint["ResponseType"] = "object"
                        elif response_schema.get('type') == 'array' and 'items' in response_schema:
                            items = response_schema['items']
                            if '$ref' in items:
                                ref = items['$ref']
                                schema_name = ref.split('/')[-1]
                                endpoint["ResponseModel"] = schema_name
                                endpoint["ResponseType"] = "array"
                            else:
                                # Inline schema
                                endpoint["ResponseType"] = "array"
                                endpoint["ResponseInlineSchema"] = items
                        else:
                            # Inline schema
                            endpoint["ResponseType"] = response_schema.get('type', 'object')
                            endpoint["ResponseInlineSchema"] = response_schema
                    
                    # Process request body
                    request_body = details.get('requestBody')
                    if request_body:
                        if '$ref' in request_body:
                            ref = request_body['$ref']
                            schema_name = ref.split('/')[-1]
                            endpoint["RequestModel"] = schema_name
                        else:
                            # Inline schema
                            endpoint["RequestInlineSchema"] = request_body
                    
                    endpoints.append(endpoint)
        
        return endpoints
    
    def _create_table_from_schema(self, schema_name: str, schema_def: dict) -> Optional[Dict[str, Any]]:
        """Create a table definition from a schema object"""
        if not schema_def or not isinstance(schema_def, dict):
            return None
        
        # Extract properties, handling references and composition
        properties = self._extract_properties_from_schema(schema_def)
        
        if not properties:
            return None
        
        # Get required properties
        required = schema_def.get('required', [])
        
        # Create table
        table = {
            "ID": self._sanitize_name(schema_name),
            "Name": schema_name,
            "Description": schema_def.get('description', ''),
            "Properties": []
        }
        
        # Add properties
        for prop_name, prop_def in properties.items():
            property_info = {
                "Name": prop_name,
                "Required": prop_name in required,
                "Description": prop_def.get('description', '') if isinstance(prop_def, dict) else ''
            }
            
            # Determine property type
            if isinstance(prop_def, dict):
                prop_type = prop_def.get('type')
                if prop_type:
                    property_info["Type"] = prop_type
                    
                    # Handle enum values
                    if 'enum' in prop_def:
                        property_info["Enum"] = prop_def['enum']
                    
                    # Handle array items
                    if prop_type == 'array' and 'items' in prop_def:
                        items = prop_def['items']
                        if '$ref' in items:
                            ref = items['$ref']
                            schema_name = ref.split('/')[-1]
                            property_info["ItemsType"] = "reference"
                            property_info["ItemsRef"] = schema_name
                        else:
                            property_info["ItemsType"] = items.get('type', 'object')
                    
                    # Handle object properties
                    if prop_type == 'object' and 'properties' in prop_def:
                        property_info["ObjectProperties"] = []
                        for sub_prop_name, sub_prop_def in prop_def['properties'].items():
                            sub_prop_info = {
                                "Name": sub_prop_name,
                                "Type": sub_prop_def.get('type', 'string'),
                                "Description": sub_prop_def.get('description', '')
                            }
                            property_info["ObjectProperties"].append(sub_prop_info)
                
                # Handle references
                if '$ref' in prop_def:
                    ref = prop_def['$ref']
                    schema_name = ref.split('/')[-1]
                    property_info["Type"] = "reference"
                    property_info["Ref"] = schema_name
            
            table["Properties"].append(property_info)
        
        return table
    
    def _generate_datasource_value(self) -> str:
        """Generate a DataSource value based on API spec information"""
        # Extract server information
        servers = self.api_spec.get('servers', [])
        server_url = "https://api.example.com"
        if servers and 'url' in servers[0]:
            server_url = servers[0]['url']
        
        # Extract authentication information
        auth_info = {}
        if self.auth_info:
            for scheme_name, scheme_details in self.auth_info.items():
                auth_type = scheme_details.get('type')
                
                if auth_type == 'apiKey':
                    auth_info['type'] = 'apiKey'
                    auth_info['name'] = scheme_details.get('name', '')
                    auth_info['in'] = scheme_details.get('in', 'header')
                elif auth_type == 'http':
                    scheme = scheme_details.get('scheme', '').lower()
                    auth_info['type'] = 'http'
                    auth_info['scheme'] = scheme
                elif auth_type == 'oauth2':
                    auth_info['type'] = 'oauth2'
                    flows = scheme_details.get('flows', {})
                    if 'clientCredentials' in flows:
                        auth_info['flow'] = 'clientCredentials'
                        auth_info['tokenUrl'] = flows['clientCredentials'].get('tokenUrl', '')
                    elif 'password' in flows:
                        auth_info['flow'] = 'password'
                        auth_info['tokenUrl'] = flows['password'].get('tokenUrl', '')
                    elif 'authorizationCode' in flows:
                        auth_info['flow'] = 'authorizationCode'
                        auth_info['authorizationUrl'] = flows['authorizationCode'].get('authorizationUrl', '')
                        auth_info['tokenUrl'] = flows['authorizationCode'].get('tokenUrl', '')
        
        # Create a connection string based on the extracted information
        connection_parts = []
        connection_parts.append(f"url={server_url}")
        
        if auth_info:
            connection_parts.append(f"auth_type={auth_info.get('type', 'none')}")
            
            if auth_info.get('type') == 'apiKey':
                connection_parts.append(f"api_key_name={auth_info.get('name', '')}")
                connection_parts.append(f"api_key_in={auth_info.get('in', '')}")
            elif auth_info.get('type') == 'http':
                connection_parts.append(f"http_scheme={auth_info.get('scheme', '')}")
            elif auth_info.get('type') == 'oauth2':
                connection_parts.append(f"oauth2_flow={auth_info.get('flow', '')}")
                if 'tokenUrl' in auth_info:
                    connection_parts.append(f"token_url={auth_info.get('tokenUrl', '')}")
                if 'authorizationUrl' in auth_info:
                    connection_parts.append(f"authorization_url={auth_info.get('authorizationUrl', '')}")
        
        # Add API name and version
        if self.api_info:
            connection_parts.append(f"api_name={self.api_name}")
            connection_parts.append(f"api_version={self.api_info.get('version', '1.0.0')}")
        
        # Create the connection string
        connection_string = ";".join(connection_parts)
        
        # Simulate AES encryption with a prefix
        # In a real implementation, this would be properly encrypted
        return f"{{AES}}{connection_string}"
    
    def generate_default_schema(self) -> str:
        """Generate the default schema XML file"""
        if not self.api_info:
            self.extract_api_info()
        
        # Create XML structure
        root = ET.Element("Xml")
        org = ET.SubElement(root, "ORG", Name=self.api_name.lower())
        
        ET.SubElement(org, "AccessMethod").text = "PDM"
        
        pdm = ET.SubElement(org, "PDM")
        ET.SubElement(pdm, "Name").text = self.api_name.lower()
        
        # Generate DataSource value from API spec information
        datasource_value = self._generate_datasource_value()
        ET.SubElement(pdm, "DataSource").text = datasource_value
        ET.SubElement(pdm, "DataSourceType").text = "XML"
        
        # Add tables
        tables = ET.SubElement(pdm, "Tables")
        
        # We no longer include common parameters section
        
        # Extract data models
        data_models = self._extract_data_models()
        
        # Add a section for data models
        if data_models:
            tables.append(ET.Comment(" DATA MODELS "))
            for model in data_models:
                model_class = ET.SubElement(tables, "Class", type=model["ID"])
                ET.SubElement(model_class, "Name").text = model["Name"]
                ET.SubElement(model_class, "Type").text = "DATA_MODEL"
                ET.SubElement(model_class, "Description").text = model.get("Description", "")
                
                properties = ET.SubElement(model_class, "Properties")
                for prop in model.get("Properties", []):
                    prop_elem = ET.SubElement(properties, "Property", ID=self._sanitize_name(prop["Name"]))
                    ET.SubElement(prop_elem, "Name").text = prop["Name"]
                    ET.SubElement(prop_elem, "Type").text = prop.get("Type", "string")
                    ET.SubElement(prop_elem, "Required").text = str(prop.get("Required", False)).lower()
                    ET.SubElement(prop_elem, "Description").text = prop.get("Description", "")
                    
                    # Handle references
                    if "Ref" in prop:
                        ET.SubElement(prop_elem, "ClassType").text = prop["Ref"]
                    
                    # Handle arrays
                    if prop.get("Type") == "array" and "ItemsType" in prop:
                        items = ET.SubElement(prop_elem, "Items")
                        ET.SubElement(items, "Type").text = prop["ItemsType"]
                        if "ItemsRef" in prop:
                            ET.SubElement(items, "ClassType").text = prop["ItemsRef"]
                    
                    # Handle object properties
                    if prop.get("Type") == "object" and "ObjectProperties" in prop:
                        obj_props = ET.SubElement(prop_elem, "ObjectProperties")
                        for obj_prop in prop["ObjectProperties"]:
                            obj_prop_elem = ET.SubElement(obj_props, "Property", ID=self._sanitize_name(obj_prop["Name"]))
                            ET.SubElement(obj_prop_elem, "Name").text = obj_prop["Name"]
                            ET.SubElement(obj_prop_elem, "Type").text = obj_prop.get("Type", "string")
                            ET.SubElement(obj_prop_elem, "Description").text = obj_prop.get("Description", "")
        
        # Extract endpoints
        endpoints_list = self._extract_endpoints()
        
        # Add a section for endpoints
        if endpoints_list:
            tables.append(ET.Comment(" API ENDPOINTS "))
            for endpoint in endpoints_list:
                endpoint_elem = ET.SubElement(tables, "Endpoint", ID=endpoint["ID"])
                ET.SubElement(endpoint_elem, "Name").text = endpoint["Name"]
                ET.SubElement(endpoint_elem, "Type").text = "ENDPOINT"
                ET.SubElement(endpoint_elem, "Path").text = endpoint["Path"]
                ET.SubElement(endpoint_elem, "Method").text = endpoint["Method"]
                ET.SubElement(endpoint_elem, "Summary").text = endpoint.get("Summary", "")
                ET.SubElement(endpoint_elem, "Description").text = endpoint.get("Description", "")
                
                # Add parameters
                if endpoint.get("Parameters"):
                    params = ET.SubElement(endpoint_elem, "Parameters")
                    for param in endpoint["Parameters"]:
                        param_elem = ET.SubElement(params, "Parameter", ID=self._sanitize_name(param["Name"]))
                        ET.SubElement(param_elem, "Name").text = param["Name"]
                        ET.SubElement(param_elem, "In").text = param["In"]
                        ET.SubElement(param_elem, "Required").text = str(param.get("Required", False)).lower()
                        
                        # Always include Type for all parameters
                        ET.SubElement(param_elem, "Type").text = param.get("Type", "string")
                        
                        # Handle common parameters
                        if param.get("IsCommon", False):
                            ET.SubElement(param_elem, "IsCommon").text = "true"
                            ET.SubElement(param_elem, "CommonRef").text = param["CommonRef"]
                        else:
                            ET.SubElement(param_elem, "IsCommon").text = "false"
                            ET.SubElement(param_elem, "Description").text = param.get("Description", "")
                
                # Add response information
                if "ResponseModel" in endpoint:
                    response = ET.SubElement(endpoint_elem, "Response")
                    ET.SubElement(response, "Type").text = endpoint.get("ResponseType", "object")
                    ET.SubElement(response, "ClassType").text = endpoint["ResponseModel"]
                elif "ResponseInlineSchema" in endpoint:
                    response = ET.SubElement(endpoint_elem, "Response")
                    ET.SubElement(response, "Type").text = endpoint.get("ResponseType", "object")
                    ET.SubElement(response, "InlineSchema").text = json.dumps(endpoint["ResponseInlineSchema"])
                
                # Add request body information
                if "RequestModel" in endpoint:
                    request = ET.SubElement(endpoint_elem, "Request")
                    ET.SubElement(request, "ClassType").text = endpoint["RequestModel"]
                elif "RequestInlineSchema" in endpoint:
                    request = ET.SubElement(endpoint_elem, "Request")
                    ET.SubElement(request, "InlineSchema").text = json.dumps(endpoint["RequestInlineSchema"])
        
        # Convert to pretty XML string
        rough_string = ET.tostring(root, 'utf-8')
        reparsed = xml.dom.minidom.parseString(rough_string)
        return reparsed.toprettyxml(indent="    ")
    
    def save_datasource_plugin_meta(self, output_path: str) -> None:
        """Save the datasource plugin meta JSON file"""
        meta_data = self.generate_datasource_plugin_meta()
        
        with open(output_path, 'w') as f:
            json.dump(meta_data, f, indent=2)
        
        print(f"Datasource plugin meta saved to: {output_path}")
    
    def save_default_schema(self, output_path: str) -> None:
        """Save the default schema XML file"""
        schema_xml = self.generate_default_schema()
        
        with open(output_path, 'w', encoding='utf-8') as f:
            f.write(schema_xml)
        
        print(f"Default schema saved to: {output_path}")
    
    def generate_files(self, output_dir: str = None) -> Tuple[str, str]:
        """Generate both the datasource plugin meta JSON and default schema XML files.

        

        Args:

            output_dir: Optional directory to save the files (defaults to current directory)

            

        Returns:

            Tuple of (meta_json_path, schema_xml_path)

        """
        if not self.api_info:
            self.extract_api_info()
        
        # Set default output directory if not provided
        if not output_dir:
            output_dir = os.getcwd()
        else:
            # Create directory if it doesn't exist
            os.makedirs(output_dir, exist_ok=True)
        
        # Generate filenames
        meta_filename = f"{self.api_name.lower()}_datasource_plugin_meta.json"
        schema_filename = f"{self.api_name.lower()}_default_schema.orx"
        
        meta_path = os.path.join(output_dir, meta_filename)
        schema_path = os.path.join(output_dir, schema_filename)
        
        # Save files
        self.save_datasource_plugin_meta(meta_path)
        self.save_default_schema(schema_path)
        
        return (meta_path, schema_path)