Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Wrapping Collections Doesn't Work As Expected #474

Closed
dshafik opened this issue Jun 14, 2023 · 2 comments
Closed

Wrapping Collections Doesn't Work As Expected #474

dshafik opened this issue Jun 14, 2023 · 2 comments

Comments

@dshafik
Copy link

dshafik commented 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 each DataObject in the collection.

↪️ To Reproduce
Provide us a pest test like this one which shows the problem:

// Added to tests/DataCollectionTest.php
it('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.

{
  "test": [
    {
      "string": "A"
    },
    {
      "string": "B"
    }
  ]
}

🖥️ Versions

Laravel: 10.12.0
Laravel Data: 3.5.1
PHP: 8.1.18

@dshafik
Copy link
Author

dshafik commented Jun 14, 2023

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 dshafik changed the title Wrapping Collections Doesn't Work Wrapping Collections Doesn't Work As Expected Jun 14, 2023
@rubenvanassche
Copy link
Member

Please read the docs on this: https://spatie.be/docs/laravel-data/v3/as-a-resource/wrapping

Data objects will only get wrapped when you're sending them as a response and never when calling toArray or toJson on it.

This is expected behavior, I've added a small section on how it can be enabled.

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

No branches or pull requests

2 participants