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

MappingFactory::fromArray() always seems to unbox the array #93

Closed
fr0gs opened this issue May 16, 2017 · 1 comment
Closed

MappingFactory::fromArray() always seems to unbox the array #93

fr0gs opened this issue May 16, 2017 · 1 comment

Comments

@fr0gs
Copy link

fr0gs commented May 16, 2017

I am constantly getting for this code:

    $mappings = array(OfferMapping::class); // Also tried with []
   

    $mapper = new Mapper($mappings);
    $transformer = new JsonApiTransformer($mapper);
    $serializer = new JsonApiSerializer($transformer);
		return response()->$serializer->serialize(Offer::all())
                     ->header('Access-Control-Allow-Origin', '*');

This error message:

1/1
FatalThrowableError in MappingFactory.php line 93:
Type error: Argument 1 passed to NilPortugues\Api\Mapping\MappingFactory::fromArray() must be of the type array, string given, called in /var/www/vendor/nilportugues/api-transformer/src/Mapping/Mapper.php on line 53
in MappingFactory.php line 93
at MappingFactory::fromArray('App\Api\Mappings\OfferMapping') in Mapper.php line 53
at Mapper->buildMapping('App\Api\Mappings\OfferMapping') in Mapper.php line 36
at Mapper->__construct(array('App\Api\Mappings\OfferMapping')) in OfferController.php line 35

I have tried passing

 $mapper = new Mapper([1]);
 $mapper = new Mapper(array(OfferMapping::class, $othermapping))

And other example values with no result. It seems to internally unbox the first one all the time.

I am using laravel 5.2 and using php-json-api 2.6.0.

@fr0gs
Copy link
Author

fr0gs commented May 16, 2017

I think I found the error.

    protected function buildMapping($mappedClass)
    {
        return (\is_string($mappedClass) && \class_exists($mappedClass, true)) ?
            MappingFactory::fromClass($mappedClass) :
            MappingFactory::fromArray($mappedClass);
    }

Which means that if you don't realize the class does not exist (was not properly imported) the function thinks it is an array. Would it be possible to throw a different error instead maybe to enhance debugging?

@fr0gs fr0gs closed this as completed Sep 8, 2017
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

1 participant