Skip to content

Fix associative collection key extraction for pre-instantiated DTO objects#93

Merged
dereuromark merged 4 commits intomasterfrom
fix/associative-collection-dto-keys
Mar 23, 2026
Merged

Fix associative collection key extraction for pre-instantiated DTO objects#93
dereuromark merged 4 commits intomasterfrom
fix/associative-collection-dto-keys

Conversation

@dereuromark
Copy link
Copy Markdown
Contributor

Summary

When passing pre-instantiated DTO objects to an associative collection with a key field, the keys were not being extracted from the DTOs - falling back to numeric indices instead.

Before: Passing [new ItemDto(['id' => 10]), new ItemDto(['id' => 20])] to an associative="true" key="id" collection resulted in keys [0, 1]

After: Keys are correctly extracted as [10, 20]

Changes

  • Add extractKeyFromObject() helper method to extract keys from objects via getter methods, toArray(), or public properties
  • Fix createCollection() and createArrayCollection() to use existing DTO objects directly when they match the expected type (instead of creating empty DTOs)
  • Fix addValueToCollection() and addValueToArrayCollection() to extract keys from object elements
  • Update optimizations.twig template to use extractKeyFromObject() for generated setFromArrayFast() code
  • Add tests for associative collections with pre-instantiated DTO objects

- XmlEngine: Throw exception for field definitions missing "name" attribute
  instead of silently skipping them
- All engines: Use consistent is_string() check for shorthand field notation
- All engines: Add validation error for invalid field types (not string or array)

This makes debugging config issues easier and ensures consistent behavior
across XML, YAML, Neon, and PHP engines.
…jects

When passing pre-instantiated DTO objects to an associative collection with
a key field, the keys were not being extracted from the DTOs - falling back
to numeric indices instead.

Changes:
- Add extractKeyFromObject() helper method to extract keys from objects via
  getter methods, toArray(), or public properties
- Fix createCollection() and createArrayCollection() to use existing DTO
  objects directly when they match the expected type (instead of creating
  empty DTOs)
- Fix addValueToCollection() and addValueToArrayCollection() to extract keys
  from object elements
- Update optimizations.twig template to use extractKeyFromObject() for
  generated setFromArrayFast() code
- Add tests for associative collections with pre-instantiated DTO objects
@codecov
Copy link
Copy Markdown

codecov Bot commented Mar 23, 2026

Codecov Report

❌ Patch coverage is 31.57895% with 39 lines in your changes missing coverage. Please review.
✅ Project coverage is 82.64%. Comparing base (a2d317e) to head (ab182fc).
⚠️ Report is 2 commits behind head on master.

Files with missing lines Patch % Lines
src/Dto/Dto.php 42.85% 16 Missing ⚠️
src/Engine/NeonEngine.php 14.28% 6 Missing ⚠️
src/Engine/PhpEngine.php 25.00% 6 Missing ⚠️
src/Engine/YamlEngine.php 25.00% 6 Missing ⚠️
src/Engine/XmlEngine.php 16.66% 5 Missing ⚠️
Additional details and impacted files
@@             Coverage Diff              @@
##             master      #93      +/-   ##
============================================
- Coverage     83.48%   82.64%   -0.85%     
- Complexity     1390     1404      +14     
============================================
  Files            40       40              
  Lines          3348     3399      +51     
============================================
+ Hits           2795     2809      +14     
- Misses          553      590      +37     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@dereuromark dereuromark merged commit e654adc into master Mar 23, 2026
10 of 12 checks passed
@dereuromark dereuromark deleted the fix/associative-collection-dto-keys branch March 23, 2026 04:34
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.

1 participant