Skip to content

fix: Initialize optional lists as UNSET, not []#1346

Merged
dbanty merged 1 commit intoopenapi-generators:mainfrom
Robbie-Palmer:fix/optional-list-unset-1305
Oct 31, 2025
Merged

fix: Initialize optional lists as UNSET, not []#1346
dbanty merged 1 commit intoopenapi-generators:mainfrom
Robbie-Palmer:fix/optional-list-unset-1305

Conversation

@Robbie-Palmer
Copy link
Copy Markdown
Contributor

Summary

Optional list properties were being incorrectly initialized with empty lists [] instead of UNSET when calling from_dict() with missing values. This breaks the semantic distinction between an omitted field and a field explicitly set to an empty list, which matters for PATCH operations and APIs that treat these cases differently.

Changes

The fix modifies the list_property.py.jinja template to:

  • Initialize optional list properties as UNSET with proper type annotations
  • Only create and populate lists when the source value is not UNSET
  • Preserve existing behavior for required list properties

Test Plan

  • All existing tests pass (417 tests)
  • Updated golden record snapshots to reflect the corrected behavior
  • Changes are minimal and focused on optional list properties only

Fixes #1305
Fixes #961

…n from_dict

Optional list properties were being initialized with empty lists [] instead
of UNSET when calling from_dict() with missing values. This caused unnecessary
empty arrays to be included in API request payloads.

The fix modifies the list_property.py.jinja template to:
- Initialize optional list properties as UNSET with proper type annotations
- Only create and populate lists when the source value is not UNSET
- Preserve existing behavior for required list properties

Fixes: openapi-generators#1305
Fixes: openapi-generators#961
Copy link
Copy Markdown
Collaborator

@dbanty dbanty left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks great, thanks!

@dbanty dbanty enabled auto-merge October 31, 2025 14:48
@dbanty dbanty added this pull request to the merge queue Oct 31, 2025
Merged via the queue into openapi-generators:main with commit 30b27c2 Oct 31, 2025
22 checks passed
@knope-bot knope-bot bot mentioned this pull request Oct 31, 2025
@Robbie-Palmer Robbie-Palmer deleted the fix/optional-list-unset-1305 branch October 31, 2025 16:21
github-merge-queue bot pushed a commit that referenced this pull request Nov 3, 2025
> [!IMPORTANT]
> Merging this pull request will create this release

## Fixes

- Remove non-existent CHANGELOG.md references from UV and Poetry
templates (#1344)
- Initialize optional lists as UNSET, not [] (#1346)
- Correct docstring typos in client template (#1347)
- Replace bare except blocks with specific exception types (#1348)

### Update `uv_build` to 0.9

#1352 by @johnthagen

`uv` has been in the `0.9.x` release cycle for a while, so update
templates to use the corresponding `uv_build` range.

Co-authored-by: knope-bot[bot] <152252888+knope-bot[bot]@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Optional array field generates with empty list [] instead of UNSET in from_dict method Optional property of type list is initialized with empty list instead of UNSET when using from_dict()

2 participants