flugg / laravel-responder

A Laravel Fractal package for building API responses, giving you the power of Fractal with Laravel's elegancy.
MIT License
861 stars 86 forks source link

Different types in NoopSerializer #193

Open konstmal opened 1 year ago

konstmal commented 1 year ago

Flugg\Responder\Serializers\NoopSerializer::null(): Return value must be of type array, null returned at /var/www/html/vendor/flugger/laravel-responder/src/Serializers/NoopSerializer.php:51)

In NoopSerializer returned type of null() function is array, but function always returns null

SachinBahukhandi commented 1 year ago

How were you able to get this error? PHP version, laravel version?

konstmal commented 1 year ago

PHP v 8.0.1 Laravel v8.83.27

SachinBahukhandi commented 1 year ago

PHP v 8.0.1 Laravel v8.83.27

Sorry, forgot to mention, what is the pacakge-version? Also, could you please add a minimum reproducable example?

konstmal commented 1 year ago

I caught the error in 3.3.0 version, where was added type hint for functions In 3.2.0 there are no problems

SachinBahukhandi commented 1 year ago

I caught the error in 3.3.0 version, where was added type hint for functions In 3.2.0 there are no problems

I tried using this code snippet:

 return $transformation->resource(User::all())->serializer(NoopSerializer::class)->transform();

But it did not break.

konstmal commented 1 year ago

$anyUser = User::query()->first(); return [ 'other' => transformation($anyUser, new UserTransformer())->transform(), ];

This is my code, and the error exists where $anyUser has some null fields

ivanich274 commented 1 year ago

same problem, reverted back to v3.2.0

zdravstvuy1714 commented 1 year ago

+1

morhi commented 1 year ago

Would be nice to have a fix for this.. this code is obviously not working:

    public function null(): array
    {
        return null; # Cannot return null if type must be array
    }

I guess the type definition for this method should be ?array, because null must be returned for relationships, that cannot be resolved, so that they actually return null instead of [].