Installation
composer require draw/swagger-bundle
Enable the bundle in config/bundles.php:
return [
// ...
Draw\SwaggerBundle\DrawSwaggerBundle::class => ['all' => true],
];
First Use Case: Annotate a Controller
Add @Swagger\Tag and @Swagger\Get annotations to a controller method:
use Draw\Swagger\Schema\Annotation as Swagger;
class ApiController extends AbstractController
{
/**
* @Swagger\Tag(name="Users")
* @Swagger\Get(
* path="/users",
* summary="Get all users",
* @Swagger\Response(
* response=200,
* description="List of users",
* @Swagger\Schema(ref="#/definitions/User")
* )
* )
*/
public function getUsers(): Response
{
// ...
}
}
Generate Documentation Run the command to generate Swagger UI:
php bin/console draw:swagger:generate
Access the docs at /swagger.
Documenting API Endpoints
Use @Swagger\* annotations for routes, parameters, and responses:
/**
* @Swagger\Post(
* path="/users",
* summary="Create a user",
* @Swagger\Parameter(
* name="user",
* in="body",
* required=true,
* @Swagger\Schema(ref="#/definitions/User")
* ),
* @Swagger\Response(
* response=201,
* description="User created"
* )
* )
*/
Defining Schemas
Create YAML/JSON schema files in config/swagger/ or inline with @Swagger\Schema:
# config/swagger/schemas/user.yaml
definitions:
User:
type: object
properties:
id: { type: integer }
name: { type: string }
Handling Doctrine Entities
Enable Doctrine support in config/packages/draw_swagger.yaml:
draw_swagger:
enableDoctrineSupport: true
Use @Swagger\Schema to reference entities:
@Swagger\Schema(ref="#/definitions/App\Entity\User")
Customizing Responses
Override default responses with responseConverter:
draw_swagger:
responseConverter: true
schema:
responses:
404:
description: "Resource not found"
Query Parameters Convert query parameters to Swagger attributes:
draw_swagger:
convertQueryParameterToAttribute: true
@Swagger\Schema to document form data structures.@ApiResource for seamless integration.Draw\Swagger\Extractor\ExtractorInterface to validate docs programmatically.FOSRestBundle Deprecation The bundle no longer supports FOSRestBundle. Migrate to native Symfony routing or use annotations directly.
Doctrine Auto-Detection
enableDoctrineSupport: null auto-detects Doctrine, but may fail silently. Explicitly set true/false if issues arise.
Schema Validation
The schema config is not validated—ensure it matches Swagger 2.0 format to avoid runtime errors.
Annotation Parsing Annotations must be parsed by the bundle’s extractor. Ensure:
Symfony\Bundle\FrameworkBundle\Controller\AbstractController.Circular References
Avoid circular references in schemas (e.g., User referencing Order which references User). Use ref sparingly.
php bin/console debug:swagger:extract
php bin/console cache:clear
Custom Extractors
Implement Draw\Swagger\Extractor\ExtractorInterface to add support for custom annotations or services.
Post-Processing
Override the Draw\SwaggerBundle\EventListener\GenerateEvent to modify the generated schema:
use Draw\SwaggerBundle\Event\GenerateEvent;
$event->setSchema(array_merge($event->getSchema(), ['custom' => 'field']));
Schema Aliases
Use definitionAliases to simplify FQCNs in docs:
draw_swagger:
definitionAliases:
- { class: App\Entity\User, alias: User }
Dynamic Documentation Generate docs on-demand via a custom command:
use Draw\Swagger\Generator;
$generator = new Generator();
$schema = $generator->generate();
file_put_contents('api-docs.json', json_encode($schema));
@Swagger\Parameter for Path/Query/Headers
Differentiate parameter types with in="path", in="query", etc.@Swagger\Example
Add examples for complex requests/responses:
@Swagger\Parameter(
@Swagger\Example(
summary="Example user",
value={"id": 1, "name": "John Doe"}
)
)
schema.info.version field for clarity.How can I help you explore Laravel packages today?