Skip to content

Eager Loading Extension partial objects causing properties to not be available #1069

@bwegrzyn

Description

@bwegrzyn

It seems like the eager loading extension loads partial objects from Doctrine through the use of the @Groups annotation. While this seems like it would be a good idea in theory, proves to be quite a hassle in practice.

I've started using @dunglas https://github.com/dunglas/doctrine-json-odm to store events with a bunch of data in them as opposed to using inheritance with several children. Because these json_document fields contain quite a bit of info that I don't need to expose in the API, I don't want to expose the entire field. Instead, I create custom getters and setters and expose those via the @Groups annotation.

However, because the json_document field is not exposed through the @Groups annotation, it is always NULL when the eager loading extension is enabled, and thus the custom getters that I have exposed return NULL as well.

In the example below, you can see the context field being using to the store the json_document. Individual properties of this property are exposed through the getMessage() and setMessage() methods. This does not work when eager loading is enabled as the context field is always NULL, but works correctly when eager loading is disabled.

I'd like to propose a few methods on how to handle this:

  1. Remove partial objects from the eager loading extension as they prove to be more painful than helpful.
  2. Maybe add a property to the @ApiProperty annotation that will cause the eager loading extension to load the property regardless of the @Group annotation.
  3. Any other ideas?
/**
 * @ApiResource(
 *     attributes={
 *          "normalization_context"={"groups"={"api-read"}},
 *          "denormalization_context"={"groups"={"api-write"}}
 *     }
 * )
 *
 * @ORM\Entity
 */
class Event
{
    
    /**
     * @ORM\Column(type="json_document", options={"jsonb": true})
     *
     * @var EventContextInterface
     */
    protected $context;

    /**
     * @return EventContextInterface
     */
    public function getContext()
    {
        return $this->context;
    }

    /**
     * @param EventContextInterface $context
     */
    protected function setContext(EventContextInterface $context)
    {
        $this->context = $context;
    }
    
    /**
     * @Groups({"api-read"})
     *
     * @return string
     */
    public function getMessage()
    {
        return $this->getContext()->getMessage();
    }

    /**
     * @Groups({"api-write"})
     *
     * @param $message
     *
     * @return $this
     */
    public function setMessage($message)
    {
        $this->getContext()->setMessage($message);

        return $this;
    }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions