Optional array field generates with empty list [] instead of UNSET in from_dict method · Issue #1305 · openapi-generators/openapi-python-client · GitHub
Describe the bug
Optional array field generates with empty list [] instead of UNSET in from_dict method
Desktop (please complete the following information):
OS: macOS 13.3
Python Version: 3.10.12
openapi-python-client version:0.25.3
When an optional array field is not specified as required in the OpenAPI schema, openapi-python-client incorrectly generates the from_dict method. The generated code initializes the field with an empty list [] instead of the sentinel UNSET value.
This behavior causes the client to send an empty array in the request body for an optional field that was not provided, which is an incorrect representation of the data. The correct behavior should be to omit the field entirely from the payload.
Expected Behavior
For an optional array field, if the corresponding key is not present in the input dictionary, the generated code should set the attribute to UNSET.
Actual Behavior
The generated from_dict method initializes the array field with an empty list [], regardless of whether the key is present in the input dictionary.
Steps to Reproduce
Use the following minimal OpenAPI specification:
# mini_example.yamlopenapi: 3.0.3info:
title: Test APIversion: 1.0.0paths: {}components:
schemas:
ExampleResourceSchema:
type: objectproperties:
locations:
maxItems: 100description: 'This is a test array'type: arrayitems:
$ref: '#/components/schemas/LocationSchema'LocationSchema:
type: objectproperties:
name:
type: string
Inspect the generated ExampleResourceSchema.from_dict method.
The Problematic Code
The generated from_dict method for ExampleResourceSchema contains the following incorrect logic:
@classmethoddeffrom_dict(cls: type[T], src_dict: Mapping[str, Any]) ->T:
from ..models.location_schemaimportLocationSchemad=dict(src_dict)
locations= [] # <-- This line incorrectly initializes the field to an empty list_locations=d.pop("locations", UNSET)
forlocations_item_datain_locationsor []:
locations_item=LocationSchema.from_dict(locations_item_data)
locations.append(locations_item)
example_resource_schema=cls(
locations=locations,
)
example_resource_schema.additional_properties=dreturnexample_resource_schema
Proposed Fix
The correct implementation should check for the presence of the UNSET value before initializing the list. A working version of the method would be:
Activity
Fix optional list properties incorrectly initialized as empty lists i…
josteinl commented on Sep 26, 2025
I struggle with this problem as well, and would welcome a fix!
Robbie-Palmer commented on Oct 31, 2025
I'm having the same issue in v0.26.2
Interestingly, the to_dict() method in the same generated file implements this correctly:
The fix for from_dict() should follow the same pattern - initialize to UNSET, only create the list if _errors is not UNSET.
Fix optional list properties incorrectly initialized as empty lists i…
Fix optional list properties incorrectly initialized as empty lists i…
fix: Initialize optional lists as UNSET, not [] (#1346)