Skip to content

Commit 633b26f

Browse files
author
Doug Borg
committed
fix: correct array item type wrapping for reference items
Address review comment about array type handling. The previous implementation incorrectly used _wrap_optional on the entire List[...] after already handling optionality for reference items, which would produce incorrect types. ## Problem For arrays with Reference items: - Required array with items: `List[ItemType]` ✅ - Optional array with items: should be `Optional[List[Optional[ItemType]]]` ✅ (not `Optional[List[ItemType]]` ❌) ## Solution Build the Optional[List[...]] wrapper explicitly and pass the array's required status to _generate_property_from_reference's force_required parameter. This preserves the original behavior where reference items inherit the array's required status. For schema items (non-reference), always pass True since items are always required within the array. ## Testing - All 55 model_generator tests pass - All 250 project tests pass - Preserves original behavior for Optional[List[Optional[Type]]] pattern Co-authored-by: copilot-pull-request-reviewer
1 parent 6f8ecb2 commit 633b26f

File tree

1 file changed

+15
-6
lines changed

1 file changed

+15
-6
lines changed

src/openapi_python_generator/language_converters/python/model_generator.py

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -202,31 +202,40 @@ def _convert_array_type(
202202
Handle array type conversion.
203203
204204
:param schema: Schema object with type="array"
205-
:param required: Whether the field is required
205+
:param required: Whether the field is required (for the array itself)
206206
:param model_name: Name of the model being generated
207207
:return: TypeConversion for the array type
208208
"""
209209
import_types: Optional[List[str]] = None
210210

211+
# Build the List[...] wrapper
212+
if required:
213+
list_prefix = "List["
214+
list_suffix = "]"
215+
else:
216+
list_prefix = "Optional[List["
217+
list_suffix = "]]"
218+
211219
# Handle array items
212220
if isinstance(schema.items, Reference30) or isinstance(schema.items, Reference31):
221+
# For reference items, pass the array's required status to force_required
222+
# This makes items Optional when array is optional: Optional[List[Optional[Type]]]
213223
converted_reference = _generate_property_from_reference(
214224
model_name or "", "", schema.items, schema, required
215225
)
216226
import_types = converted_reference.type.import_types
217227
original_type = "array<" + converted_reference.type.original_type + ">"
218-
converted_type = _wrap_optional(
219-
f"List[{converted_reference.type.converted_type}]", required
220-
)
228+
converted_type = list_prefix + converted_reference.type.converted_type + list_suffix
221229
elif isinstance(schema.items, Schema30) or isinstance(schema.items, Schema31):
230+
# For schema items, always pass True (items are always required within the array)
222231
item_type_str = _normalize_schema_type(schema.items)
223232
original_type = "array<" + (item_type_str if item_type_str else "unknown") + ">"
224233
item_conversion = type_converter(schema.items, True, model_name)
225-
converted_type = _wrap_optional(f"List[{item_conversion.converted_type}]", required)
234+
converted_type = list_prefix + item_conversion.converted_type + list_suffix
226235
import_types = item_conversion.import_types
227236
else:
228237
original_type = "array<unknown>"
229-
converted_type = _wrap_optional("List[Any]", required)
238+
converted_type = list_prefix + "Any" + list_suffix
230239

231240
return TypeConversion(
232241
original_type=original_type,

0 commit comments

Comments
 (0)