在Swagger / Zircote / Nelmio-api-doc中使用外部定义

时间:2017-10-05 11:23:40

标签: php symfony swagger nelmioapidocbundle

我使用以下版本:

zircote/swagger-php in version 2.0.10
nelmio/api-doc-bundle in version v3.0.0-BETA4

我的控制器有一个动作

    /**
     * @Operation(
     *     tags={"DeliverySlip"},
     *     summary="Send information after deliveryItems are processed and deliverySlip was scanned",
     *     @SWG\Response(
     *         response="200",
     *         description="Returned when successful"
     *     ),
     *     @SWG\Response(
     *         response="400",
     *         description="Returned on a missing request parameter"
     *     ),
     *     @SWG\Response(
     *         response="500",
     *         description="Returned on any other error"
     *     ),
     *     @SWG\Parameter(
     *        name="slipIdentifier",
     *        description="identifier of delivery slip",
     *        type="string",
     *        format="string",
     *        in="path"
     *     ),
     *     @SWG\Parameter(
     *        name="JSON update body",
     *        in="body",
     *        description="json login request object",
     *        required=true,
     *        @SWG\Schema(ref="#/definitions/product")
     *     )
     * )
     *
     * @Put("/deliveryslip/update/{slipIdentifier}", requirements={"slipIdentifier" = "\w+"})
     *
     * @param string $slipIdentifier
     * @param Request $request
     * @return JsonResponse
     */
    public function updateDeliverySlipAction($slipIdentifier, Request $request)

这是我想在Controller-Action中使用的模型/定义:

<?php

namespace Sendis\Presentation\RestBundle\Model;

use Swagger\Annotations as SWG;

/**
 * @SWG\Definition(
 *     definition="product",
 *     type="object",
 *     required={"name"}
 * )
 */
class Product
{
    /**
     * @SWG\Property(example="doggie")
     * @var string
     */
    public $name;
}

但是当我转到/ api / doc的文档页面时,我看到了这个错误:

Errors
Resolver error at paths./api/deliveryslip/update/{slipIdentifier}.put.parameters.1.schema.$ref
Could not resolve reference: #/definitions/product

我承认的下一件事: 我的 product.php 似乎根本没有被swagger读取。我可以在这里写任何我想要的东西。没错,即使我拼错了。这让我得出结论,swagger根本找不到我的 product.php

我对每一个提示都很有用。

亲切的问候, 最大

1 个答案:

答案 0 :(得分:0)

我找到了解决这个问题的方法。我需要加载外部定义及其完整名称空间,如下所示:

/**
     * @SWG\Put(
     *     path="/deliveryslip/update/{slipIdentifier}",
     *     tags={"DeliverySlip"},
     *     summary="Send information after deliveryItems are processed and deliverySlip was scanned",
     *     @SWG\Definition(
     *        definition="product",
     *        type="object",
     *        required={"name"}
     *     ),
     *     @SWG\Response(
     *         response="200",
     *         description="Returned when successful"
     *     ),
     *     @SWG\Response(
     *         response="400",
     *         description="Returned on a missing request parameter"
     *     ),
     *     @SWG\Response(
     *         response="500",
     *         description="Returned on any other error"
     *     ),
     *     @SWG\Parameter(
     *        name="slipIdentifier",
     *        description="identifier of delivery slip",
     *        type="string",
     *        format="string",
     *        in="path"
     *     ),
     *     @SWG\Parameter(
     *        name="JSON update body",
     *        in="body",
     *        description="json login request object",
     *        required=true,
     *        @SWG\Schema(
     *         type="array",
     *         @Model(type=Sendis\Presentation\RestBundle\Model\Product::class)
     *     )
     *     )
     * )
     *
     * @param string $slipIdentifier
     * @param Request $request
     * @return JsonResponse
     */
    public function updateDeliverySlipAction($slipIdentifier, Request $request)