如何在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
defaultattribute on properties tells Swagger what value to use if the client doesn't provide that field. - The
exampleattribute 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

