Fix(classgen): support listOf union

This commit is contained in:
Johannes Kirschbauer
2025-06-03 18:18:02 +02:00
parent 682da0e396
commit a419e49bb9

View File

@@ -2,7 +2,7 @@
import argparse
import json
import logging
import sys
import traceback
from collections.abc import Callable, Iterable
from functools import partial
from pathlib import Path
@@ -38,6 +38,7 @@ def map_json_type(
for t in json_type:
res.extend(map_json_type(t))
return sort_types(set(res))
if isinstance(json_type, dict):
items = json_type.get("items")
if items:
@@ -46,6 +47,13 @@ def map_json_type(
if not json_type.get("type") and json_type.get("tsType") == "unknown":
return ["Unknown"]
union = json_type.get("oneOf")
if union:
res: list[str] = []
for t in union:
res.extend(map_json_type(t, nested_types, parent))
return sort_types(set(res))
return sort_types(map_json_type(json_type.get("type"), nested_types))
if json_type == "string":
return ["str"]
@@ -67,6 +75,7 @@ def map_json_type(
return [f"""dict[str, {" | ".join(sort_types(nested_types))}]"""]
if json_type == "null":
return ["None"]
msg = f"Python type not found for {json_type}"
raise Error(msg)
@@ -432,15 +441,13 @@ def main() -> None:
default=None,
)
parser.set_defaults(func=run_gen)
args = parser.parse_args()
try:
args.func(args)
except Error as e:
print(e)
sys.exit(1)
if __name__ == "__main__":
try:
main()
except Exception:
print("An error occurred:")
traceback.print_exc()