docs icon indicating copy to clipboard operation
docs copied to clipboard

Use a DTO for Posting

Open kraimbaud opened this issue 7 years ago • 5 comments

In my project I need to create users, but there are different type of users, and the business logic to create each users is very complex. So I have create a UserFactory. So the front just need to push a Business object and the back-end take care of everything.

In my case the front-end can never persist a User Object directly in the DB. It is the role of the back-end.

Following this doc to use DTO for Reading, I'm trying to do the same for posting: https://api-platform.com/docs/core/dto/#how-to-use-a-dto-for-reading

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 find any documentation about that.

How can I do that? Can we improve the documentation for this demand?

kraimbaud avatar Nov 18 '18 16:11 kraimbaud

@kraimbaud The _api_swagger_context is not used if you didn't create a Decorator for swagger. Did you created it ? https://api-platform.com/docs/core/dto/#use-swagger-decorator

Jibbarth avatar Nov 28 '18 13:11 Jibbarth

@kraimbaud The _api_swagger_context is not used if you didn't create a Decorator for swagger. Did you created it ? https://api-platform.com/docs/core/dto/#use-swagger-decorator

Yes I did. The new route appear in the Swagger UI. I just need to know what do I have to put in the parameter section, but I don't find any doc about it.

kraimbaud avatar Dec 03 '18 12:12 kraimbaud

Oh, my bad.

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/

Jibbarth avatar Dec 04 '18 08:12 Jibbarth

Oh, my bad.

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/

Thanks, just what I needed

kraimbaud avatar Dec 04 '18 11:12 kraimbaud

@Jibbarth swagger decorator link is valid for v2.3

https://api-platform.com/docs/v2.3/core/dto/#use-swagger-decorator

But in latest version of docs this section is removed by commit:

https://github.com/api-platform/docs/commit/423304235fde13281ad3577976d663917dd16316#diff-f37f9f0e946c5a3bed34d25ca832d7d3

gustawdaniel avatar May 21 '19 17:05 gustawdaniel