nelmio / NelmioApiDocBundle

Generates documentation for your REST API from annotations
MIT License
2.22k stars 833 forks source link

Use swagger.yaml for API response definition instead of model/entity class #1891

Open RalphPungaKronbergs opened 2 years ago

RalphPungaKronbergs commented 2 years ago

Is it possible to use a swagger.yaml or the included definitions to reference/annotate them as a response structure definition instead of referencing a PHP model/entity?

Currently my Api Controller annotations look like (shortened):

     /**
     * Return Users
     *
     * @return JsonResponse|Response
     *
         * @Route("/api/v1/users", name="v1_user", methods={"GET"})
     *
     * @OA\Get(
     *     @OA\Response(
     *         response=200,
     *         description="Returns status 200 and a list of all users",
     *     )
     * )
     * @OA\Tag(name="users")
     */

My swagger.yaml looks like (shortened):

swagger: "2.0"
info:
  description: "Explanation of the data types"
host: "xxx"
basePath: "/api/v1"
schemes:
- "http"
- "https"
consumes:
- "application/json"
produces:
- "application/json"
paths:
  /users:
    get:
      description: "Searche for users"
      produces:
      - "application/json"
      responses:
        "200":
          description: "The result"
          schema:
            $ref: "#/definitions/UserCollectionResponse"
definitions:
  UserCollectionResponse:
    type: "object"
    properties:
      success:
        type: "boolean"
      created:
        type: "string"
        format: "date-time"
      results:
        type: "array"
        items:
          $ref: "#/definitions/User"
  User:
    type: "object"
    required:
    - "id"
    properties:
      name:
        type: "string"
      street:
        type: "string"
      streetNumber:
        type: "string"
      city:
        type: "string"
      zip:
        type: "string"
      phone:
        type: "string"

But how do I refer to these definitions to show as response definition in swagger UI?

Setup: Symfony 5.3 / nelmioApiDocBundle 4.6.2

Thanks for any help!

haidukua commented 2 years ago

@RalphPungaKronbergs https://symfony.com/bundles/NelmioApiDocBundle/current/faq.html#sharing-parameter-configuration