0

I am using Swagger v.2 with PHP Annotations and right now I am facing a small issue with "Examples" in request body.

In my controller it looks like this:

/**
     * @SWG\Post(
     *   tags={"pet"},
     *   path="/pet",
     *   summary="Create a pet",
     *   description="Create a pet",
     *   operationId="CreatePet",
     *   consumes={"application/json"},
     *   produces={"application/json"},
     *   @SWG\Parameter(
     *       name="pet",
     *           required=true,
     *           in="body",
     *           description="Pet object to be created",
     *       @SWG\Schema(
     *           @SWG\Property(property="pet",ref="#/definitions/Pet")
     *       ),
     *        @SWG\Example(
     *          ref="somepath/pet.json"
     *      )
     *   )

I need to have different Schema examples depending on certain conditions and that is why I want to have Schema examples stated in a separate json file that would be referenced this way. I got this idea from this link.

However, this does not work and I get The annotation "@Swagger\Annotations\Example" doesn't exist error. If anybody knows how it is supposed to be fixed with Annotations, any help would be appreciated.

Thank you

CannotCode
  • 125
  • 1
  • 2
  • 10
  • FYI - the Specification you linked to is OpenAPI 3.0, not OpenAPI/Swagger 2.0 (which you are using). OpenAPI/Swagger 2.0 requires inline examples, it does not support schema examples in separate files. – Helen Mar 10 '18 at 13:57

1 Answers1

0

You can try this.

    /**
     * @SWG\Post(
     *   tags={"pet"},
     *   path="/pet",
     *   summary="Create a pet",
     *   description="Create a pet",
     *   operationId="CreatePet",
     *   consumes={"application/json"},
     *   produces={"application/json"},
     *   @SWG\Parameter(
     *       name="pet",
     *           required=true,
     *           in="body",
     *           description="Pet object to be created",
     *       @SWG\Schema(
     *           @SWG\Property(property="pet",ref="#/definitions/Pet")
     *       ),
     *        example={
     *            "data": {
     *            "api_token": "ffdca087b7f97117330824ceea948a99",
     *            "id": "1",
     *            "email": "joe@doe.com",
     *            "first_name": "Joe",
     *            "last_name": "Doe"
     *            }
     *        }
     *   )
Pratik Mehta
  • 707
  • 8
  • 27
  • [INFO] Unexpected field "example" for @SWG\Parameter(name="pet",in="body"), expecting "ref", "parameter", "name", "in", "description", "required", "schema", "type", "format", "allowEmptyValue", "items", "collectionFormat", "default", "maximum", "exclusiveMaximum", "minimum", "exclusiveMinimum", "maxLength", "minLength", "pattern", "maxItems", "minItems", "uniqueItems", "enum", "multipleOf", "x" in src/info.php on line 35 [WARN] [Syntax Error] Expected Doctrine\Common\Annotations\DocLexer::T_CLOSE_PARENTHESIS, got end of string in src/info.php on line 35. – flik Nov 01 '18 at 09:23