NelmioApiDocBundle icon indicating copy to clipboard operation
NelmioApiDocBundle copied to clipboard

Support for fractal transformers

Open Nyholm opened this issue 6 years ago • 2 comments

I've looked a bit in the source code and tried to find a solution but I failed. So here is an issue with my feature request.

Im using Fractal transformers to output my data. A transformer may be as simple as:

namespace App\Transformer;

use AppBundle\Entity\Candidate\Candidate;
use League\Fractal\TransformerAbstract;

class CandidateTransformer extends TransformerAbstract
{
    /**
     * @param Candidate $candidate
     *
     * @return array
     */
    public function transform(Candidate $candidate)
    {
        $groups = [];

        foreach ($candidate->getGroupRelations() as $groupRelation) {
            $groups[$groupRelation->getGroup()->getUuid()] = $groupRelation->getType();
        }

        return [
            'id' => $candidate->getUuid(),
            'groups' => $groups,
        ];
    }
}

But it could also include other transformers.

I would love to see a feature that take these Fractal transformers and show then in "Example output". Just like we do with JMS serialiser.

Nyholm avatar Aug 29 '17 13:08 Nyholm

Actually for your use case you need definition of type for those returned data.

Does it exist anywhere in your code base? Because from what I see, your return fields might be dynamically generated.

Also are there use cases where fields might or might not exist due to specified conditions?

Might need to have something similar to JMS config file which supports expression language.

A workaround would be for you to specify

''' @Schema(type="object",@Property(....)....) '''

KoriSeng avatar Nov 07 '17 02:11 KoriSeng

See also https://github.com/nelmio/NelmioApiDocBundle/issues/1738 which has some fractal examples.

I like this idea, but @7thcubic is right that the definition of what the data looks like needs to be somewhere. The issue linked above has some @Schema(...) annotations/attributes on the transformer itself, which is neat. But it is also broken because ref=@Model(...) seems force reading all properties on the object. So maybe a solution here is to have a separate annotation/attribute for including objects that means "only read the @Schema info"

I think this is also related: https://github.com/nelmio/NelmioApiDocBundle/issues/1957 in how the @Model processes things.

chrisguitarguy avatar Mar 26 '22 01:03 chrisguitarguy