Skip to content

Commit 7e5cffb

Browse files
authored
Call the correct method of oneof examples (#3180)
1 parent d9ac7ea commit 7e5cffb

File tree

1 file changed

+38
-1
lines changed

1 file changed

+38
-1
lines changed

.generator/src/generator/formatter.py

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -320,6 +320,7 @@ def get_name_and_imports(schema):
320320

321321
def _format_oneof(schema, data, name, default_name, replace_values, imports):
322322
matched = 0
323+
matched_sub_schema = None
323324
extra_imports = one_of_imports = set()
324325
for sub_schema in schema["oneOf"]:
325326
try:
@@ -341,6 +342,7 @@ def _format_oneof(schema, data, name, default_name, replace_values, imports):
341342
# parameters += formatted
342343
parameters = value
343344
extra_imports = one_of_imports
345+
matched_sub_schema = sub_schema
344346
matched += 1
345347
except (KeyError, ValueError, TypeError) as e:
346348
print(f"{e}")
@@ -349,7 +351,24 @@ def _format_oneof(schema, data, name, default_name, replace_values, imports):
349351
raise ValueError(f"[{matched}] {data} is not valid for schema {name}")
350352

351353
imports |= extra_imports
354+
355+
# Detect if we need to use factory method due to type erasure collision
352356
if name:
357+
# Use prepare_oneof_methods to detect collisions
358+
from . import openapi
359+
methods_info = prepare_oneof_methods(schema, openapi.type_to_java)
360+
361+
# Find the method info for the matched sub_schema
362+
for method_info in methods_info:
363+
if method_info['schema'] == matched_sub_schema:
364+
if method_info['use_factory']:
365+
# Use static factory method
366+
return name, f"{name}.{method_info['constructor_name']}(\n{parameters})", imports
367+
else:
368+
# Use regular constructor
369+
return name, f"new {name}(\n{parameters})", imports
370+
371+
# Fallback to regular constructor if no match found
353372
return name, f"new {name}(\n{parameters})", imports
354373
elif "oneOf" in schema and default_name:
355374
imports.add(f"{default_name}Item")
@@ -480,6 +499,7 @@ def format_data_with_schema_list(
480499
name, imports = get_name_and_imports(schema)
481500

482501
if "oneOf" in schema:
502+
matched_sub_schema = None
483503
for sub_schema in schema["oneOf"]:
484504
try:
485505
named, value, one_of_imports = format_data_with_schema(
@@ -488,12 +508,29 @@ def format_data_with_schema_list(
488508
default_name=default_name,
489509
replace_values=replace_values,
490510
)
511+
matched_sub_schema = sub_schema
491512
except (KeyError, ValueError):
492513
continue
493514

494515
if name:
495516
one_of_imports.add(f"{name}")
496-
value = f"new {name}({value})"
517+
# Detect if we need to use factory method due to type erasure collision
518+
from . import openapi
519+
methods_info = prepare_oneof_methods(schema, openapi.type_to_java)
520+
521+
# Find the method info for the matched sub_schema
522+
for method_info in methods_info:
523+
if method_info['schema'] == matched_sub_schema:
524+
if method_info['use_factory']:
525+
# Use static factory method
526+
value = f"{name}.{method_info['constructor_name']}({value})"
527+
else:
528+
# Use regular constructor
529+
value = f"new {name}({value})"
530+
break
531+
else:
532+
# Fallback to regular constructor if no match found
533+
value = f"new {name}({value})"
497534
elif default_name:
498535
one_of_imports.add(f"{default_name}Item")
499536
value = f"new {default_name}Item({value})"

0 commit comments

Comments
 (0)