Reputation: 5092
I'm using API platform in my Symfony4 app to expose my resources. It's a great framework but it force you by default to have all your Business logic in the front-end side, because it expose all your Entities and not a Business Object.
I don't like that and I prefer to have my business logic in the back-end side.
I need to create users, but there are different type of users. So I have create a UserFactory in the back-end-side. So the front just need to push a Business object and the back-end take care of everything.
The front front can never persist a User Object directly in the DB. It is the role of the back-end
Following this tutorial to use DTO for Reading: https://api-platform.com/docs/core/dto/#how-to-use-a-dto-for-reading
I'm trying to do the same for posting. And it works. Here is my Controller code:
/**
* @Route(
* path="/create/model",
* name="create-model",
* methods={"POST"},
* defaults={
* "_api_respond"=true,
* "_api_normalization_context"={"api_sub_level"=true},
* "_api_swagger_context"={
* "tags"={"User"},
* "summary"="Create a user Model",
* "parameters"={
*
* },
* "responses"={
* "201"={
* "description"="User Model created",
* "schema"={
* "type"="object",
* "properties"={
* "firstName"={"type"="string"},
* "lastName"={"type"="string"},
* "email"={"type"="string"},
* }
* }
* }
* }
* }
* }
* )
* @param Request $request
* @return \App\Entity\User
* @throws \App\Exception\ClassNotFoundException
* @throws \App\Exception\InvalidUserException
*/
public function createModel(Request $request)
{
$model = $this->serializer->deserialize($request->getContent(), Model::class, 'json');
$user = $this->userFactory->create($model);
$this->userRepository->save($user);
return $user;
}
It works great, but I would love my new resource to work in the Swagger UI, so I can Create via POST method new resources directly in the web interface.
For that I think I need to complete the parameter section in my _api_swagger_context. But I don't fin any documentation about that.
How can I do that?
Upvotes: 0
Views: 4551
Reputation: 5092
Found the answer here: https://github.com/api-platform/docs/issues/666
You can fill parameters like this :
"parameters" = {
{
"name" = "data",
"in" = "body",
"required" = "true",
"schema" = {
"type" = "object",
"properties" = {
"firstName"={"type"="string"},
"lastName"={"type"="string"},
"email" = {"type" = "string" }
}
},
},
},
More docs about parameters for swagger here : https://swagger.io/docs/specification/2-0/describing-parameters/
Upvotes: 1