forked from BasedHardware/omi
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinventory_app_client_schemas.py
More file actions
1169 lines (1039 loc) · 44.9 KB
/
Copy pathinventory_app_client_schemas.py
File metadata and controls
1169 lines (1039 loc) · 44.9 KB
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
#!/usr/bin/env python3
"""Inventory Flutter REST schemas and backend OpenAPI coverage.
This is a progress tool for the schema single-source migration. It intentionally
uses static source scanning so it can run before the backend import harness or
Flutter toolchain is available.
"""
from __future__ import annotations
import argparse
import json
import re
from dataclasses import dataclass
from pathlib import Path
from typing import Any
ROOT_DIR = Path(__file__).resolve().parents[2]
APP_SCHEMA_DIR = ROOT_DIR / 'app' / 'lib' / 'backend' / 'schema'
APP_API_DIR = ROOT_DIR / 'app' / 'lib' / 'backend' / 'http' / 'api'
APP_MODELS_DIR = ROOT_DIR / 'app' / 'lib' / 'models'
APP_CLIENT_SPEC_PATH = ROOT_DIR / 'docs' / 'api-reference' / 'app-client-openapi.json'
MODEL_REST_DTO_FILES = (
APP_MODELS_DIR / 'announcement.dart',
APP_MODELS_DIR / 'subscription.dart',
APP_MODELS_DIR / 'user_usage.dart',
)
LOCAL_NON_REST_SCHEMA_FILES = frozenset(
{
APP_SCHEMA_DIR / 'bt_device' / 'bt_device.dart',
APP_SCHEMA_DIR / 'message_event.dart', # local SSE/WS event schema, not a REST DTO
}
)
# These helpers parse an SSE transport frame, not an OpenAPI REST response.
# Keep this allowlist exact: adding an entry requires a behavioral parser test
# and must not be used to exempt normal HTTP response DTO decoding.
STREAM_PROTOCOL_DECODER_FUNCTIONS = frozenset(
{
(APP_API_DIR / 'messages.dart', 'parseVoiceMessageStreamChunk'),
}
)
# These helpers inspect a bounded error envelope to choose an application
# control path; they do not decode an OpenAPI response DTO. Keep the allowlist
# exact so normal REST response decoding remains covered by the migration gate.
NON_REST_RESPONSE_DECODER_FUNCTIONS = frozenset(
{
(APP_API_DIR / 'conversations.dart', 'isSyncRecoveryWindowExceededResponse'),
}
)
NON_REST_DECODE_CONTEXTS = frozenset({'stream_protocol', 'error_discriminator'})
CLASS_RE = re.compile(r'^\s*class\s+([A-Za-z_][A-Za-z0-9_]*)\b', re.MULTILINE)
ENUM_RE = re.compile(r'^\s*enum\s+([A-Za-z_][A-Za-z0-9_]*)\b', re.MULTILINE)
FROM_JSON_RE = re.compile(r'\b(?:factory|static)?\s*([A-Za-z_][A-Za-z0-9_]*)?\.?fromJson\s*\(')
TO_JSON_RE = re.compile(r'\btoJson\s*\(')
INTERPOLATION_RE = re.compile(r'\$\{[^}]+\}|\$[A-Za-z_][A-Za-z0-9_]*')
OPENAPI_PARAM_RE = re.compile(r'\{[^}]+\}')
GENERATED_WIRE_RE = re.compile(r"""package:omi/backend/schema/gen/[^'"]+_wire\.g\.dart|wire\.Generated""")
GENERATED_MARKERS = (
'Generated by',
'JsonSerializableGenerator',
'DO NOT EDIT',
)
RAW_DECODE_RE = re.compile(
r'\bjsonDecode\s*\(|\bjson\.decode\s*\(|\bas\s+(?:Map|List)<[^>]+>|\b[A-Za-z_][A-Za-z0-9_]*\.fromJson\s*\(|\[[\'"][A-Za-z_][A-Za-z0-9_]*[\'"]\]'
)
WIRE_DECODE_RE = re.compile(r'wire\.Generated[A-Za-z0-9_]+\.fromJson|\.fromGenerated\s*\(|fromGeneratedWireJson')
def repo_relative_path(path: Path) -> str:
"""Return a stable repository path for JSON reports and diagnostics."""
return path.relative_to(ROOT_DIR).as_posix()
def _collect_wire_backed_type_names() -> list[str]:
"""Collect Dart type names whose fromJson delegates to wire.Generated* parsers.
Covers two patterns:
- typedefs: ``typedef DevApiKey = wire.GeneratedDevApiKey;``
- adapter classes with ``factory X.fromGenerated(...)`` or ``static X fromGeneratedWireJson(...)``
Decode sites that call ``TypeName.fromJson`` for these names are generated-backed
even though the call-site text doesn't contain ``wire.Generated``.
"""
names: set[str] = set()
typedef_re = re.compile(r'^\s*typedef\s+([A-Za-z_][A-Za-z0-9_]*)\s*=\s*wire\.Generated', re.MULTILINE)
adapter_re = re.compile(
r'(?:factory|static)\s+([A-Za-z_][A-Za-z0-9_]*)\.(?:fromGenerated|fromGeneratedWireJson)\s*\('
)
for path in sorted(APP_SCHEMA_DIR.rglob('*.dart')):
if path.name.endswith('.g.dart') or path.name.endswith('.gen.dart'):
continue
text = path.read_text(encoding='utf-8')
names.update(typedef_re.findall(text))
names.update(adapter_re.findall(text))
return sorted(names, key=len, reverse=True) # longest first for alternation
_WIRE_BACKED_NAMES = _collect_wire_backed_type_names()
_WIRE_BACKED_DECODE_RE = (
re.compile(r'\b(?:' + '|'.join(re.escape(n) for n in _WIRE_BACKED_NAMES) + r')\.(?:fromJson|fromResponseJson)\s*\(')
if _WIRE_BACKED_NAMES
else re.compile(r'(?!)') # never matches
)
@dataclass(frozen=True)
class DartSchemaFile:
path: Path
classes: list[str]
enums: list[str]
from_json_count: int
to_json_count: int
generated: bool
generated_backed: bool
def to_report(self) -> dict[str, Any]:
return {
'path': repo_relative_path(self.path),
'classes': self.classes,
'enums': self.enums,
'fromJson': self.from_json_count,
'toJson': self.to_json_count,
'generated': self.generated,
'generated_backed': self.generated_backed,
}
@dataclass(frozen=True)
class AppRoute:
path: Path
route: str
normalized_route: str
line: int
function_name: str | None
function_start_line: int | None
function_end_line: int | None
http_method: str | None
called_function_ranges: tuple[tuple[str, int, int], ...]
def to_report(self) -> dict[str, Any]:
return {
'path': repo_relative_path(self.path),
'route': self.route,
'normalized_route': self.normalized_route,
'line': self.line,
'function_name': self.function_name,
'function_start_line': self.function_start_line,
'function_end_line': self.function_end_line,
'http_method': self.http_method,
'called_function_ranges': [
{'function_name': name, 'function_start_line': start, 'function_end_line': end}
for name, start, end in self.called_function_ranges
],
}
@dataclass(frozen=True)
class OpenApiOperation:
path: str
normalized_path: str
method: str
operation_id: str
unmodeled_success_response: bool
response_schema: str
request_schema: str | None
def to_report(self) -> dict[str, Any]:
return {
'path': self.path,
'normalized_path': self.normalized_path,
'method': self.method,
'operation_id': self.operation_id,
'unmodeled_success_response': self.unmodeled_success_response,
'response_schema': self.response_schema,
'request_schema': self.request_schema,
}
@dataclass(frozen=True)
class DartDecodeSite:
path: Path
line: int
kind: str
snippet: str
generated_backed: bool
context: str
def to_report(self) -> dict[str, Any]:
return {
'path': repo_relative_path(self.path),
'line': self.line,
'kind': self.kind,
'snippet': self.snippet,
'generated_backed': self.generated_backed,
'context': self.context,
}
@dataclass(frozen=True)
class AppOperationManifestItem:
path: Path
route: str
normalized_route: str
line: int
function_name: str | None
function_start_line: int | None
function_end_line: int | None
http_method: str | None
called_function_ranges: tuple[tuple[str, int, int], ...]
operations: list[OpenApiOperation]
decode_sites: list[DartDecodeSite]
raw_decode_scope: str
def to_report(self) -> dict[str, Any]:
stream_protocol_sites = [site for site in self.decode_sites if site.context == 'stream_protocol']
raw_sites = [
site
for site in self.decode_sites
if not site.generated_backed and site.context not in NON_REST_DECODE_CONTEXTS
]
raw_response_sites = [site for site in raw_sites if site.context == 'response_decode']
raw_request_sites = [site for site in raw_sites if site.context == 'request_encode']
generated_backed_sites = [site for site in self.decode_sites if site.generated_backed]
return {
'path': repo_relative_path(self.path),
'route': self.route,
'normalized_route': self.normalized_route,
'line': self.line,
'function_name': self.function_name,
'function_start_line': self.function_start_line,
'function_end_line': self.function_end_line,
'http_method': self.http_method,
'called_function_ranges': [
{'function_name': name, 'function_start_line': start, 'function_end_line': end}
for name, start, end in self.called_function_ranges
],
'operations': [operation.to_report() for operation in self.operations],
'raw_decode_scope': self.raw_decode_scope,
'decode_site_count': len(self.decode_sites),
'decode_sites': [site.to_report() for site in self.decode_sites],
'stream_protocol_decode_site_count': len(stream_protocol_sites),
'stream_protocol_decode_sites': [site.to_report() for site in stream_protocol_sites],
'generated_backed_decode_site_count': len(generated_backed_sites),
'generated_backed_decode_sites': [site.to_report() for site in generated_backed_sites],
'raw_decode_site_count': len(raw_sites),
'raw_decode_sites': [site.to_report() for site in raw_sites],
'raw_response_decode_site_count': len(raw_response_sites),
'raw_response_decode_sites': [site.to_report() for site in raw_response_sites],
'raw_request_encode_site_count': len(raw_request_sites),
'raw_request_encode_sites': [site.to_report() for site in raw_request_sites],
}
def scan_dart_schema_file(path: Path) -> DartSchemaFile:
text = path.read_text(encoding='utf-8')
return DartSchemaFile(
path=path,
classes=CLASS_RE.findall(text),
enums=ENUM_RE.findall(text),
from_json_count=len(FROM_JSON_RE.findall(text)),
to_json_count=len(TO_JSON_RE.findall(text)),
generated=any(marker in text[:500] for marker in GENERATED_MARKERS) or path.name.endswith('.g.dart'),
generated_backed=bool(
GENERATED_WIRE_RE.search(text) or WIRE_DECODE_RE.search(text) or _WIRE_BACKED_DECODE_RE.search(text)
),
)
def scan_dart_schemas() -> list[DartSchemaFile]:
return [
scan_dart_schema_file(path)
for path in sorted(APP_SCHEMA_DIR.rglob('*.dart'))
if not path.name.endswith('.gen.dart')
]
def scan_rest_dto_files() -> list[DartSchemaFile]:
paths = [*sorted(APP_SCHEMA_DIR.rglob('*.dart')), *sorted(APP_API_DIR.glob('*.dart'))]
paths.extend(path for path in MODEL_REST_DTO_FILES if path.exists())
return [
scan_dart_schema_file(path)
for path in paths
if not path.name.endswith('.gen.dart')
and not path.name.endswith('.g.dart')
and path not in LOCAL_NON_REST_SCHEMA_FILES
]
def scan_local_non_rest_schema_files() -> list[DartSchemaFile]:
return [scan_dart_schema_file(path) for path in sorted(LOCAL_NON_REST_SCHEMA_FILES) if path.exists()]
def decode_site_kind(line: str) -> str:
if 'jsonDecode' in line or 'json.decode' in line:
return 'jsonDecode'
if '.fromJson' in line:
return 'fromJson'
if ' as Map<' in line or ' as List<' in line:
return 'cast'
return 'field_access'
def decode_site_context(line: str) -> str:
stripped = line.strip()
if re.search(r'\b(?:requestBody|request|payload|body|fields)\s*\[', stripped):
return 'request_encode'
if re.search(r'\b(?:jsonDecode|json\.decode|response\.body|response\.bodyBytes|decoded|data)\b', stripped):
return 'response_decode'
if '.fromJson' in stripped or ' as Map<' in stripped or ' as List<' in stripped:
return 'response_decode'
return 'unknown'
def scan_dart_decode_sites() -> list[DartDecodeSite]:
paths = [*sorted(APP_SCHEMA_DIR.rglob('*.dart')), *sorted(APP_API_DIR.glob('*.dart'))]
paths.extend(path for path in MODEL_REST_DTO_FILES if path.exists())
sites: list[DartDecodeSite] = []
for path in paths:
if path.name.endswith('.gen.dart') or path.name.endswith('.g.dart') or path in LOCAL_NON_REST_SCHEMA_FILES:
continue
lines = path.read_text(encoding='utf-8').splitlines()
functions = _function_ranges(path.read_text(encoding='utf-8'))
for index, line in enumerate(lines, start=1):
stripped = line.strip()
if not stripped or stripped.startswith('//') or not RAW_DECODE_RE.search(line):
continue
window = '\n'.join(lines[max(0, index - 3) : min(len(lines), index + 3)])
enclosing_function = _enclosing_function(functions, index)
is_stream_protocol = (
enclosing_function is not None and (path, enclosing_function.name) in STREAM_PROTOCOL_DECODER_FUNCTIONS
)
is_error_discriminator = (
enclosing_function is not None
and (path, enclosing_function.name) in NON_REST_RESPONSE_DECODER_FUNCTIONS
)
sites.append(
DartDecodeSite(
path=path,
line=index,
kind=decode_site_kind(line),
snippet=stripped[:220],
generated_backed=bool(WIRE_DECODE_RE.search(window) or _WIRE_BACKED_DECODE_RE.search(window)),
context=(
'stream_protocol'
if is_stream_protocol
else 'error_discriminator' if is_error_discriminator else decode_site_context(line)
),
)
)
return sites
def normalize_app_route(route: str) -> str:
route = INTERPOLATION_RE.sub('{param}', route)
route = re.sub(r'(?<=[A-Za-z0-9_-])\{param\}$', '', route)
route = route.split('?', 1)[0]
route = re.sub(r'/+', '/', route)
return '/' + route.lstrip('/')
def normalize_openapi_path(path: str) -> str:
return OPENAPI_PARAM_RE.sub('{param}', path)
def route_prefix(route: str) -> str:
parts = route.strip('/').split('/')
if len(parts) >= 2:
return '/' + '/'.join(parts[:2])
if parts and parts[0]:
return '/' + parts[0]
return '/'
def _outer_quote_before(text: str, marker_start: int) -> str | None:
for index in range(marker_start - 1, -1, -1):
char = text[index]
if char not in ("'", '"'):
continue
backslashes = 0
cursor = index - 1
while cursor >= 0 and text[cursor] == '\\':
backslashes += 1
cursor -= 1
if backslashes % 2 == 0:
return char
return None
def _dart_string_tail_after_marker(text: str, marker_start: int, marker: str) -> str | None:
quote = _outer_quote_before(text, marker_start)
if quote is None:
return None
tail: list[str] = []
cursor = marker_start + len(marker)
if cursor < len(text) and text[cursor] == '}':
cursor += 1
interpolation_depth = 0
while cursor < len(text):
char = text[cursor]
if interpolation_depth == 0 and char == quote:
return ''.join(tail)
if char == '$' and cursor + 1 < len(text) and text[cursor + 1] == '{':
interpolation_depth += 1
tail.append('${')
cursor += 2
continue
if interpolation_depth > 0 and char in ("'", '"'):
string_quote = char
tail.append(char)
cursor += 1
while cursor < len(text):
nested_char = text[cursor]
tail.append(nested_char)
cursor += 1
if nested_char == '\\' and cursor < len(text):
tail.append(text[cursor])
cursor += 1
continue
if nested_char == string_quote:
break
continue
if interpolation_depth > 0 and char == '{':
interpolation_depth += 1
elif interpolation_depth > 0 and char == '}':
interpolation_depth -= 1
tail.append(char)
cursor += 1
return None
@dataclass(frozen=True)
class RouteOccurrence:
route: str
line: int
@dataclass(frozen=True)
class FunctionRange:
name: str
start_line: int
end_line: int
text: str
def _scan_marker_route_occurrences(
text: str, marker: str, *, must_start_with: str | None = None
) -> list[RouteOccurrence]:
routes: list[RouteOccurrence] = []
for match in re.finditer(re.escape(marker), text):
route = _dart_string_tail_after_marker(text, match.start(), marker)
if route is None:
continue
if must_start_with is not None and not route.startswith(must_start_with):
continue
routes.append(RouteOccurrence(route=route, line=text.count('\n', 0, match.start()) + 1))
return routes
def _scan_marker_routes(text: str, marker: str, *, must_start_with: str | None = None) -> list[str]:
return [
occurrence.route for occurrence in _scan_marker_route_occurrences(text, marker, must_start_with=must_start_with)
]
def _line_start_offsets(text: str) -> list[int]:
offsets = [0]
offsets.extend(index + 1 for index, char in enumerate(text) if char == '\n')
return offsets
def _line_for_offset(line_offsets: list[int], offset: int) -> int:
line = 1
for index, start in enumerate(line_offsets, start=1):
if start > offset:
break
line = index
return line
def _matching_brace_offset(text: str, open_brace: int) -> int | None:
depth = 0
in_string: str | None = None
in_line_comment = False
in_block_comment = False
cursor = open_brace
while cursor < len(text):
char = text[cursor]
next_char = text[cursor + 1] if cursor + 1 < len(text) else ''
if in_line_comment:
if char == '\n':
in_line_comment = False
cursor += 1
continue
if in_block_comment:
if char == '*' and next_char == '/':
cursor += 2
in_block_comment = False
else:
cursor += 1
continue
if in_string:
if char == '\\':
cursor += 2
continue
if char == in_string:
in_string = None
cursor += 1
continue
if char in ("'", '"'):
in_string = char
cursor += 1
continue
if char == '/' and next_char == '/':
cursor += 2
in_line_comment = True
continue
if char == '/' and next_char == '*':
cursor += 2
in_block_comment = True
continue
if char == '{':
depth += 1
elif char == '}':
depth -= 1
if depth == 0:
return cursor
cursor += 1
return None
FUNCTION_START_RE = re.compile(
r'(?m)^(?P<indent>[ \t]*)(?!class\b|enum\b|typedef\b|mixin\b|extension\b)(?:[A-Za-z_][^\n;=]*[ \t]+)?(?P<name>[A-Za-z_][A-Za-z0-9_]*)\s*\('
)
HTTP_METHOD_RE = re.compile(r"""\bmethod\s*:\s*['"]([A-Z]+)['"]""")
CONTROL_STATEMENT_NAMES = frozenset({'if', 'for', 'while', 'switch', 'catch'})
METHOD_DECLARATION_PREFIXES = (
'static ',
'Future',
'Stream',
'void ',
'bool ',
'int ',
'double ',
'String ',
'Map<',
'List<',
)
def _previous_nonempty_line(text: str, offset: int) -> str:
prefix = text[:offset].splitlines()
for line in reversed(prefix):
if line.strip():
return line
return ''
def _matching_delimiter_offset(text: str, open_offset: int, open_char: str, close_char: str) -> int | None:
depth = 0
cursor = open_offset
in_string: str | None = None
in_line_comment = False
in_block_comment = False
while cursor < len(text):
char = text[cursor]
next_char = text[cursor + 1] if cursor + 1 < len(text) else ''
if in_line_comment:
if char == '\n':
in_line_comment = False
cursor += 1
continue
if in_block_comment:
if char == '*' and next_char == '/':
cursor += 2
in_block_comment = False
else:
cursor += 1
continue
if in_string:
if char == '\\':
cursor += 2
continue
if char == in_string:
in_string = None
cursor += 1
continue
if char in ("'", '"'):
in_string = char
cursor += 1
continue
if char == '/' and next_char == '/':
cursor += 2
in_line_comment = True
continue
if char == '/' and next_char == '*':
cursor += 2
in_block_comment = True
continue
if char == open_char:
depth += 1
elif char == close_char:
depth -= 1
if depth == 0:
return cursor
cursor += 1
return None
def _function_ranges(text: str) -> list[FunctionRange]:
line_offsets = _line_start_offsets(text)
ranges: list[FunctionRange] = []
for match in FUNCTION_START_RE.finditer(text):
name = match.group('name')
if name in CONTROL_STATEMENT_NAMES:
continue
if match.group('indent'):
previous = _previous_nonempty_line(text, match.start())
stripped_line = text[
match.start() : text.find('\n', match.start()) if '\n' in text[match.start() :] else len(text)
].strip()
if not stripped_line.startswith(METHOD_DECLARATION_PREFIXES) and (
previous[:1].isspace() or previous.rstrip().endswith(('{', '}', ';'))
):
continue
close_paren = _matching_delimiter_offset(text, match.end() - 1, '(', ')')
if close_paren is None:
continue
body_match = re.match(r'\s*(?:async\*?|sync\*?)?\s*\{', text[close_paren + 1 :])
if body_match is None:
continue
open_brace = close_paren + body_match.end()
close_brace = _matching_brace_offset(text, open_brace)
if close_brace is None:
continue
ranges.append(
FunctionRange(
name=name,
start_line=_line_for_offset(line_offsets, match.start()),
end_line=_line_for_offset(line_offsets, close_brace),
text=text[match.start() : close_brace + 1],
)
)
return ranges
def _enclosing_function(functions: list[FunctionRange], line: int) -> FunctionRange | None:
for function in functions:
if function.start_line <= line <= function.end_line:
return function
return None
def _infer_http_method(function: FunctionRange | None) -> str | None:
if function is None:
return None
method_match = HTTP_METHOD_RE.search(function.text)
if method_match:
return method_match.group(1)
if 'makeStreamingApiCall' in function.text or 'makeMultipart' in function.text:
return 'POST'
return None
def _called_function_ranges(
function: FunctionRange | None, functions: list[FunctionRange]
) -> tuple[tuple[str, int, int], ...]:
if function is None:
return ()
called: list[tuple[str, int, int]] = []
for candidate in functions:
if candidate.name == function.name:
continue
if re.search(rf'\b{re.escape(candidate.name)}\s*\(', function.text):
called.append((candidate.name, candidate.start_line, candidate.end_line))
return tuple(called)
def _mask_dart_comments(text: str) -> str:
chars = list(text)
cursor = 0
in_string: str | None = None
in_line_comment = False
in_block_comment = False
while cursor < len(chars):
char = chars[cursor]
next_char = chars[cursor + 1] if cursor + 1 < len(chars) else ''
if in_line_comment:
if char == '\n':
in_line_comment = False
else:
chars[cursor] = ' '
cursor += 1
continue
if in_block_comment:
if char == '*' and next_char == '/':
chars[cursor] = ' '
chars[cursor + 1] = ' '
cursor += 2
in_block_comment = False
else:
if char != '\n':
chars[cursor] = ' '
cursor += 1
continue
if in_string:
if char == '\\' and cursor + 1 < len(chars):
cursor += 2
continue
if char == in_string:
in_string = None
cursor += 1
continue
if char in ("'", '"'):
in_string = char
cursor += 1
continue
if char == '/' and next_char == '/':
chars[cursor] = ' '
chars[cursor + 1] = ' '
cursor += 2
in_line_comment = True
continue
if char == '/' and next_char == '*':
chars[cursor] = ' '
chars[cursor + 1] = ' '
cursor += 2
in_block_comment = True
continue
cursor += 1
return ''.join(chars)
def scan_app_routes() -> list[AppRoute]:
routes: list[AppRoute] = []
for path in sorted(APP_API_DIR.glob('*.dart')):
original_text = path.read_text(encoding='utf-8')
text = _mask_dart_comments(original_text)
functions = _function_ranges(original_text)
env_routes = _scan_marker_route_occurrences(text, 'Env.apiBaseUrl', must_start_with='v')
base_routes = [
occurrence
for occurrence in env_routes
if _enclosing_function(functions, occurrence.line) is None
and re.search(rf"""_baseUrl\s*=\s*['"]\$\{{Env\.apiBaseUrl\}}{re.escape(occurrence.route)}['"]""", text)
]
for occurrence in env_routes:
function = _enclosing_function(functions, occurrence.line)
if function is None:
continue
routes.append(
AppRoute(
path=path,
route='/' + occurrence.route.lstrip('/'),
normalized_route=normalize_app_route(occurrence.route),
line=occurrence.line,
function_name=function.name if function else None,
function_start_line=function.start_line if function else None,
function_end_line=function.end_line if function else None,
http_method=_infer_http_method(function),
called_function_ranges=_called_function_ranges(function, functions),
)
)
for base_route in base_routes:
for local_route in _scan_marker_route_occurrences(text, '_baseUrl', must_start_with='/'):
route = base_route.route.rstrip('/') + local_route.route
function = _enclosing_function(functions, local_route.line)
routes.append(
AppRoute(
path=path,
route='/' + route.lstrip('/'),
normalized_route=normalize_app_route(route),
line=local_route.line,
function_name=function.name if function else None,
function_start_line=function.start_line if function else None,
function_end_line=function.end_line if function else None,
http_method=_infer_http_method(function),
called_function_ranges=_called_function_ranges(function, functions),
)
)
unique: dict[tuple[Path, str, str], AppRoute] = {}
for route in routes:
unique[(route.path, route.normalized_route, route.http_method or '')] = route
return sorted(unique.values(), key=lambda item: (str(item.path), item.normalized_route))
def load_openapi_schema_names(path: Path) -> list[str]:
if not path.exists():
return []
spec = json.loads(path.read_text(encoding='utf-8'))
return sorted(spec.get('components', {}).get('schemas', {}).keys())
def load_openapi_paths(path: Path) -> list[str]:
if not path.exists():
return []
spec = json.loads(path.read_text(encoding='utf-8'))
return sorted(spec.get('paths', {}).keys())
def is_unmodeled_success_schema(schema: dict[str, Any]) -> bool:
if schema == {}:
return True
return (
schema.get('type') == 'object'
and not schema.get('properties')
and schema.get('additionalProperties')
in (
None,
True,
)
)
def operation_has_unmodeled_success_response(operation: dict[str, Any]) -> bool:
for status_code, response in operation.get('responses', {}).items():
if not status_code.startswith('2'):
continue
content = response.get('content') or {}
json_content = content.get('application/json')
if not json_content:
continue
schema = json_content.get('schema')
if isinstance(schema, dict) and is_unmodeled_success_schema(schema):
return True
return False
def schema_ref_name(schema: Any) -> str:
if not isinstance(schema, dict):
return ''
ref = schema.get('$ref')
if isinstance(ref, str):
return ref.rsplit('/', 1)[-1]
if 'items' in schema:
item_name = schema_ref_name(schema['items'])
return f'array[{item_name}]' if item_name else 'array'
if schema.get('type'):
return str(schema['type'])
if 'anyOf' in schema:
names = [schema_ref_name(item) for item in schema['anyOf']]
return ' | '.join(name for name in names if name)
return ''
def operation_response_schema_name(operation: dict[str, Any]) -> str:
for status_code, response in operation.get('responses', {}).items():
if not status_code.startswith('2'):
continue
schema = ((response.get('content') or {}).get('application/json') or {}).get('schema')
name = schema_ref_name(schema)
if name:
return name
return ''
def operation_request_schema_name(operation: dict[str, Any]) -> str | None:
content = (operation.get('requestBody') or {}).get('content') or {}
schema = (content.get('application/json') or {}).get('schema')
name = schema_ref_name(schema)
return name or None
def load_openapi_operations(path: Path) -> list[OpenApiOperation]:
if not path.exists():
return []
spec = json.loads(path.read_text(encoding='utf-8'))
operations = []
for route, methods in spec.get('paths', {}).items():
for method, operation in methods.items():
if not isinstance(operation, dict):
continue
operations.append(
OpenApiOperation(
path=route,
normalized_path=normalize_openapi_path(route),
method=method.upper(),
operation_id=operation.get('operationId', ''),
unmodeled_success_response=operation_has_unmodeled_success_response(operation),
response_schema=operation_response_schema_name(operation),
request_schema=operation_request_schema_name(operation),
)
)
return sorted(operations, key=lambda item: (item.path, item.method))
def build_operation_manifest(
app_routes: list[AppRoute],
openapi_operations: list[OpenApiOperation],
decode_sites: list[DartDecodeSite],
) -> list[AppOperationManifestItem]:
operations_by_route: dict[str, list[OpenApiOperation]] = {}
for operation in openapi_operations:
operations_by_route.setdefault(operation.normalized_path, []).append(operation)
decode_sites_by_file: dict[Path, list[DartDecodeSite]] = {}
for site in decode_sites:
decode_sites_by_file.setdefault(site.path, []).append(site)
manifest: list[AppOperationManifestItem] = []
for route in app_routes:
operations = operations_by_route.get(route.normalized_route, [])
if route.http_method:
operations = [operation for operation in operations if operation.method == route.http_method]
if not operations:
continue
file_decode_sites = decode_sites_by_file.get(route.path, [])
if route.function_start_line is not None and route.function_end_line is not None:
route_decode_sites = [
site for site in file_decode_sites if route.function_start_line <= site.line <= route.function_end_line
]
helper_decode_sites = [
site
for site in file_decode_sites
for _, start_line, end_line in route.called_function_ranges
if start_line <= site.line <= end_line
]
route_decode_sites = sorted(
{*route_decode_sites, *helper_decode_sites}, key=lambda site: (site.path, site.line, site.kind)
)
scope = 'enclosing_function_and_called_helpers'
else:
route_decode_sites = file_decode_sites
scope = 'dart_api_file'
manifest.append(
AppOperationManifestItem(
path=route.path,
route=route.route,
normalized_route=route.normalized_route,
line=route.line,
function_name=route.function_name,
function_start_line=route.function_start_line,
function_end_line=route.function_end_line,
http_method=route.http_method,
called_function_ranges=route.called_function_ranges,
operations=operations,
decode_sites=route_decode_sites,
raw_decode_scope=scope,
)
)
return sorted(manifest, key=lambda item: (str(item.path), item.normalized_route))
def build_report(spec_path: Path) -> dict[str, Any]:
dart_files = scan_rest_dto_files()
local_non_rest_files = scan_local_non_rest_schema_files()
manual_files = [
item for item in dart_files if not item.generated and (item.from_json_count > 0 or item.to_json_count > 0)
]
generated_backed_files = [item for item in manual_files if item.generated_backed]
remaining_manual_files = [item for item in manual_files if not item.generated_backed]
app_routes = scan_app_routes()
decode_sites = scan_dart_decode_sites()
raw_decode_sites = [site for site in decode_sites if not site.generated_backed]
openapi_paths = load_openapi_paths(spec_path)
openapi_operations = load_openapi_operations(spec_path)
operation_manifest = build_operation_manifest(app_routes, openapi_operations, decode_sites)
openapi_prefixes = sorted({route_prefix(path) for path in openapi_paths})
app_route_prefixes = sorted({route_prefix(route.normalized_route) for route in app_routes})
uncovered_prefixes = sorted(set(app_route_prefixes) - set(openapi_prefixes))
app_route_keys = {(route.normalized_route, route.http_method) for route in app_routes if route.http_method}
app_route_paths_without_method = {route.normalized_route for route in app_routes if not route.http_method}
app_used_unmodeled_operations = [
operation
for operation in openapi_operations
if operation.unmodeled_success_response
and (
(operation.normalized_path, operation.method) in app_route_keys
or operation.normalized_path in app_route_paths_without_method
)
]
unmodeled_operations = [operation for operation in openapi_operations if operation.unmodeled_success_response]
return {
'dart_schema_dirs': [
repo_relative_path(APP_SCHEMA_DIR),
repo_relative_path(APP_API_DIR),
*(repo_relative_path(path) for path in MODEL_REST_DTO_FILES if path.exists()),
],
'app_client_openapi': repo_relative_path(spec_path),
'openapi_schema_count': len(load_openapi_schema_names(spec_path)),
'openapi_schemas': load_openapi_schema_names(spec_path),
'openapi_path_count': len(openapi_paths),
'openapi_paths': openapi_paths,