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

Transform empty iterable object to array instead EmptyObject #589

Open
psprokofiev opened this issue Jan 13, 2025 · 1 comment
Open

Transform empty iterable object to array instead EmptyObject #589

psprokofiev opened this issue Jan 13, 2025 · 1 comment

Comments

@psprokofiev
Copy link

If I pass empty Generator to json normalizer, I have {data: {}} in response, but I expect to get {data: []}.
Let's look at \CuyZ\Valinor\Normalizer\Transformer\RecursiveTransformer::defaultTransformer
You make tranformation of generator and check - is it closed or not, if closed - you return empty object, but why?

        if (is_iterable($value)) {
            ...

            if (! $result->valid()) {
                return EmptyObject::get();
            }

            return $result;
        }

Empty generator at response is empty list, isn't it?
How about to replace into

            if (! $result->valid()) {
                return [];
            }

Than response is as expected - just {data: []}.

Btw, removing JSON_FORCE_OBJECT from constructor of JsonNormalizer doesn't resolve the issue.

@grizzm0
Copy link

grizzm0 commented Jan 15, 2025

I ran into this myself the other day. We use classes that extends ArrayObject for collections. If it's empty the normalizer returns an empty object instead of an empty array.

The problem here is that both ['foo' => 'bar'] and ['foo'] are iterable.
You'd expect an empty object for the first one and an empty array for the second one.

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