使用 swagger-php 将嵌套属性序列化为表单数据
Serialize nested properties as form-data with swagger-php
这是在 OpenApi 中完成编码对象示例的方式
https://github.com/OAI/OpenAPI-Specification/blob/master/versions/3.0.0.md
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
我正在努力实现同样的目标,但要大摇大摆-php。
我不知道的是如何在 @OA\MediaType
中传递 encodings
以将 test
属性 编码为 multipart/form-data
因为默认情况下编码为 application/json
EX:
* @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
/**
* 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"}}
但是没用
我认为唯一的解决方案是将所有字段:
* @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"
* )
* )
* )
* )
这是在 OpenApi 中完成编码对象示例的方式 https://github.com/OAI/OpenAPI-Specification/blob/master/versions/3.0.0.md
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
我正在努力实现同样的目标,但要大摇大摆-php。
我不知道的是如何在 @OA\MediaType
中传递 encodings
以将 test
属性 编码为 multipart/form-data
因为默认情况下编码为 application/json
EX:
* @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
/**
* 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"}}
但是没用
我认为唯一的解决方案是将所有字段:
* @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"
* )
* )
* )
* )