You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
✏️ Describe the bug
When doing as the manual describes to wrap a collection (DataObject::collection()->wrap('foo')), the result JSON should be an object with a single key (foo) that is an array of objects representing each DataObject in the collection.
↪️ To Reproduce
Provide us a pest test like this one which shows the problem:
// Added to tests/DataCollectionTest.phpit('wraps data collection', function() {
$collection = SimpleData::collection(['A', 'B'])->wrap('test');
expect($collection->toJson())
->toEqual('{"test": [{"string":"A"},{"string":"B"}]}');
expect(json_encode($collection))
->toEqual('{"test": [{"string":"A"},{"string":"B"}]}');
});
it('wraps data collection of data objects', function() {
$collection = SimpleData::collection([
SimpleData::from('A'),
SimpleData::from('B'),
])->wrap('test');
expect($collection->toJson())
->toEqual('{"test": [{"string":"A"},{"string":"B"}]}');
expect(json_encode($collection))
->toEqual('{"test": [{"string":"A"},{"string":"B"}]}');
});
Outputs:
• Tests\DataCollectionTest > it wraps data collection of data objects
Failed asserting that two strings are equal.
at tests/DataCollectionTest.php:550
546▕ SimpleData::from('B'),
547▕ ])->wrap('test');
548▕
549▕ expect($collection->toJson())
➜ 550▕ ->toEqual('{"test": [{"string":"A"},{"string":"B"}]}');
551▕
552▕ expect(json_encode($collection))
553▕ ->toEqual('{"test": [{"string":"A"},{"string":"B"}]}');
554▕ });
--- Expected
+++ Actual
@@ @@
-'{"test": [{"string":"A"},{"string":"B"}]}'
+'[{"string":"A"},{"string":"B"}]'
• Tests\DataCollectionTest > it wraps data collection
Failed asserting that two strings are equal.
at tests/DataCollectionTest.php:537
533▕ it('wraps data collection', function() {
534▕ $collection = SimpleData::collection(['A', 'B'])->wrap('test');
535▕
536▕ expect($collection->toJson())
➜ 537▕ ->toEqual('{"test": [{"string":"A"},{"string":"B"}]}');
538▕
539▕ expect(json_encode($collection))
540▕ ->toEqual('{"test": [{"string":"A"},{"string":"B"}]}');
541▕ });
--- Expected
+++ Actual
@@ @@
-'{"test": [{"string":"A"},{"string":"B"}]}'
+'[{"string":"A"},{"string":"B"}]'
✅ Expected behavior
The collection values should be wrapped in the specified wrap() key.
I'm pretty confident this is due to way transform() is called in \Spatie\LaravelData\Concerns\TransformableData::toJson().
The default value for \Spatie\LaravelData\Concerns\BaseDataCollectable::transform(wrapExecutionType) is \Spatie\LaravelData\Support\Wrapping\WrapExecutionType::Disabled and the method is only called with \Spatie\LaravelData\Support\Wrapping\WrapExecutionType::Enabled in \Spatie\LaravelData\Concerns\ResponsableData::toResponse()
The documentation is unclear if this is expected behavior, but I don't think that it should be.
dshafik
changed the title
Wrapping Collections Doesn't Work
Wrapping Collections Doesn't Work As Expected
Jun 14, 2023
✏️ Describe the bug
When doing as the manual describes to wrap a collection (
DataObject::collection()->wrap('foo')
), the result JSON should be an object with a single key (foo
) that is an array of objects representing eachDataObject
in the collection.↪️ To Reproduce
Provide us a pest test like this one which shows the problem:
Outputs:
✅ Expected behavior
The collection values should be wrapped in the specified
wrap()
key.🖥️ Versions
Laravel: 10.12.0
Laravel Data: 3.5.1
PHP: 8.1.18
The text was updated successfully, but these errors were encountered: