api-spec.php 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. Dynamic References import all attributes from the reference object to the specified object.
  2. It works similar to the `#ref` however allows for customization of properties and attributes.
  3. The dynamic reference uses a `$` instead of `#` for the `ref` attribute.
  4. <?php
  5. /**
  6. * @SWG\Info(
  7. * version="1.0.0",
  8. * title="Example of using references in swagger-php",
  9. * )
  10. *
  11. * @SWG\Definition(
  12. * definition="ExampleDefinition",
  13. * @SWG\Property(
  14. * property="status",
  15. * type="string",
  16. * description="The status of a product",
  17. * enum={"available", "discontinued"},
  18. * default="available"
  19. * )
  20. * )
  21. */
  22. ?>
  23. Define a default object which be our base structure.
  24. In this case it is a response, which will contain a variable `data` property.
  25. We are also showing how it is possible to extend Definitions on sub Schemas by extending the 'ExampleDefinition'.
  26. <?php
  27. /**
  28. * @SWG\Response(
  29. * response="Json",
  30. * description="the basic response",
  31. * @SWG\Schema(
  32. * ref="$/definitions/ExampleDefinition",
  33. * @SWG\Property(
  34. * type="boolean",
  35. * property="success"
  36. * ),
  37. * @SWG\Property(
  38. * property="data"
  39. * ),
  40. * @SWG\Property(
  41. * property="errors",
  42. * type="object"
  43. * ),
  44. * @SWG\Property(
  45. * property="token",
  46. * type="string"
  47. * )
  48. * )
  49. * )
  50. *
  51. */
  52. ?>
  53. Then you can extend the response in this example POST request by using the '$' ref.
  54. As you can see `ref="$/responses/Json` is telling it to extend the base `Json` response.
  55. We follow the reference with a `Schema` layout which specifies that the `data` property will actually be a `Product`.
  56. <?php
  57. /**
  58. * @SWG\Post(
  59. * path="/api/path",
  60. * summary="Post to URL",
  61. * @SWG\Parameter(
  62. * name="body",
  63. * in="body",
  64. * required=true,
  65. * @SWG\Schema(
  66. * @SWG\Property(
  67. * property="name",
  68. * type="string",
  69. * maximum=64
  70. * ),
  71. * @SWG\Property(
  72. * property="description",
  73. * type="string"
  74. * )
  75. * )
  76. * ),
  77. * @SWG\Response(
  78. * response=200,
  79. * description="Example extended response",
  80. * ref="$/responses/Json",
  81. * @SWG\Schema(
  82. * @SWG\Property(
  83. * property="data",
  84. * ref="#/definitions/Product"
  85. * )
  86. * )
  87. * ),
  88. * security={{"Bearer":{}}}
  89. * )
  90. */
  91. ?>