-
Notifications
You must be signed in to change notification settings - Fork 9
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
getConfig() is being called early in controller if getParameters() is called. #5
Comments
Fixed in Additionally you can set encoder this way public function show($idx)
{
...
// trigger parse params and headers
$this->checkParameters();
// optional media type check
$mediaType = $this->getCodecMatcher()->getEncoderRegisteredMatchedType();
if (MediaTypeInterface::JSON_API_MEDIA_TYPE === $mediaType->getMediaType()) {
$this->getCodecMatcher()->setEncoder(function () {
$encoder = ...; // e.g. Encoder::instance(...);
return $encoder;
});
}
return $this->getContentResponse(...);
} tl;dr (history of the fix)The root of the problem is that this trait is trying to be helpful (maybe too much 😄) When
What are the possible solutions?Firstly I feel coupling between parameter parsing and matching encoder/decoder has to be changed. It doesn't look beautiful to intercept it with overriding I think
I tend to think the first variant is more flexible. In pseudo-code it might be if ($this->getEncoderMediaType()->getType() === 'application/vnd.api+json') {
$this->setEncoder(Encoder::instance([
Author::class => AuthorSchema::class,
...
]);
} If such a snippet is not added to Controller handler (e.g. What do you think? |
This works for me, and it makes more sense to be able to inject your own encoder vs just overriding an element in the config array. |
Nice to hear it 😄 |
I just found some strange behaviour in the execution order of
JsonApiTrait
.Assuming the following methods added to the base controller:
If
getParameters()
is called inside a controller action beforesetSchema()
then
getConfig()
is also called before the schema can be injected into theconfig array.
The text was updated successfully, but these errors were encountered: