api-platform icon indicating copy to clipboard operation
api-platform copied to clipboard

abstract class MaxDepth on denormalization side

Open modiodio opened this issue 7 years ago • 2 comments

Hi there, I have a use case that I want to figure out.

I have these entities below :

Message Entity

/**
 * @ORM\Entity(repositoryClass="App\Repository\MessageRepository")
 * @ORM\InheritanceType("SINGLE_TABLE")
 * @ORM\DiscriminatorColumn(name="type", type="integer")
 * @ORM\DiscriminatorMap({1 = "QUESTION", 2 = "ANSWER", 3 = "COMMENT"})
 * @ApiResource(
 *    collectionOperations={}, itemOperations={}
 * )
 */
abstract class Message
{

    /**
     * Identifiant unique du message
     * @ORM\Column(type="guid")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="UUID")
     */
    private $id;

    /**
     * @ORM\Column(type="text", nullable=false)
     * @Groups({"messageWrite"})
     */
    private $message;

    ...
}

Question Entity

class Question extends Message
{
    /**
     * @ORM\ManyToOne(targetEntity="App\Entity\User", inversedBy="messageReceived")
     * @ORM\JoinColumn(name="recipient", referencedColumnName="id")
     * @ApiSubresource
     * @Groups({"messageWrite"})
     */
    private $recipient;

    ...
}

Answer Entity

/**
 * @ApiResource(
 *    collectionOperations={
 *        "get",
 *        "post"   = {"method"="POST", "denormalization_context"={"groups"={"messageWrite"}}}
 *    },
 *    itemOperations={
 *        "get", "put", "delete"
 *    },
 *    attributes={
 *        "normalization_context"={"groups"={"messageRead"}}
 *    }
 * )
 * @ORM\Entity(repositoryClass="App\Repository\AnswerRepository")
 */
class Answer extends Message
{
    /**
     * @ORM\OneToOne(targetEntity="App\Entity\Question", inversedBy="answer")
     * @ORM\JoinColumn(nullable=true, name="question", referencedColumnName="id", onDelete="CASCADE")
     * @Groups({"messageWrite"})
     * @ApiSubresource(maxDepth=1)
     */
    private $question;

    ...
}

On the entity Answer, I added the endpoint POST to answer to a question. But In swagger I want to have only the parameter "question" and not all the field behind.

What I have now :

{
  "question": {
    "message": "string",
    ...
  },
  "message": "string",
  ...
}

What I want to have

{
  "question": "string",
  "message": "string",
  ...
}

modiodio avatar Apr 24 '18 11:04 modiodio

Hello guys, I am still having the problem at 2022. Even if I do not use DiscriminatorColumn. I notice that even if I don't use inheritances. Only by creating the relations Question and Answer on the same message entity, the problem remains the same. I want to use this in that way to get atomic resource as described by @dunglas

modiodio avatar Jul 23 '22 15:07 modiodio

Currently it is not possible to set maxDepth to zero. is it possible to be able to set it to zero so that it only returns the uri of the resource and not the whole resource as in my case?

modiodio avatar Jul 24 '22 08:07 modiodio