user3615057
user3615057

Reputation: 21

Api Platform custom swagger/openapi context body

I am using Api Platform with Symfony 4, and I want to create a custom endpoint. Everything works fine, but I cannot change 2 things: body and response format (in openapi documentation).

Parameters and responses status codes works just fine.

*          "login"={
 *              "route_name"="api_login",
 *              "method" = "post",
 *              "openapi_context" = {
 *                  "parameters" = {},
 *                  "body" = {
 *                      "description" ="Username and password",
 *                      "schema" = {
 *                          "type" = "object",
 *                          "required" = {"email","password"},
 *                          "properties" = {
 *                                   "email" = {
 *                                      "type" = "string"
 *                                   },
 *                                   "password" = {
 *                                      "type" = "string"
 *                                   }
 *                          }
 *                      }
 *                  },
 *                  "responses" = {
 *                      "200" = {
 *                          "description" = "User logged in",
 *                          "schema" =  {
 *                              "type" = "object",
 *                              "required" = {
 *                                  "token",
 *                                  "refresh_token"
 *                              },
 *                              "properties" = {
 *                                   "token" = {
 *                                      "type" = "string"
 *                                   },
 *                                   "refresh_token" = {
 *                                      "type" = "string"
 *                                   }
 *                              }
 *                          }
 *                      },
 *                      "401" = {
 *                          "description" = "invalid password or email"
 *                      }
 *                  },
 *                  "summary" = "Login user in application",
 *                  "consumes" = {
 *                      "application/json",
 *                      "text/html",
 *                   },
 *                  "produces" = {
 *                      "application/json"
 *                   }
 *              }
 *          }

Upvotes: 1

Views: 11277

Answers (2)

pavel
pavel

Reputation: 149

This works for me, please see the documentation. https://swagger.io/docs/specification/describing-request-body/

 * @ApiResource(
 *     collectionOperations={
 *         "get": {
 *             "method": "GET",
 *             "access_control": "is_granted('ROLE_USER', object)",
 *         },
 *         "post": {
 *             "method": "POST",
 *             "access_control": "is_granted('ROLE_USER', object)",
 *             "openapi_context": {
 *                 "requestBody": {
 *                     "content": {
 *                         "application/ld+json": {
 *                             "schema": {
 *                                 "type": "object",
 *                                 "properties": {
 *                                     "token": {"type": "string", "example": "[email protected]"},
 *                                     "refresh_token": {"type": "string", "example": "123456"},
 *                                 },
 *                             },
 *                         },
 *                     },
 *                 },
 *             },
 *         },
 *     }
 * )

Upvotes: 2

Michael
Michael

Reputation: 26

Take a look at this response on api-platform issue (documentation here is really nicely formatted in yaml instead of keeping them in php arrays, quite nice idea) and read the docs, they will probably help You to decorate documentation in any way You want to.

Upvotes: 0

Related Questions