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

Bug Fix: DataTransformer returning same class as original should include JSONLD Data #3479

Merged
merged 22 commits into from
Apr 29, 2020
Merged

Bug Fix: DataTransformer returning same class as original should include JSONLD Data #3479

merged 22 commits into from
Apr 29, 2020

Conversation

silverbackdan
Copy link
Contributor

@silverbackdan silverbackdan commented Apr 3, 2020

Q A
Bug fix? yes
New feature? no
BC breaks? no
Deprecations? no
Tickets fixes #3474
License MIT
Doc PR api-platform/docs#...

In ApiPlatform\Core\DataTransformer\DataTransformerInterface there is a comment that states that DTOs should be allowed to return the same original object if no transformation is done. This resulted in missing LD data. (No @id, @context etc.).

     /**
     * Transforms the given object to something else, usually another object.
     * This must return the original object if no transformation has been done.
     *
     * @param object $object
     *
     * @return object
     */
    public function transform($object, string $to, array $context = []);

This update checks if the output class is the same as the original, and if so populated the extended metadata in the JsonLd\ItemNormalizer as it would not be added using the JsonLd\ObjectNormalizer

@silverbackdan
Copy link
Contributor Author

Scrutinizer does not appear to be related to this PR.

Behat test added.

If this shouldn't be allowed perhaps it's worth throwing some kind of exception, but it does seem a pretty useful way of using this feature for me and it doesn't seem to cost much to allow this feature to be used in this way.

@silverbackdan
Copy link
Contributor Author

silverbackdan commented Apr 3, 2020

The latest commit handles both if the dto output class is set as the same as the original resource and if the transformer falls back to returning the original resource. This is done using a serialization context key.

As the original resource will use the item normalizer that supports API Platform resources, this context key allows us to prevent an infinite loop.

@silverbackdan
Copy link
Contributor Author

All test failures appear to be related to the 2.5 branch and not this PR.

@soyuka soyuka requested a review from teohhanhui April 15, 2020 14:31
@soyuka
Copy link
Member

soyuka commented Apr 15, 2020

@teohhanhui do you have thoughts on this? I think that the use case is real, just not fond of the context to prevent recursion, maybe that there's something better to be done?

In `ApiPlatform\Core\DataTransformer\DataTransformerInterface` there is a comment that states that DTOs should be allowed to return the same original object if no transformation is done. This resulted in missing LD data. (No @id, @context etc.).
```php
     /**
     * Transforms the given object to something else, usually another object.
     * This must return the original object if no transformation has been done.
     *
     * @param object $object
     *
     * @return object
     */
    public function transform($object, string $to, array $context = []);
```
This update checks if the output class is the same as the original, and if so populated the extended metadata in the JsonLd\ItemNormalizer as it would not be added using the JsonLd\ObjectNormalizer
When a DataTransformer returns the original resource as a fallback, a context key is set so when repeating the serialization/normalization we can populate the extended jsonld data
Previous push reverted a change which is beneficial to performance. Instead of re-looping over the serializer/normalizers, if the DTO output class is defined as the same as the resource, we can detect this in the 1st round of serialization
…set no matter how the same class/object is returned
- Prevent BC change on AbstractItemNormalizer::transformOutput
- Remove useless documentation
@silverbackdan
Copy link
Contributor Author

Hi, I did make these changes but I think I did something wrong with rebasing and I'm not sure how to notify that the changes requested are done ... so this is me doing that.

Copy link
Contributor

@vincentchalamon vincentchalamon left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Minor changes requested, then 👍 for me.
Waiting for @api-platform/core-team opinion about this feature.

@silverbackdan
Copy link
Contributor Author

Thank you @vincentchalamon for the review - all changes applied now. I'll wait for core team's opinion.

`src/Serializer/AbstractItemNormalizer.php`: Update constant name
Change author phpdoc on test documents and entities
@soyuka
Copy link
Member

soyuka commented Apr 28, 2020

I like this, thanks @vincentchalamon @silverbackdan

@vincentchalamon
Copy link
Contributor

@silverbackdan Could you please update your branch from the 2.5 remote branch?

@silverbackdan
Copy link
Contributor Author

Thanks all! @vincentchalamon - I've just pressed the button to update from 2.5 remote - seems to have been able to merge without conflict.

@soyuka soyuka merged commit f8ccee0 into api-platform:2.5 Apr 29, 2020
@soyuka
Copy link
Member

soyuka commented Apr 29, 2020

thanks @silverbackdan

@silverbackdan silverbackdan deleted the patch/dto-output-class-same-as-original branch May 8, 2020 19:34
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

Successfully merging this pull request may close these issues.

None yet

4 participants