Symfony 如何在Swagger(OpenApi)中添加JSON请求和响应示例?
我的Symfony 4应用程序中有一个API端点,我想用NelmioApiDocBundle和Swagger记录它。端点将JSON作为请求数据,并返回一些自定义JSON作为响应。如何使用注释将它们的示例添加到文档中?我在文档页面上看不到任何示例,只有说明Symfony 如何在Swagger(OpenApi)中添加JSON请求和响应示例?,symfony,swagger,swagger-ui,openapi,nelmioapidocbundle,Symfony,Swagger,Swagger Ui,Openapi,Nelmioapidocbundle,我的Symfony 4应用程序中有一个API端点,我想用NelmioApiDocBundle和Swagger记录它。端点将JSON作为请求数据,并返回一些自定义JSON作为响应。如何使用注释将它们的示例添加到文档中?我在文档页面上看不到任何示例,只有说明 /** * @Route("/order/import", methods={"POST"}, name="order_import") * @OA\RequestBody (
/**
* @Route("/order/import", methods={"POST"}, name="order_import")
* @OA\RequestBody (
* request="order",
* description="Order data in JSON format",
* @OA\Schema(
* type="object",
* example={"hello": "world"}
* )
* )
* @OA\Response(
* response=200,
* description="Returns the JSON data after import",
* @OA\Schema(
* type="object",
* example={"foo": "bar"}
* )
* )
* @OA\Tag(name="import")
对于NelmioApiDocBundle v4,您可以这样做
use OpenApi\Annotations as OA;
/**
* @OA\Parameter(
* name="body",
* in="path",
* required=true,
* @OA\JsonContent(
* type="object",
* @OA\Property(property="property1", type="number"),
* @OA\Property(property="property2", type="number"),
* ),
* )
*
* @OA\Response(
* response=200,
* description="",
* @OA\JsonContent(
* type="object",
* @OA\Property(property="property1", type="number"),
* @OA\Property(property="property2", type="number"),
* )
* )
*/
对于v3
use Swagger\Annotations as SWG;
/**
* @SWG\Parameter(
* name="body",
* in="body",
* required=true,
* @SWG\Schema(
* @SWG\Property(property="test1", type="string"),
* @SWG\Property(property="test2", type="string"),
* ),
* )
*
* @SWG\Response(
* description="",
* response=200,
* @SWG\Schema(
* @SWG\Property(property="test1", type="string"),
* @SWG\Property(property="test2", type="string"),
* ),
* )
*/
但是最好通过@Model注释来完成,如中所述,如果您有DTO或实体。谢谢,使用@OA\JsonContent完成了这项任务!但是我没有使用@OA\Parameter,而是使用@OA\RequestBody更好地在api/doc中表示请求