【问题标题】:Swagger PHP: how to declare property to use schema definition?Swagger PHP:如何声明属性以使用模式定义?
【发布时间】:2025-12-23 12:35:06
【问题描述】:

我的应用程序的响应如下所示:

{
  "status": "success",
  "data": {
      "status": "ready"
   },
  "request_id": "string"
}

我试图在 Swagger 中定义响应

 *           @SWG\Response (
 *              response=200,
 *              description="Success response",
 *              @SWG\Schema (
 *                  @SWG\Property(
 *                      property="status",
 *                      type="string",
 *                      default="success"
 *                  ),
 *                  @SWG\Property(
 *                      property="data",
 *                      @SWG\Schema(
 *                          ref="#/definitions/Service/models/Status"
 *                      )
 *                  ),
 *                  @SWG\Property(
 *                      property="request_id",
 *                      type="string"
 *                  ),
 *              )
 *          ),

但它不使用 Status 的 Schema 定义,所以我的回复实际上是这样的:

{
  "status": "success",
  "data": {},
  "request_id": "string"
}

如何定义数据属性以使用架构定义?或者可以用其他方式完成吗?

【问题讨论】:

    标签: php schema swagger


    【解决方案1】:

    这是多么有趣的事实,人们有时会在发布问题后找到答案。

    答案是:

    *           @SWG\Response (
     *              response=200,
     *              description="Success response",
     *              @SWG\Schema (
     *                  @SWG\Property(
     *                      property="status",
     *                      type="string",
     *                      default="success"
     *                  ),
     *                  @SWG\Property(
     *                      property="data",
     *                      ref="#/definitions/Service/models/Status"
     *                  ),
     *                  @SWG\Property(
     *                      property="request_id",
     *                      type="string"
     *                  ),
     *              )
     *          ),
    

    【讨论】:

    • 您是否要将您的答案标记为正确,以防其他人有同样的问题?
    • 您好@JJHunter,但这对我不起作用,我有相同的查询并应用您的答案但不起作用您可以查看我的问题*.com/questions/43627098/…
    • 干得好,你拯救了我的一天 :)
    最近更新 更多