Skip to content

Commit 54599f1

Browse files
mab05kdunglas
authored andcommitted
update docs for adding feature to update swagger context on properties (#315)
1 parent b256ae4 commit 54599f1

File tree

1 file changed

+119
-0
lines changed

1 file changed

+119
-0
lines changed

core/swagger.md

Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,125 @@ final class SwaggerDecorator implements NormalizerInterface
6969
}
7070
```
7171

72+
## Adding Swagger Context
73+
74+
Sometimes you may want to have additional information included in your Swagger documentation. Follow these steps.
75+
76+
### Properties
77+
78+
The following configuration will provide additional context to your Swagger definitions:
79+
80+
```php
81+
<?php
82+
83+
// src/AppBundle/Entity/Product.php
84+
85+
namespace AppBundle\Entity;
86+
87+
use ApiPlatform\Core\Annotation\ApiResource;
88+
use ApiPlatform\Core\Annotation\ApiProperty;
89+
use Doctrine\ORM\Mapping as ORM;
90+
use Symfony\Component\Validator\Constraints as Assert;
91+
92+
/**
93+
* @ApiResource
94+
* @ORM\Entity
95+
*/
96+
class Product // The class name will be used to name exposed resources
97+
{
98+
/**
99+
* @ORM\Column(type="integer")
100+
* @ORM\Id
101+
* @ORM\GeneratedValue(strategy="AUTO")
102+
*/
103+
public $id;
104+
105+
/**
106+
* @param string $name A name property - this description will be avaliable in the API documentation too.
107+
*
108+
* @ORM\Column
109+
* @Assert\NotBlank
110+
*
111+
* @ApiProperty(
112+
* "attributes"={
113+
* "swagger_context"={
114+
* "type"="string",
115+
* "enum"={"one", "two"},
116+
* "example"="one"
117+
* }
118+
* }
119+
* )
120+
*/
121+
public $name;
122+
123+
/**
124+
* @ORM\Column
125+
* @Assert\DateTime
126+
*
127+
* @ApiProperty(
128+
* "attributes"={
129+
* "swagger_context"={
130+
* "type"="string",
131+
* "format"="date-time"
132+
* }
133+
* }
134+
* )
135+
*/
136+
public $timestamp;
137+
}
138+
```
139+
140+
or in YAML:
141+
142+
```yaml
143+
resources:
144+
AppBundle\Entity\Product:
145+
properties:
146+
name:
147+
attributes:
148+
swagger_context:
149+
type: string
150+
enum: ['one', 'two']
151+
example: one
152+
timestamp:
153+
attributes:
154+
swagger_context:
155+
type: string
156+
format: date-time
157+
```
158+
159+
Will produce the following Swagger:
160+
```json
161+
{
162+
"swagger": "2.0",
163+
"basePath": "/",
164+
...
165+
166+
"definitions": {
167+
"Product": {
168+
"type": "object",
169+
"description": "This is a product.",
170+
"properties": {
171+
"id": {
172+
"type": "integer",
173+
"readOnly": true
174+
},
175+
"name": {
176+
"type": "string",
177+
"description": "This is a name.",
178+
"enum": ["one", "two"],
179+
"example": "one"
180+
},
181+
"timestamp": {
182+
"type": "string",
183+
"format": "date-time"
184+
}
185+
}
186+
}
187+
}
188+
}
189+
```
190+
72191
Previous chapter: [AngularJS Integration](angularjs-integration.md)
73192

74193
Next chapter: [The Serialization Process](serialization.md)

0 commit comments

Comments
 (0)