【问题标题】:Api Platform custom swagger/openapi context bodyApi 平台自定义 swagger/openapi 上下文主体
【发布时间】:2019-10-23 04:01:33
【问题描述】:

我正在使用带有 Symfony 4 的 Api 平台,并且我想创建一个自定义端点。一切正常,但我无法更改两件事:正文和响应格式(在 openapi 文档中)。

参数和响应状态码工作得很好。

*          "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"
 *                   }
 *              }
 *          }

【问题讨论】:

    标签: symfony swagger symfony4 api-platform.com


    【解决方案1】:

    这对我有用,请参阅文档。 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@example.com"},
     *                                     "refresh_token": {"type": "string", "example": "123456"},
     *                                 },
     *                             },
     *                         },
     *                     },
     *                 },
     *             },
     *         },
     *     }
     * )
    

    【讨论】:

    • 您能否解释一下这个特定答案如何解决 OPs 问题?
    【解决方案2】:

    看看this response on api-platform issue (这里的文档在 yaml 中的格式非常好,而不是将它们保存在 php 数组中,非常好的主意)和read the docs,它们可能会帮助您装饰文档随心所欲。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-02-25
      • 1970-01-01
      • 2020-01-15
      • 1970-01-01
      • 1970-01-01
      • 2020-02-26
      • 2022-10-04
      • 1970-01-01
      相关资源
      最近更新 更多