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

Exception in sync relationships when parent has selfLink = false #66

Closed
VoronovAlexander opened this issue Apr 8, 2021 · 3 comments
Closed

Comments

@VoronovAlexander
Copy link

VoronovAlexander commented Apr 8, 2021

I have
protected bool $selfLink = false; and BelongsToMany::make('articles') inside UserSchema.

Now i can success attach relation with POST /users/448/relationships/articles

But cant sync it with PATCH, i get 500 error on vendor/laravel-json-api/encoder-neomerx/src/Document.php:194
It is bug inside RelationshipDocument?
if i delete two lines it, then its works
Also if i set selfLink = true its works

image

image

Trace

[2021-04-08 05:06:57] local.ERROR: Unable to encode compound document. {"exception":"[object] (LogicException(code: 0): Unable to encode compound document. at /var/www/laravel/vendor/laravel-json-api/encoder-neomerx/src/Document.php:194)
[stacktrace]
#0 /var/www/laravel/vendor/laravel-json-api/core/src/Core/Responses/Internal/ResourceIdentifierCollectionResponse.php(80): LaravelJsonApi\\Encoder\\Neomerx\\Document->toJson(0)
#1 /var/www/laravel/vendor/laravel-json-api/core/src/Core/Responses/RelationshipResponse.php(104): LaravelJsonApi\\Core\\Responses\\Internal\\ResourceIdentifierCollectionResponse->toResponse(Object(Illuminate\\Http\\Request))
#2 /var/www/laravel/vendor/laravel/framework/src/Illuminate/Routing/Router.php(750): LaravelJsonApi\\Core\\Responses\\RelationshipResponse->toResponse(Object(Illuminate\\Http\\Request))
#3 /var/www/laravel/vendor/laravel/framework/src/Illuminate/Routing/Router.php(737): Illuminate\\Routing\\Router::toResponse(Object(Illuminate\\Http\\Request), Object(LaravelJsonApi\\Core\\Responses\\RelationshipResponse))
#4 /var/www/laravel/vendor/laravel/framework/src/Illuminate/Routing/Router.php(691): Illuminate\\Routing\\Router->prepareResponse(Object(Illuminate\\Http\\Request), Object(LaravelJsonApi\\Core\\Responses\\RelationshipResponse))
#5 /var/www/laravel/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(128): Illuminate\\Routing\\Router->Illuminate\\Routing\\{closure}(Object(Illuminate\\Http\\Request))
#6 /var/www/laravel/vendor/laravel-json-api/laravel/src/Http/Middleware/BootJsonApi.php(103): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}(Object(Illuminate\\Http\\Request))
#7 /var/www/laravel/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(167): LaravelJsonApi\\Laravel\\Http\\Middleware\\BootJsonApi->handle(Object(Illuminate\\Http\\Request), Object(Closure), 'external-v1')
#8 /var/www/laravel/vendor/laravel/framework/src/Illuminate/Routing/Middleware/SubstituteBindings.php(41): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}(Object(Illuminate\\Http\\Request))
#9 /var/www/laravel/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(167): Illuminate\\Routing\\Middleware\\SubstituteBindings->handle(Object(Illuminate\\Http\\Request), Object(Closure))
#10 /var/www/laravel/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(103): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}(Object(Illuminate\\Http\\Request))
#11 /var/www/laravel/vendor/laravel/framework/src/Illuminate/Routing/Router.php(693): Illuminate\\Pipeline\\Pipeline->then(Object(Closure))
#12 /var/www/laravel/vendor/laravel/framework/src/Illuminate/Routing/Router.php(668): Illuminate\\Routing\\Router->runRouteWithinStack(Object(Illuminate\\Routing\\Route), Object(Illuminate\\Http\\Request))
#13 /var/www/laravel/vendor/laravel/framework/src/Illuminate/Routing/Router.php(634): Illuminate\\Routing\\Router->runRoute(Object(Illuminate\\Http\\Request), Object(Illuminate\\Routing\\Route))
#14 /var/www/laravel/vendor/laravel/framework/src/Illuminate/Routing/Router.php(623): Illuminate\\Routing\\Router->dispatchToRoute(Object(Illuminate\\Http\\Request))
#15 /var/www/laravel/vendor/laravel/framework/src/Illuminate/Foundation/Http/Kernel.php(166): Illuminate\\Routing\\Router->dispatch(Object(Illuminate\\Http\\Request))
#16 /var/www/laravel/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(128): Illuminate\\Foundation\\Http\\Kernel->Illuminate\\Foundation\\Http\\{closure}(Object(Illuminate\\Http\\Request))
#17 /var/www/laravel/vendor/laravel/framework/src/Illuminate/Foundation/Http/Middleware/TransformsRequest.php(21): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}(Object(Illuminate\\Http\\Request))
#18 /var/www/laravel/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(167): Illuminate\\Foundation\\Http\\Middleware\\TransformsRequest->handle(Object(Illuminate\\Http\\Request), Object(Closure))
#19 /var/www/laravel/vendor/laravel/framework/src/Illuminate/Foundation/Http/Middleware/TransformsRequest.php(21): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}(Object(Illuminate\\Http\\Request))
#20 /var/www/laravel/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(167): Illuminate\\Foundation\\Http\\Middleware\\TransformsRequest->handle(Object(Illuminate\\Http\\Request), Object(Closure))
#21 /var/www/laravel/vendor/laravel/framework/src/Illuminate/Foundation/Http/Middleware/ValidatePostSize.php(27): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}(Object(Illuminate\\Http\\Request))
#22 /var/www/laravel/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(167): Illuminate\\Foundation\\Http\\Middleware\\ValidatePostSize->handle(Object(Illuminate\\Http\\Request), Object(Closure))
#23 /var/www/laravel/vendor/laravel/framework/src/Illuminate/Foundation/Http/Middleware/PreventRequestsDuringMaintenance.php(87): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}(Object(Illuminate\\Http\\Request))
#24 /var/www/laravel/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(167): Illuminate\\Foundation\\Http\\Middleware\\PreventRequestsDuringMaintenance->handle(Object(Illuminate\\Http\\Request), Object(Closure))
#25 /var/www/laravel/vendor/fruitcake/laravel-cors/src/HandleCors.php(37): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}(Object(Illuminate\\Http\\Request))
#26 /var/www/laravel/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(167): Fruitcake\\Cors\\HandleCors->handle(Object(Illuminate\\Http\\Request), Object(Closure))
#27 /var/www/laravel/vendor/fideloper/proxy/src/TrustProxies.php(57): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}(Object(Illuminate\\Http\\Request))
#28 /var/www/laravel/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(167): Fideloper\\Proxy\\TrustProxies->handle(Object(Illuminate\\Http\\Request), Object(Closure))
#29 /var/www/laravel/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(103): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}(Object(Illuminate\\Http\\Request))
#30 /var/www/laravel/vendor/laravel/framework/src/Illuminate/Foundation/Http/Kernel.php(141): Illuminate\\Pipeline\\Pipeline->then(Object(Closure))
#31 /var/www/laravel/vendor/laravel/framework/src/Illuminate/Foundation/Http/Kernel.php(110): Illuminate\\Foundation\\Http\\Kernel->sendRequestThroughRouter(Object(Illuminate\\Http\\Request))
#32 /var/www/laravel/public/index.php(55): Illuminate\\Foundation\\Http\\Kernel->handle(Object(Illuminate\\Http\\Request))
#33 {main}

[previous exception] [object] (LogicException(code: 0): Relation articles on resource object users does not have a self link. at /var/www/laravel/vendor/laravel-json-api/encoder-neomerx/src/Schema/Schema.php:186)
[stacktrace]
#0 /var/www/laravel/vendor/laravel-json-api/neomerx-json-api/src/Encoder/EncoderPropertiesTrait.php(453): LaravelJsonApi\\Encoder\\Neomerx\\Schema\\Schema->getRelationshipSelfLink(Object(LaravelJsonApi\\Core\\Resources\\JsonApiResource), 'articles')
#1 /var/www/laravel/vendor/laravel-json-api/encoder-neomerx/src/RelationshipDocument.php(84): Neomerx\\JsonApi\\Encoder\\Encoder->withRelationshipSelfLink(Object(LaravelJsonApi\\Core\\Resources\\JsonApiResource), 'articles')
#2 /var/www/laravel/vendor/laravel-json-api/encoder-neomerx/src/Document.php(192): LaravelJsonApi\\Encoder\\Neomerx\\RelationshipDocument->encode()
#3 /var/www/laravel/vendor/laravel-json-api/core/src/Core/Responses/Internal/ResourceIdentifierCollectionResponse.php(80): LaravelJsonApi\\Encoder\\Neomerx\\Document->toJson(0)
#4 /var/www/laravel/vendor/laravel-json-api/core/src/Core/Responses/RelationshipResponse.php(104): LaravelJsonApi\\Core\\Responses\\Internal\\ResourceIdentifierCollectionResponse->toResponse(Object(Illuminate\\Http\\Request))
#5 /var/www/laravel/vendor/laravel/framework/src/Illuminate/Routing/Router.php(750): LaravelJsonApi\\Core\\Responses\\RelationshipResponse->toResponse(Object(Illuminate\\Http\\Request))
#6 /var/www/laravel/vendor/laravel/framework/src/Illuminate/Routing/Router.php(737): Illuminate\\Routing\\Router::toResponse(Object(Illuminate\\Http\\Request), Object(LaravelJsonApi\\Core\\Responses\\RelationshipResponse))
#7 /var/www/laravel/vendor/laravel/framework/src/Illuminate/Routing/Router.php(691): Illuminate\\Routing\\Router->prepareResponse(Object(Illuminate\\Http\\Request), Object(LaravelJsonApi\\Core\\Responses\\RelationshipResponse))
#8 /var/www/laravel/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(128): Illuminate\\Routing\\Router->Illuminate\\Routing\\{closure}(Object(Illuminate\\Http\\Request))
#9 /var/www/laravel/vendor/laravel-json-api/laravel/src/Http/Middleware/BootJsonApi.php(103): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}(Object(Illuminate\\Http\\Request))
#10 /var/www/laravel/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(167): LaravelJsonApi\\Laravel\\Http\\Middleware\\BootJsonApi->handle(Object(Illuminate\\Http\\Request), Object(Closure), 'external-v1')
#11 /var/www/laravel/vendor/laravel/framework/src/Illuminate/Routing/Middleware/SubstituteBindings.php(41): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}(Object(Illuminate\\Http\\Request))
#12 /var/www/laravel/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(167): Illuminate\\Routing\\Middleware\\SubstituteBindings->handle(Object(Illuminate\\Http\\Request), Object(Closure))
#13 /var/www/laravel/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(103): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}(Object(Illuminate\\Http\\Request))
#14 /var/www/laravel/vendor/laravel/framework/src/Illuminate/Routing/Router.php(693): Illuminate\\Pipeline\\Pipeline->then(Object(Closure))
#15 /var/www/laravel/vendor/laravel/framework/src/Illuminate/Routing/Router.php(668): Illuminate\\Routing\\Router->runRouteWithinStack(Object(Illuminate\\Routing\\Route), Object(Illuminate\\Http\\Request))
#16 /var/www/laravel/vendor/laravel/framework/src/Illuminate/Routing/Router.php(634): Illuminate\\Routing\\Router->runRoute(Object(Illuminate\\Http\\Request), Object(Illuminate\\Routing\\Route))
#17 /var/www/laravel/vendor/laravel/framework/src/Illuminate/Routing/Router.php(623): Illuminate\\Routing\\Router->dispatchToRoute(Object(Illuminate\\Http\\Request))
#18 /var/www/laravel/vendor/laravel/framework/src/Illuminate/Foundation/Http/Kernel.php(166): Illuminate\\Routing\\Router->dispatch(Object(Illuminate\\Http\\Request))
#19 /var/www/laravel/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(128): Illuminate\\Foundation\\Http\\Kernel->Illuminate\\Foundation\\Http\\{closure}(Object(Illuminate\\Http\\Request))
#20 /var/www/laravel/vendor/laravel/framework/src/Illuminate/Foundation/Http/Middleware/TransformsRequest.php(21): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}(Object(Illuminate\\Http\\Request))
#21 /var/www/laravel/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(167): Illuminate\\Foundation\\Http\\Middleware\\TransformsRequest->handle(Object(Illuminate\\Http\\Request), Object(Closure))
#22 /var/www/laravel/vendor/laravel/framework/src/Illuminate/Foundation/Http/Middleware/TransformsRequest.php(21): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}(Object(Illuminate\\Http\\Request))
#23 /var/www/laravel/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(167): Illuminate\\Foundation\\Http\\Middleware\\TransformsRequest->handle(Object(Illuminate\\Http\\Request), Object(Closure))
#24 /var/www/laravel/vendor/laravel/framework/src/Illuminate/Foundation/Http/Middleware/ValidatePostSize.php(27): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}(Object(Illuminate\\Http\\Request))
#25 /var/www/laravel/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(167): Illuminate\\Foundation\\Http\\Middleware\\ValidatePostSize->handle(Object(Illuminate\\Http\\Request), Object(Closure))
#26 /var/www/laravel/vendor/laravel/framework/src/Illuminate/Foundation/Http/Middleware/PreventRequestsDuringMaintenance.php(87): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}(Object(Illuminate\\Http\\Request))
#27 /var/www/laravel/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(167): Illuminate\\Foundation\\Http\\Middleware\\PreventRequestsDuringMaintenance->handle(Object(Illuminate\\Http\\Request), Object(Closure))
#28 /var/www/laravel/vendor/fruitcake/laravel-cors/src/HandleCors.php(37): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}(Object(Illuminate\\Http\\Request))
#29 /var/www/laravel/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(167): Fruitcake\\Cors\\HandleCors->handle(Object(Illuminate\\Http\\Request), Object(Closure))
#30 /var/www/laravel/vendor/fideloper/proxy/src/TrustProxies.php(57): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}(Object(Illuminate\\Http\\Request))
#31 /var/www/laravel/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(167): Fideloper\\Proxy\\TrustProxies->handle(Object(Illuminate\\Http\\Request), Object(Closure))
#32 /var/www/laravel/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(103): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}(Object(Illuminate\\Http\\Request))
#33 /var/www/laravel/vendor/laravel/framework/src/Illuminate/Foundation/Http/Kernel.php(141): Illuminate\\Pipeline\\Pipeline->then(Object(Closure))
#34 /var/www/laravel/vendor/laravel/framework/src/Illuminate/Foundation/Http/Kernel.php(110): Illuminate\\Foundation\\Http\\Kernel->sendRequestThroughRouter(Object(Illuminate\\Http\\Request))
#35 /var/www/laravel/public/index.php(55): Illuminate\\Foundation\\Http\\Kernel->handle(Object(Illuminate\\Http\\Request))
#36 {main}
"} 
@lindyhopchris
Copy link
Contributor

@VoronovAlexander I'm not sure this actually is a bug. Disabling the self link is intended for use when the resource is not retrievable by its id. However, if you have a relationship endpoint then your resource is retrievable by its id.

Why have you disabled the self link?

@lindyhopchris
Copy link
Contributor

As a follow up, this is made clear in the docs which do state that the self link should be disabled if the resource is not retrievable by its id:
https://laraveljsonapi.io/docs/1.0/schemas/#disabling-the-self-link

@lindyhopchris
Copy link
Contributor

Closing this as I don't consider this a bug - the resource is retrievable by its id, so this package expects there to be a self link.

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