首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
社区首页 >问答首页 >使用swagger-php将嵌套属性序列化为form-data

使用swagger-php将嵌套属性序列化为form-data
EN

Stack Overflow用户
提问于 2019-01-29 07:56:08
回答 1查看 2.9K关注 0票数 1

这就是在OpenApi https://github.com/OAI/OpenAPI-Specification/blob/master/versions/3.0.0.md中编码对象示例的方式

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
requestBody:
  content:
    multipart/mixed:
      schema:
        type: object
        properties:
          id:
            # default is text/plain
            type: string
            format: uuid
          address:
            # default is application/json
            type: object
            properties: {}
          historyMetadata:
            # need to declare XML format!
            description: metadata in XML format
            type: object
            properties: {}
          profileImage:
            # default is application/octet-stream, need to declare an image type only!
            type: string
            format: binary
      encoding:
        historyMetadata:
          # require XML Content-Type in utf-8 encoding
          contentType: application/xml; charset=utf-8
        profileImage:
          # only accept png/jpeg
          contentType: image/png, image/jpeg
          headers:
            X-Rate-Limit-Limit:
              description: The number of allowed requests in the current period
              schema:
                type: integer

我正在尝试实现相同的功能,但使用swagger-php。我不知道的是如何在@OA\MediaType中传递encodings来将test属性编码为multipart/form-data,因为默认情况下编码为application/json

例如:

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
 * @OA\Post(
 *     path="/admin/test",
 *     summary="Create new Test",
 *     description="Will attempt to create a new Test",
 *     tags={"Admin Test"},
 *     @OA\RequestBody(
 *          @OA\MediaType(
 *              mediaType="multipart/form-data",
 *              encoding={}
 *              @OA\Schema(
 *                  type="object",
 *                  @OA\Property(
 *                      property="test",
 *                      type="object",
 *                      description="test"
 *                      ref="#/components/schemas/MyTestSchema"
 *                  )
 *              )
 *      )

他们在这里有一些例子:

https://github.com/zircote/swagger-php/tree/master/Examples

但是我没有找到任何关于编码的例子

在这里,字段被定义为https://github.com/zircote/swagger-php/blob/master/src/Annotations/MediaType.php

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
   /**
     * A map between a property name and its encoding information.
     * The key, being the property name, must exist in the schema as a property.
     * The encoding object shall only apply to requestBody objects when the media type is multipart or application/x-www-form-urlencoded.
     */
    public $encoding = UNDEFINED;

我试过encoding={"recommended"={"contentType"="multipart/form-data"}},但它没有用。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-02-12 16:50:06

我认为唯一的解决方案是将所有字段:

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
 * @OA\Post(
 *     path="/admin/test",
 *     summary="Create new Test",
 *     description="Will attempt to create a new Test",
 *     tags={"Admin Test"},
 *     @OA\RequestBody(
 *          @OA\MediaType(
 *              mediaType="multipart/form-data",
 *              @OA\Schema(
 *                  @OA\Property(
 *                      property="test[name]",
 *                      type="string",
 *                      description="name"
 *                  ),
 *                  @OA\Property(
 *                      property="test[desc]",
 *                      type="string",
 *                      description="description"
 *                  )
 *              )
 *          )
 *      )
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/54416255

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档
查看详情【社区公告】 技术创作特训营有奖征文