I'm trying to create a custom action/controller/operation in Api-platform.
Basically, i need a custom action documented in Swagger but not as ApiResource since the JSON body the action receives does not have a corresponding entity in the database.
The body would look something like this:
[
{
"someEntityId": "2",
"message": "test message",
"userId": "11"
},
{
"someEntityId": "8",
"message": "test message two",
"userId": "16"
}
]
As i said i cannot have a ApiResource since that would break REST principles and entity with these fields does not exist. I'm using this data to perform some custom business logic and this JSON is deserialized into a DTO, to be precise collection of DTOs.
Regular Symfony controller with routing should be the way to go, at least i think, but i can't get this route to show up in Swagger even with annotations for a controller.
I've tried this:
/**
* @Route(
* methods={"POST"},
* name="api_post_notify_users",
* path="/api/users/notify_users",
* )
* @SWG\Post(
* path="/api/users/notify_users",
* summary="Summary",
* description="Description",
* produces={"application/json"},
* @SWG\Response(
* response=200,
* description="Success",
* )
* )
* @return Response
*/
public function __invoke()
{
$data = $this->get('request_stack')->getCurrentRequest()->getContent();
return new Response($data);
}
Not sure what am i missing. Also, is there a way to validate this JSON array against the DTO so to be sure that all fields in JSON are being sent.
Any help is much appreciated!