You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Swagger-PHP中指定POST请求的默认JSON请求体?

Ah, I get it—you want to set a default JSON request body for your POST endpoint in Swagger-PHP so it shows up pre-filled in the Swagger UI. The issue with your current code is that using default="{}" directly on the @SWG\Parameter for the body isn't the right approach for complex request bodies in Swagger 2.0. Let me walk you through the correct way to set this up:

Option 1: Set default values for individual fields

If you want each field in your request body to have a default value that the UI will auto-fill, add the default attribute to each @SWG\Property inside your schema:

/**
 * Setup order
 *
 * @SWG\Post(
 *     path="/order/setup",
 *     operationId="setupOrder",
 *     tags={"Orders"},
 *     summary="Setup an order with status draft.",
 *     description="Setup an order with status draft",
 *     consumes={"application/json"},
 *     @SWG\Parameter(
 *         name="body",
 *         in="body",
 *         description="Json order info body (customer and products info)",
 *         required=true,
 *         @SWG\Schema(
 *             type="object",
 *             @SWG\Property(
 *                 property="customer",
 *                 type="object",
 *                 @SWG\Property(property="name", type="string", default="John Doe"),
 *                 @SWG\Property(property="email", type="string", default="john@example.com")
 *             ),
 *             @SWG\Property(
 *                 property="products",
 *                 type="array",
 *                 @SWG\Items(
 *                     type="object",
 *                     @SWG\Property(property="id", type="integer", default=1),
 *                     @SWG\Property(property="quantity", type="integer", default=2)
 *                 ),
 *                 default={}
 *             )
 *         )
 *     ),
 *     @SWG\Response(
 *         response=200,
 *         description="Order created successfully"
 *     )
 * )
 */

Option 2: Provide a full example request body

If you want to show a complete, ready-to-use JSON example as the default in the UI, use the example attribute on the @SWG\Schema itself. This is great for demonstrating the full structure of a valid request:

/**
 * Setup order
 *
 * @SWG\Post(
 *     path="/order/setup",
 *     operationId="setupOrder",
 *     tags={"Orders"},
 *     summary="Setup an order with status draft.",
 *     description="Setup an order with status draft",
 *     consumes={"application/json"},
 *     @SWG\Parameter(
 *         name="body",
 *         in="body",
 *         description="Json order info body (customer and products info)",
 *         required=true,
 *         @SWG\Schema(
 *             type="object",
 *             @SWG\Property(
 *                 property="customer",
 *                 type="object",
 *                 @SWG\Property(property="name", type="string"),
 *                 @SWG\Property(property="email", type="string")
 *             ),
 *             @SWG\Property(
 *                 property="products",
 *                 type="array",
 *                 @SWG\Items(
 *                     type="object",
 *                     @SWG\Property(property="id", type="integer"),
 *                     @SWG\Property(property="quantity", type="integer")
 *                 )
 *             ),
 *             // This full example will be pre-filled in Swagger UI
 *             example={
 *                 "customer": {"name": "John Doe", "email": "john@example.com"},
 *                 "products": [{"id": 1, "quantity": 2}]
 *             }
 *         )
 *     ),
 *     @SWG\Response(
 *         response=200,
 *         description="Order created successfully"
 *     )
 * )
 */

Key Notes:

  • Remove the default="{}" from your @SWG\Parameter—that syntax doesn't work for body parameters, since the body's structure is defined by the schema.
  • The default attribute on properties tells Swagger what value to use if the client doesn't provide that field.
  • The example attribute on the schema gives a complete, human-readable example that the UI will display (and let users send directly).

Either approach will get you the default request body you want in Swagger UI—pick whichever fits your use case better!

内容的提问来源于stack exchange,提问作者Paul

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.25 06:59:44